1

The problem is that I have a spring form and 2 @ModelAttribute params with the same properties in my controller. The 'commandName' parameter of the the form is set to one of my modelAttributes names. I was surprised that the maps the property not only to the model attribute specified with 'commandName', but also to the second one.

I haven't found the exact solution here, except the similar to mine: Spring-form multiple forms with same model atribute name properties

But in my case I can't see any 'strange things', I have one form, one Model attribute to bind this form, and one model attribute to have accsess to controller scoped @SessionAttribute. I've also tried to use form's 'modelAttribute' parameter (Actually I can't see any difference between them), but it didn't help.

My code example:

view.jsp:

<form:form name="form" action="/myAction" method="POST" commandName="model1">
    <form:input path="property"/>
    ....
    <input type="submit" value="Submit"/>
</form:form>

Controller.java

@SessionAttributes("model2")
class Controller {
    @RequestMapping(value = "/myAction", method = POST)
    public String submitEditSite(final @ModelAttribute(value = "model1") Model1 model1,
                                 final @ModelAttribute(value = "model2") Model2 model2) {
        ....
        return "redirect:/home";
    }
}

Model1.java Model2.java

class Model1 {
    private String property;
}
class Model2 {
    private String property;
}

Where am I wrong?

1 Answer 1

3

If I understand you correctly you want to prevent the setting of any property on model2, right? Then this should do:

  @InitBinder("model2")
  public void initBinder(WebDataBinder binder) {
    binder.setDisallowedFields("*");
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.