java - Spring 3 wrongly binding request data on all model attributes -



java - Spring 3 wrongly binding request data on all model attributes -

i have page submits data. submited fields include id parameter.

<form:form modelattribute="command" action="info.html"> <form:input path="id"/> ... </form:form>

my comand object pojo such id field:

public class mycommand { private integer id; public integer getid() { homecoming id; } public void setid(integer id) { this.id = id; } .... }

this annotated in controller this:

@modelattribute("command") public mycommand initializecommand() { homecoming new mycommand(...); }

while handler method looks this:

public void handle(@modelattribute("command") mycommand cmd, ...)

when submit form, spring binds parameters command object. binds parameters every object found in model (to model attributes) has id property. example, bean like:

public class foobar { private integer id; public integer getid() { homecoming id; } public void setid(integer id) { this.id = id; } .... }

set like:

@modelattribute("foobar") public foobar initializefoobar() { homecoming new foobar(...); }

when in handler method, modify following, binding occurs on both model attributes (cmd , foobar):

public void handle(@modelattribute("command") mycommand cmd, @modelattribute("foobar") foobar foobar, ...) { // when submit form next values equal: // foobar.getid() same cmd.getid() }

why , how can stop it?

i want command binded request submited data, not every model has matching property names comes on request.

you seem mixing 2 distinct use-cases of @modelattribute annotation.

when it's used annotate parameters of @requestmapping annotated method, annotation bind request parameters annotated method argument (in case, bind applicable request parameters both objects).

on other hand, if want expose info view model attribute, it's plenty annotate accessor method of foobar instance @modelattribute, did. need not (and shouldn't) include foobar among request handling method parameters, type of @modelattribute refers incoming , not outgoing model attributes.

the javadoc @modelattribute makes distinction quite clear:

can used expose command objects web view, using specific attribute names, through annotating corresponding parameters of requestmapping annotated handler method).

can used expose reference info web view through annotating accessor methods in controller class based on requestmapping annotated handler methods, such accessor methods allowed have arguments requestmapping supports handler methods, returning model attribute value expose.

java spring data-binding spring-3

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -