Announcement Announcement Module
No announcement yet.
Help binding field to Date object Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help binding field to Date object

    I'm used spring roo to create my project. In it I have created this form backing object

    package edu.unlv.cs.rebelhotel.form;
    public class FormWorkEffortQuery {
    	private String userId;
    	private boolean userIdSelected;
        private String employerName;
        private boolean employerNameSelected;
        private String employerLocation;
        private boolean employerLocationSelected;
        private Validation validation;
        private boolean validationSelected;
        @DateTimeFormat(style = "S-")
        private Date startDate;
        @DateTimeFormat(style = "S-")
        private Date endDate;
        private boolean datesSelected;
    Here is my view....
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <div xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="" version="2.0">
        < contentType="text/html;charset=UTF-8"/>
        <jsp:output omit-xml-declaration="yes"/>
     <form:create id="fc_edu_unlv_cs_rebelhotel_form_FormWorkEffort" modelAttribute="formworkeffortquery" path="/workeffortquery?query" render="${empty dependencies}" z="Uf7OgrlFmtWkcuJmcJOSqG30iWM=">
        	 	<td><field:input field="userId" id="c_edu_unlv_cs_rebelhotel_form_FormWorkEffort_NSHE"  z=""/></td>
            	<td><field:checkbox field="userIdSelected" id="c_edu_unlv_cs_rebelhotel_form_FormWorkEffortQuery_useUserId" /></td>
           		<td><field:input field="employerName" id="c_edu_unlv_cs_rebelhotel_form_FormWorkEffort_employerName" z=""/></td>
       			<td><field:checkbox field="employerNameSelected" id="c_edu_unlv_cs_rebelhotel_form_FormWorkEffortQuery_useEmployerName" /></td>
          	 	<td><field:input field="employerLocation" id="c_edu_unlv_cs_rebelhotel_form_FormWorkEffort_employerLocation" z=""/></td>
           		<td><field:checkbox field="employerLocationSelected" id="c_edu_unlv_cs_rebelhotel_form_FormWorkEffortQuery_useemployerLocation" /></td>
           		<td> <field:select field="validation" id="c_edu_unlv_cs_rebelhotel_form_FormWorkEffort_validation" items="${validations}" path="validations" z=""/></td>
           		<td><field:checkbox field="validationSelected" id="c_edu_unlv_cs_rebelhotel_form_FormWorkEffortQuery_useValidation" /></td>
            	<td><field:datetime dateTimePattern="${formWorkEffort_startdate_date_format}" field="startDate" id="c_edu_unlv_cs_rebelhotel_form_FormWorkEffort_startDate" z=""/>
            	<field:datetime dateTimePattern="${formWorkEffort_enddate_date_format}" field="endDate" id="c_edu_unlv_cs_rebelhotel_form_FormWorkEffort_endDate" z=""/></td>
            	<td><field:checkbox field="datesSelected" id="c_edu_unlv_cs_rebelhotel_form_FormWorkEffortQuery_useDates" /></td>

    Everything works fine except that whenever i try to enter a date into the endDate and startDate binding fails as i get this error:

    [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endDate'.

    I believed that spring mvc had default converters for dates.
    I've been able to create a binder using propertyeditors for the date fields but that seems to take override the default type conversion for the other fields.
    My question is how can i do the type conversion using the FormattingConversionServiceFatctorybean since I read that this is the direction that I should go.

    I already tried to put a custom converter but it didn't seem to make a difference

     * A central place to register application Converters and Formatters.
    public class ApplicationConversionServiceFactoryBean extends
    		FormattingConversionServiceFactoryBean {
    	Converter<String, java.util.Date> getStringConverter() {
    		return new Converter<String, java.util.Date>() {
    			public Date convert(String source) {
    				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    				java.util.Date date = new java.util.Date();
    				try {
    					date = sdf.parse(source);
    				} catch (ParseException e) {
    					// TODO Auto-generated catch block
    				// TODO Auto-generated method stub
    				return date;
    	protected void installFormatters(FormatterRegistry registry) {
    		// Register application converters and formatters
    Last edited by frnkydz; Mar 18th, 2011, 06:43 PM.