views:

109

answers:

2

I have been trying out the new Spring MVC 3.0 Type Conversion Framework. I cannot find out how to trap conversion errors.

I am using the new mvc schema:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt;

     <!-- Scan this package and sub-packages for Annotated Controllers -->
     <context:component-scan base-package="springmvc.simple"/>             

     <!-- New Spring 3.0 tag to enable new Converter and Formatting Frameworks -->             
     <mvc:annotation-driven/>

     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/views/" />
      <property name="suffix" value=".jsp" />
     </bean>

    </beans>

And a simple command class:

public class Amount {

 @NumberFormat(style=Style.CURRENCY)
 @Min(0)
 private BigDecimal amount = BigDecimal.valueOf(10000, 2);

 @DateTimeFormat(iso=ISO.DATE)
 private Date date = new Date();

 public Date getDate() {
  return date;
 }

 public BigDecimal getAmount() {
  return amount;
 }

}

And an equally simple controller:

@Controller
@RequestMapping(value="/addVat.html")
public class AddVatController {

 @InitBinder
 public void initBinder(WebDataBinder binder) {
  binder.initDirectFieldAccess();
    }

 @RequestMapping(method = RequestMethod.GET)
 public String setupForm(Model model) {
  model.addAttribute("commandBean", new Amount());
  return "addVatForm";
 }

 @RequestMapping(method = RequestMethod.POST)
 public String onSubmit(@ModelAttribute("commandBean") @Valid Amount commandBean, BindingResult amountBinding, Model model) {

  if (amountBinding.hasErrors()) {
   return "addVatForm";
  }

  BigDecimal result = commandBean.getAmount().multiply(new BigDecimal("1.175"));
  model.addAttribute("result", result);

  return "result";
 }
}

This works fine - I get validation error in my result.jsp if I enter a negative value for the BigDecimal. However if I try to send a Date such as 2010-07-024, which does not conform to ####-##-##, I get an error:

org.springframework.core.convert.ConversionFailedException: Unable to convert value 2010-07-024 from type 'java.lang.String' to type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value 2010-07-024 from type 'java.lang.String' to type 'java.util.Date'; nested exception is java.lang.IllegalArgumentException: Invalid format: "2010-07-024" is malformed at "4"
 org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:40)
 org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:135)
 org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:199)
 org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:169)
 org.springframework.beans.DirectFieldAccessor.setPropertyValue(DirectFieldAccessor.java:125)
 org.springframework.beans.AbstractPropertyAccessor.setPropertyValue(AbstractPropertyAccessor.java:50)
 org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
 org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:665)
 org.springframework.validation.DataBinder.doBind(DataBinder.java:561)
 org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:190)
 org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:110)
 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.doBind(AnnotationMethodHandlerAdapter.java:696)
 org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:744)
 org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:296)
 org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:163)
 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
 org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Which is fine - but how do I trap the error? I was expecting BindingResult to simply contain another error?

A: 

See if registering appropriate DateEditor can help you..... 15.3.2.12

    @InitBinder
   public void initBinder(WebDataBinder binder) {
         binder.initDirectFieldAccess();
         **/* register appropriate date editor */**
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
becomputer06
That certainly makes the error go away - but I thought the whole point of Spring 3 was that Property Editors were 'old hat' and that the new ConverterFramework (which in turn is using JodaTime) should do this for you, by virtue of the @DateTimeFormat(iso=ISO.DATE) annotation?
Forge_7
A: 

I ran into a similar problem, but unfortunately the @InitBinder fix above introduces another problem:

java.lang.IllegalStateException: Cannot access fields on null target instance <variable name>

The variable is a @RequestParam parameter. Apparently, bind.initDirectFieldAccess() tries to access more than is good for it, and fails.. Any ideas how to prevent this?

edit Found the solution myself: @InitBinder allows for specific values to be added to indicate what should be included, like so:

@InitBinder(value={"myField1","myField4"})
MagiTec