Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Dysfonction or help how to use CustomDateEditor

  1. #1
    Join Date
    Dec 2006
    Posts
    5

    Default Dysfonction or help how to use CustomDateEditor

    Goal:
    I would like to show a Date field in my jsp, this date field must be initialise at today.The format date have to respect the pattern: dd/MM/yyyy.

    formBackingObject initilisation :
    Code:
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
    		StateIndicatorForm form = new StateIndicatorForm();		
                    form.setBeginDay(new Date());
    		return(form);
    	}

    Date pattern format respect:

    Code:
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    	// Récupération du pattern date et création du SimpleDateFormat
    	SimpleDateFormat sdf = new SimpleDateFormat(messageSource.getMessage("pattern.date.yearMonthDay")); 
    	sdf.setLenient(false);
    	// Convertir en date si pattern correct et 10 caractères exactement
    	// Autoriser une date vide ou null.
    	binder.registerCustomEditor(Date.class,new CustomDateEditor(sdf,true,10));
    	
    	// The field must be required
    	String[] requiredFields={"beginDay"};
    	binder.setRequiredFields(requiredFields);
    }
    On the JSP :

    Code:
    <spring:bind path="stateIndicatorForm.beginDay">
        <input type="text" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}”/>” />
    </spring:bind>
    Now we see the jsp at screen with the good format: Ex 05/01/2007. The user change field value and submit the form (Command object). (${status.editor} is not empty )

    The probleme is that the pattern isn’t respect at the second time (after passed by the onSubmit method or the Command’s validate method) (${status.editor} is null )
    The date field is : Fri Jan 05 00:00:00 CET 2007 !!!!!!!!!!

    Investigation into the framework seems that the CustomDateEditor (with the pattern) affected the first time in the initbinder method isn’t preserve at the second time. Then the CustomDateEditor is null...

    Regard Jule the monkey

  2. #2
    Join Date
    Aug 2005
    Location
    Boston, United States
    Posts
    45

    Default

    For a form implementing BaseCommandController (for example SimpleFormController), try adding this initBinder:

    Code:
       @Override
        protected void initBinder(HttpServletRequest request,
        		ServletRequestDataBinder binder) {
        	binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"),true));
        }

  3. #3
    Join Date
    Dec 2006
    Posts
    5

    Default

    Hi,

    thanks but I think I already use it...

    I don't see any difference between the both code.
    I forgot to specify in the preview post the bundle ressource key for pattern.date.yearMonthDay: dd/MM/yyyy
    Code:
    SimpleDateFormat sdf = new SimpleDateFormat(messageSource.getMessage("pattern.date.yearMonthDay"));
    Last edited by jim060; Jan 11th, 2007 at 08:35 AM.

  4. #4
    Join Date
    Aug 2005
    Location
    Boston, United States
    Posts
    45

    Default

    Where are you seeing the date formatted as Fri Jan 05 00:00:00 CET 2007? Is this in your successView? What code are you using to display the date?

    does the success view use this code?
    Code:
    <spring:bind path="stateIndicatorForm.beginDay">
        <input type="text" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}”/>” />
    </spring:bind>

  5. #5
    Join Date
    Sep 2006
    Location
    Omaha, NE
    Posts
    60

    Default big dysfunction!!!

    We have the same problem. The only solution we can up with was to write our own DateFormat JSTL Tag.

    Code:
    <spring:bind path="myDate">
    <input tabindex="6" type="text" size="12" name="${status.expression}" value="<myTaglib:fmtDt date="${status.value}"/>">
    </spring:bind>
    We can't get the CustomDateEditor to run after the submit. So...we just take the date and convert it how we want it.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    How does your onSubmit method look like?!
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    Can you post the code of your controller? Depending on how you're overriding/handling validation and onSubmit, this could make sense.

  8. #8
    Join Date
    Dec 2006
    Posts
    5

    Default

    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;
    							
    	}
    }

  9. #9
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Well first of all you construct a model with a new map. You should be using the Model provided by the BindException object.

    You don't want to hardcode view names into your controller. I assume here that you want to show the successView so use getSuccessView() instead.

    Code:
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
    	return new ModelAndView("myTestView", errors.getModel());
    }
    Last edited by Marten Deinum; Jan 12th, 2007 at 07:39 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  10. #10
    Join Date
    Nov 2006
    Location
    The Netherlands
    Posts
    33

    Default

    Small question, why don't you use the JSTL fmt taglib for formatting the date?

    Code:
    <fmt:formatDate value="${scope.object.yourVar}" pattern="dd/MM/yyyy" />
    And ehmmm, I don't think I get the problem, the first time you go to the page the date is displayed correctly, then you post the form with it (mind that you're setting the received date/time from the form to the submitted date and 00:00:00 hours, which is normal behavior).
    Then you go back to the page and the date doesn't get rendered correct anymore?
    Or is the time incorrect in your controller?
    Barre Dijkstra

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •