Closed
Description
Barry Wilks opened SPR-7809 and commented
Using @DateTimeFormat
annotation with direct field access causes unhandled exception.
Keith Donald suggested trying without direct field access (i.e. using bean properties), and this fixes the problem.
The code to re-produce the issue is shown below. This can also be seen in the forum.
Attached file has complete eclipse web project example, without libraries.
Using 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">
<!-- 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>
With 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 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";
}
}
Affects: 3.0 GA
Reference URL: http://forum.springsource.org/showthread.php?p=333754#post333754
Attachments:
- test.zip (10.58 kB)
Referenced from: commits d3d7634