- 
                Notifications
    You must be signed in to change notification settings 
- Fork 38.8k
Description
Oliver Drotbohm opened SPR-5867 and commented
I'd really like to see the HTML form library not insisting on providing a non-null bean as backing object. The use case is the following: I want to use the same JSP for handling creation as well as editing an entity. Besides that I do not want to expose a public empty constructor for my entity class due to inforcement of constraints (non null properties etc.). To persist the entity (with JPA) and to bind form data to the entity, a private or public empty constructor is enough for the frameworks to deal with. Now suppose the following controller:
@RequestMapping(..)
public String showForm(@RequestParam(value="id", required=false) Long id, Model model) {
  if (null == id) {
    model.addAttribute("user", new User());
  else
    model.addAttribute("user", userDao.readById(id));
  return "userForm";
}
The JSP the states something like:
<form:form modelAttribute="user"> .. </form:form>
The key thing to notice here is that I am required to put an empty User instance into the model to render the form correctly. Thus I am required to offer an public empty constructor, which leads the loss of any enforcing restrictions to the internals of that class.
I've already considered two options:
- Using a separate method with @ModelAttributeon it to automagically get the empty instance created by Spring on each controller invocation. As the controller is rather used for an entire module than a single entity, this would cause the method unnecessarily being invoked when accessing other entities forms and thus unnecessarily polluting the model.
- I could extend the method signature to public String showForm(@ModelAttribute("user") User user, @RequestParam(value="id", required = false) Long id, Model model)to get the empty User instance created by Spring but the signature then is rather verbose unintuitive and contains somewhat redundant parameters.
What I'd actually like to see is something like this:
public String showForm(@RequestParam(value="id", required=false) Long id, Model model) {
  if (null != id) {
    model.addAttribute("user", userDao.readById(id));
  }
  return "userForm";
}
And either the <form:form /> tag simply accepting the non available bean or maybe explicitly activating this accepting behaviour by adding required="false" to it. Both versions should then render an empty form for the properties given in the form elements. This would not only lead to more concise code but also getting rid of the technology forcing the design of the entity class only for reasons of showing an empty form.
Regards,
Ollie
Issue Links:
- Null property values in nested paths relating to <spring:bind> tag, <form:xxx> tags, and ServletRequestDataBinder [SPR-1860] #6554 Null property values in nested paths relating to spring:bind tag, form:xxx tags, and ServletRequestDataBinder
- <form> tag library should WARN instead of blowup when command missing [SPR-2607] #7296 tag library should WARN instead of blowup when command missing
6 votes, 6 watchers