سؤال

I am using two struts/dojo datetimepicker in jsp page of my struts 2 web application. These allow to select date and displaying properly but when i submit the form and access the datetimepickers value in variables(with getter and setter) with same name as name of datetimepicker in jsp form it returns null value.

i want how to get datetimepickers value in struts action and then store them in MySQL database

my jsp page-->

 <%@taglib prefix="sx" uri="/struts-dojo-tags"%>
 <html>
 <head>
 <sx:head />
 </head>
 <body>
 <form name="cFrom" method="post" action="saveForm"  
 enctype="multipart/form-data">
  state Date: <sx:datetimepicker formatLength="medium" value="%{'today'}"     
  name="sDate"></sx:datetimepicker><br/>
  End Date: <sx:datetimepicker formatLength="medium" name="eDate">    
 </sx:datetimepicker><br/>
 <button type="submit">Save</button>
 </form>
 </body>
 </html>

action class-->

 import java.util.*;
 public class saveFormAction extends ActionSupport{

private static final long serialVersionUID = 1L;
private Date sDate;
    private Date eDate;
    public Date getsDate() {
    return sDate;
}

public void setsDate(Date sDate) {
    this.sDate = sDate;
}

public Date geteDate() {
    return eDate;
}

public void seteDate(Date eDate) {
    this.eDate = eDate;
}
    public String saveForm() throws SQLException{           
    Connection con=DBConnect.makeConnection();
    PreparedStatement pst=con.prepareStatement("INSERT INTO saveForm(Start_ON,  
    Expire_On) values(?,?)");
    pst.setDate(1, sDate);
    pst.setDate(2, eDate);
    int i=0;
    i=pst.executeUpdate();
    if(i>0){
        return SUCCESS; 
    }
    return ERROR;
   }
   }

MySQL table-->

 CREATE TABLE saveform(start_on DATE, Expire_On DATE)
هل كانت مفيدة؟

المحلول 2

I corrected my getter and setter and I had another error in the following lines-->

pst.setDate(1, sDate);
pst.setDate(2, eDate);

I replaced above lines of code with the following -->

pst.setDate(1, new java.sql.Date(sDate.getTime()));
pst.setDate(2, new java.sql.Date(eDate.getTime()));

نصائح أخرى

Datetimepicker is giving you value in format of 2013-03-27T00:00:00+11:00 when your you use private String Date but you are using private Date sDate which is causing problem of type missmacth . TO resolve this problem need to do type conversion from datetimepicker to java.util.Date format you can do it by this way

String obj = "2013-03-27T00:00:00+11:00";
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
        try {
            SimpleDateFormat parseFormatter = new SimpleDateFormat("yyyy-MM-dd");
            //SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss+hh:mm"); 
            Date date = parseFormatter.parse(obj);

            String formattedDate = formatter.format(date);
            System.out.println(formattedDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top