thisisananth
thisisananth

Reputation: 398

Spring MVC customdateEditor not working

I have a form which has a date. I registered a customDateEditor for converting the user input String to date.

public class CategoryExclusionFormController extends SimpleFormController {
  private ModelMap modelMap = new ModelMap();

protected final Log logger = LogFactory.getLog(getClass());

private ExclusionDAO exclusionDAO;

public ExclusionDAO getExclusionDAO() {
    return exclusionDAO;
}

public void setExclusionDAO(ExclusionDAO exclusionDAO) {
    this.exclusionDAO = exclusionDAO;
}

public ModelAndView onSubmit(Object command)
        throws ServletException {
    return new ModelAndView(new RedirectView(getSuccessView()));
}

protected Object formBackingObject(HttpServletRequest request) throws ServletException {
        CatExclCriteria catExcl = new CatExclCriteria();
        return catExcl;
}

@Override
protected Map referenceData(HttpServletRequest request) throws Exception {
    modelMap.put("categories", exclusionDAO.getCategories());
    modelMap.put("statuses", exclusionDAO.getStatuses());
    return modelMap;
}

@InitBinder
public void initBinder(WebDataBinder binder) {
    DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class,"endDate", new CustomDateEditor(
            dateFormat, false));

}
}

My jsp has Enter the start date in MM-dd-YYYY format

 <form:input path="endDate" />


 </form:form>

My appname-servlet.xml has

  <bean name="/categoryExcl.htm" class="com.lsr.CategoryExclusionFormController">
    <property name="sessionForm" value="true"/>
    <property name="commandName" value="catExcl"/>
    <property name="commandClass" value="com.lsr.model.CatExclCriteria"/>
    <property name="formView" value="categoryExclusion"/>
    <property name="successView" value="home.htm"/>
    <property name="exclusionDAO" ref="exclusionDAO"/>
   </bean>

When I enter the date and hit submit, I am getting the following error

 Field error in object 'catExcl' on field 'endDate': rejected value [05-12-2011];
 codes [typeMismatch.catExcl.endDate,typeMismatch.endDate,typeMismatch.java.util.Date,typeMismatch];
 arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes
 [catExcl.endDate,endDate]; arguments []; default message [endDate]];
 default message   
 [Failed to convert property value of type [java.lang.String] to required type
 [java.util.Date] for property 'endDate'; nested exception is
 java.lang.IllegalArgumentException: 
 Cannot convert value of type [java.lang.String] to required type [java.util.Date] for
 property 'endDate': no matching editors or conversion strategy found],

I am not sure why it is not using the customDateEditor. I've seen answers to the related questions but I couldn't find the error.

Upvotes: 0

Views: 2970

Answers (1)

abalogh
abalogh

Reputation: 8281

Shot in the dark; try simply overriding the initBinder() method:

@Override
public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
    DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class,"endDate", new CustomDateEditor(
            dateFormat, false));
}

Upvotes: 4

Related Questions