Skip to content

Allow null as modelAttribute on form:form binding [SPR-5867] #10536

@spring-projects-issues

Description

@spring-projects-issues

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:

  1. Using a separate method with @ModelAttribute on 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.
  2. 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:

6 votes, 6 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions