spring-mvc - Spring 3 类型转换错误

标签 spring-mvc type-conversion

我一直在尝试新的 Spring MVC 3.0 类型转换框架。我不知道如何捕获转换错误。

我正在使用新的 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 :
@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 只包含另一个错误?

最佳答案

看看注册合适的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));
    }

关于spring-mvc - Spring 3 类型转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3164758/

相关文章:

java - 通过 webjar 将 Bootstrap 附加到 Spring MVC 项目

python - 为什么 Python 的 UUID 构造函数会产生与 C# Guid 构造函数不同的结果?

c# - 从通用委托(delegate)函数中获取参数类型

c++ - 是否有一种内置方法可以转换为不同的基础类型但保留 const 限定符?

json - Spring - 返回原始 JSON,无需双重序列化

java - Spring MVC @PathVariable 被视为 @RequestParam

java - 每次 servlet 容器启动时我都需要运行的代码块

java - 无法使 bean 配置工作

java - 将日期或日历类型转换为字符串格式

c++ - uint8_t VS uint32_t 不同的行为