Announcement Announcement Module
Collapse
No announcement yet.
Help binding field to Date object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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

    Code:
    package edu.unlv.cs.rebelhotel.form;
    @RooJavaBean
    @RooToString
    public class FormWorkEffortQuery {
    
    	private String userId;
    	private boolean userIdSelected;
    	
        private String employerName;
        private boolean employerNameSelected;
        
        private String employerLocation;
        private boolean employerLocationSelected;
    
        @Enumerated
        private Validation validation;
        private boolean validationSelected;
        
        @Temporal(TemporalType.TIMESTAMP)
        @DateTimeFormat(style = "S-")
        private Date startDate;
    
        @Temporal(TemporalType.TIMESTAMP)
        @DateTimeFormat(style = "S-")
        private Date endDate;
        
        private boolean datesSelected;
        
    }
    Here is my view....
    Code:
    <?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="http://java.sun.com/JSP/Page" version="2.0">
        <jsp:directive.page 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=">
        	
        	
        	
       <table>
        	<tr>
        	 	<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>
            </tr>
           	<tr>
           		<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>
          	</tr>
          	<tr>
          	 	<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>
            </tr>
           <tr>
           		<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>
            </tr>
            <tr>
            	<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>
            </tr>
            </table>
       </form:create>
     
    </div>



    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

    Code:
    	/**
     * A central place to register application Converters and Formatters.
     */
    @RooConversionService
    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
    					e.printStackTrace();
    				}
    				
    				// TODO Auto-generated method stub
    				return date;
    			}
    		};
    	}
    }
    
    	@Override
    	protected void installFormatters(FormatterRegistry registry) {
    
    super.installFormatters(registry);
    		// Register application converters and formatters
    registry.addConverter(getStringConverter());
    
    }
    Last edited by frnkydz; Mar 18th, 2011, 06:43 PM.
Working...
X