Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@DateTimeFormat causes exception when used with Direct Field Access bean binding [SPR-7809] #12465

Closed
spring-projects-issues opened this issue Dec 10, 2010 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

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:

Referenced from: commits d3d7634

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

As far as I could reproduce the issue, this has been fixed since Spring 3.0.2 already... If an issue remains with a newer Spring version, let me know.

Juergen

@spring-projects-issues spring-projects-issues added type: bug A general bug in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0.2 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants