Sorry for this late answer but I simplified my code. The issue is always the same one.
My url to test the code :
http://localhost:8080/myApplication/secure/testForm.do
I see my JSP "testForm.jsp" initialised date with a good format : 12/01/2007
I click to my submit button.
When I come back on my JSP, I see date with a bad format : Fri Jan 12 00:00:00 CET 2007
But I do nothing in my onSubmit method and validate method.
The first time, my JSP can get CustomDateEditor with a good pattern, but JSP can't get the CustomDateEditor to run after the submit.
Détails of my application :
Web.xml :
Code:
<servlet>
<servlet-name>myApplication</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
myApplication-servlet.xml :
Code:
<!-- ========================= VALIDATOR ========================= -->
<bean name="myFormValidator" class="fr.coliposte.surf.web.springIhm.test.MyFormValidator" >
<property name="messageSource" ref="messageSourceAccessor" />
</bean>
<!-- =============== DEFINITIONS OF PUBLIC CONTROLLERS =============== -->
<bean name="/secure/testForm.do" class="fr.coliposte.surf.web.springIhm.test.MyController">
<property name="sessionForm" value="false"/>
<property name="commandName" value="myForm"/>
<property name="validator" ref="myFormValidator"/>
<property name="validateOnBinding" value="true"/>
<property name="messageSource" ref="messageSourceAccessor" />
<property name="formView" value="myTestView"/>
<property name="successView" value="myTestView"/>
<property name="useCacheControlHeader" value="false" />
</bean>
<!-- ======================= VIEW DEFINITIONS ========================= -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location" value="/WEB-INF/views.xml"/>
</bean>
views.xml :
Code:
<bean id="myTestView" class="org.springframework.web.servlet.view.JstlView">
<property name="url">
<value>/jsp/test/testForm.jsp</value>
</property>
</bean>
jsp/test/testForm.jsp :
Code:
<form name="form" method="post" action="<c:url value="/secure/testForm.do" />" >
<table align="center" class="form">
<tr>
<td>Date :
<spring:bind path="myForm.beginDay">
<input type="text" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>" size="25" />
</spring:bind>
</td>
</tr>
<tr>
<td colspan="3" class="button">
<input type="submit" value="submit"/>
</td>
</tr>
</table>
</form>
<!-- Support for Spring errors object -->
<spring:bind path="myForm.*">
<c:forEach var="error" items="${status.errorMessages}">
<b><font color="red">
<br><c:out value="${error}"/>
</font></b>
</c:forEach>
</spring:bind>
MyForm :
Code:
public class MyForm implements Serializable{
private Date beginDay;
public Date getBeginDay() {
return beginDay;
}
public void setBeginDay(Date beginDay) {
this.beginDay = beginDay;
}
}
MyController :
Code:
protected Object formBackingObject(HttpServletRequest request) throws Exception {
MyForm form = new MyForm();
form.setBeginDay(new Date());
return(form);
}
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(“dd/MM/yyyy”);
sdf.setLenient(false);
binder.registerCustomEditor(Date.class,new CustomDateEditor(sdf,true,10));
String[] requiredFields={"beginDay"};
binder.setRequiredFields(requiredFields);
}
protected void onBindAndValidate(HttpServletRequest request,
Object command,
BindException errors) throws Exception {
super.onBindAndValidate(request, command, errors);
}
protected Map referenceData(HttpServletRequest request) throws Exception {
return(super.referenceData(request));
}
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws Exception {
Map model = new HashMap();
MyForm form = (MyForm)command;
model.put(this.getCommandName(), command);
return new ModelAndView("myTestView", model);
}
MyFormValidator :
Code:
public class MyFormValidator implements Serializable, Validator{
public boolean supports(Class clazz) {
return MyForm.class.isAssignableFrom(clazz);
}
public void validate(Object obj, Errors errors){
MyForm form = (MyForm)obj;
}
}