I have a page which submits some data. The submited fields include an ID parameter.
<form:form modelAttribute="command" action="info.html">
<form:input path="id"/>
...
</form:form>
My comand object is a POJO with such an id field:
public class MyCommand {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
....
}
This is annotated in the controller like this:
@ModelAttribute("command")
public MyCommand initializeCommand() {
return new MyCommand(...);
}
While my handler method looks something like this:
public void handle(@ModelAttribute("command") MyCommand cmd, ...)
When I submit the form, Spring binds the parameters to the command object. But it also binds the parameters to every object found in the model (to all model attributes) that has an id property. For example, a bean like:
public class FooBar {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
....
}
set up like:
@ModelAttribute("fooBar")
public FooBar initializeFooBar() {
return new FooBar(...);
}
When in my handler method, which I modify like the following, the binding occurs on both model attributes (cmd and fooBar):
public void handle(@ModelAttribute("command") MyCommand cmd,
@ModelAttribute("fooBar") FooBar fooBar, ...) {
// when i submit my form the following values are equal:
// fooBar.getId() is the same as cmd.getId()
}
Why is this and how can I stop it?
I want only my command to be binded with the request submited data, not every model that has matching property names with what comes on the request.