Here's an JSPs, action class and struts.xml
http://localhost:8080/Sample/student_edit.action calls form/jsp
studentDetails.
Erase name and submit form, this will result in validation being failed.
This will erase timestamp from hidden field
<s:hidden name="student.createdTimestmp"/>
If not validation error occurs, then it retains time.
-----Original Message-----
From: "Dave Newton" [davelnew...@gmail.com]
Date: 06/06/2012 12:57 PM
To: "Struts Users Mailing List" <user@struts.apache.org>
Subject: Re: Struts 2 losing timestamp in java.util.Date when validation failed
On Wed, Jun 6, 2012 at 12:54 PM, s <srrem...@excite.com> wrote:
> I found following work around.
> Sorry can't attach source JSP, because of client restrictions.
Here's the trivial workaround:
* Create a minimally-failing example containing essentially identical JSP.
* Post it.
Whether or not your client wishes to admit it, it's unlikely any sort of IP
law would protect the usage of a hidden field and date render in an example
JSP.
Dave
<?xml version="1.0" encoding="UTF-8" ?>
<!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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="default" namespace="/" extends="struts-default">
<action name="student_*" method="{1}" class="com.mysample.actions.StudentAction">
<interceptor-ref name="defaultStack" />
<result name="success">/jsp/studentDetails.jsp</result>
<result name="input">/jsp/studentDetails.jsp</result>
<result name="list">/jsp/studentList.jsp</result>
</action>
</package>
</struts>
package com.mysample.actions;
import java.sql.Timestamp;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import com.mysample.model.Student;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
public class StudentAction extends ActionSupport implements Preparable{
private static final long serialVersionUID = 1L;
private Student student;
public void prepare(){
//
}
public void prepareEditSave(){
//get Model from session
student = (Student) ActionContext.getContext().getSession().get("model");
}
public String edit(){
student = new Student();
student.setId(Integer.valueOf(1001));
student.setName("John Doe");
student.setBirthDate(new Date(75,02,15));
//Timestamp from DB
Timestamp tmStmp = new Timestamp(1338320605000l);
tmStmp.setNanos(344000000);
student.setCreatedTimestmp(tmStmp);
//put Model in Session
ActionContext.getContext().getSession().put("model", student);
return SUCCESS;
}
public String editSave(){
if (StringUtils.isBlank(student.getName())){
addFieldError("name", "Student Name is required");
return INPUT;
}
//persist to Database & remove it from session
ActionContext.getContext().getSession().remove("model");
return "list";
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Student Detail</title>
<s:head/>
</head>
<body>
<s:fielderror />
<s:actionerror />
<br>
<br>
<s:form name="studentDetail" id="studentDetail" method="post" theme="simple">
<s:hidden name="student.createdTimestmp"/>
<table>
<tr>
<td>
<s:label value="Id"/>:<s:textfield name="student.id" readonly="true"
size="15"/>
</td>
<td> </td>
</tr>
<tr>
<td>
<s:label value="Name"/>:<s:textfield name="student.name" size="50" />
</td>
<td> </td>
</tr>
<tr>
<td>
<s:date name="student.birthDate" var="birthDateId" format="MM/dd/yyyy"/>
<s:label value="Birth Date"/>:<s:textfield name="student.birthDate"
value="%{birthDateId}" label="Birth Date" size="10"/>
</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<s:submit action="student_editSave" value="Save"/>
<s:reset/>
</td>
</tr>
</table>
</s:form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Student Detail</title>
<s:head/>
</head>
<body>
<h1> Student created successfully ....!!!</h1>
</body>
</html>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org