简体   繁体   English

使用struts2将表单数据保存在数据库中

[英]saving form data in the database using struts2

When we click the save button it must save the form data in the database but its doing nothing. 当我们单击保存按钮时,它必须将表单数据保存在数据库中,但是什么也没做。 Below is the code 下面是代码

BodyDaywise.jsp BodyDaywise.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib prefix="s" uri="/struts-tags" %>    
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Day Wise</title>
</head>
<body align="center">
    <h1 align="center">Day Wise Form</h1>
        <html:form action="daywise" class="BodyDaywiseAction" method="POST" >

            LoginDate: <input type="date" name="LoginDate" displayformat="yyyy-mm-dd" label="Login Date(yyyy-mm-dd)"/><br><br>
            LoginTime:<input id="start" type="time"  name="LoginTime"/><br><br>
            LogoutTime:<input id="end" type="time"  name="LogoutTime"/><br><br>
            Task:<input type="textarea" name="Task" label="Task" cols="20" rows="5"/><br><br>

            <input type="submit" value="save" name="Save" onClick=""/>
            <button type="submit" value="Clear" name="clear">Clear</button>
            <input type="button" value="cancel" onClick="history.back();"/>
            </html:form>
</body>
</html>

Action class: BodyDaywiseAction.java 动作类:BodyDaywiseAction.java

package com.timesheet.action;
import com.opensymphony.xwork2.ActionSupport;
import com.timesheet.db.DaywiseDBO;
import java.sql.Date;
import java.sql.Time;
public class BodyDaywiseAction extends ActionSupport {
public BodyDaywiseAction()
{
}

private Date LoginDate;
private Time LoginTime;
private Time LogoutTime;
private String Task;


public Date getLoginDate() {
    return LoginDate;
}

public void setLoginDate(Date LoginDate) {
    this.LoginDate = LoginDate;
}

public Time getLoginTime() {
    return LoginTime;
}

public void setLoginTime(Time LoginTime) {
    this.LoginTime = LoginTime;
}

public Time getLogoutTime() {
    return LogoutTime;
}

public void setLogoutTime(Time LogoutTime) {
    this.LogoutTime = LogoutTime;
}

public String getTask() {
    return Task;
}

public void setTask(String Task) {
    this.Task = Task;
}

    private static final long serialVersionUID = 1L;



 @Override
public String execute() throws Exception {

     int i=DaywiseDBO.save(this);  
    if(i>0){  
    return "success";  
    }  
    return "error";  
}

@Override
public void validate() {
     if("".equals(getTask())){  
        addFieldError("Task", "Task must be filled !");
     }
}

}

DaywiseDBO.java DaywiseDBO.java

package com.timesheet.db;
import com.timesheet.action.BodyDaywiseAction;
import com.timesheet.dbutil.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DaywiseDBO {  

public static int save(BodyDaywiseAction BDA) throws Exception{  
int status=0;  
Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
DBUtil util = null;

try{  
util = new DBUtil();
conn = util.getConnection();
ps = conn.prepareStatement("insert into logintable values(?,?,?,?)");  
ps.setDate(1,BDA.getLoginDate());  
ps.setTime(2,BDA.getLoginTime());  
ps.setTime(3,BDA.getLogoutTime());  
ps.setString(4,BDA.getTask());  

status=ps.executeUpdate();  

}catch(Exception e){
e.printStackTrace();}  
return status;  
}  

} 

struts.xml struts.xml

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default, tiles-default">
    <result-types>
        <result-type name="tiles"
            class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>
    <action name="loginaction" class="com.timesheet.action.LoginAction" method="execute">
        <result name="input" >/Login.jsp</result>
        <result name="success" type="tiles">/bodydaywise.tiles</result>
        <result name="error" type="tiles">/error.jsp</result>
    </action>
    <action name="daywise" class="com.timesheet.action.BodyDaywiseAction">
        <result name="success" type="tiles">/bodydaywisesuccess.tiles</result>
        <result name="error" type="tiles">/error.jsp</result>
    </action>
    </package>
  </struts>

Please let me know if I'm missing something. 如果我缺少什么,请告诉我。

You are using wrong date/time type in the action class. 您在操作类中使用了错误的日期/时间类型。 Struts2 have not build-in converters for java.sql.* types. Struts2没有用于java.sql.*类型的内置转换器。 The date/time values should be converted to date if you use java.util.Date . 如果使用java.util.Date则日期/时间值应转换为日期。 objects of this type can contain both date and time values. 此类型的对象可以包含日期和时间值。

import java.util.Date;
import java.sql.Time;

Change the getters and setters accordingly to return the required. 相应地更改获取器和设置器以返回所需的内容。 You can also set the date object to Calendar and do some calculations, after that you can create a Timestamp object from the calendar. 您还可以将日期对象设置为“ Calendar并进行一些计算,然后可以从日历中创建一个“ Timestamp对象。 For example 例如

user.setCreateDate(new Timestamp(Calendar.getInstance().getTimeInMillis()));

sets the current date & time to the user object before the user is added to the database. 在将用户添加到数据库之前,将当前日期和时间设置为用户对象。

The example of using PreparedStatement : JDBC PreparedStatement example – Insert a record . 使用PreparedStatement的示例: JDBC PreparedStatement示例–插入一条记录

There's also an example to save only date part of the Date : Insert date value in PreparedStatement . 还有一个示例, 保存Date日期部分: 在PreparedStatement中插入日期值 (Don't use it, because it doesn't save a time portion of the Date ). (不要使用它,因为它不会保存Date的时间部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM