2010-07-02 50 views
4

我一直在嘗試新的Spring MVC 3.0類型轉換框架。我找不到如何捕捉轉換錯誤。Spring 3類型轉換錯誤

我現在用的是新的MVC架構:

<?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"> 

    <!-- 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> 

和一個簡單的命令類:

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

} 

和同樣簡單的控制器:

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

這工作得很好 - 我如果我爲BigDecimal輸入負值,則會在result.jsp中獲取驗證錯誤。但是,如果我嘗試發送日期如2010-07-024,這不符合#### - ####,我得到一個錯誤:

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) 

這很好 - 但如何我是否會陷入錯誤?我期待BindingResult只包含另一個錯誤?

回答

0

根據克林斯曼Hoeller,這是固定在Spring 3.0.2

2

看看是否合適註冊DateEditor可以幫助你..... 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)); 
    } 
+1

這無疑使錯誤消失 - 但我認爲Spring 3的重點在於Property Editors是'舊帽子',並且新的ConverterFramework(反過來使用JodaTime)應該爲你做這件事,憑藉@DateTimeFormat(iso = ISO.DATE)註釋? – 2010-07-02 15:58:43