java - Spring - @ModelAttribute behaviour -
i'm having problem @modelattribute usage.
having simple spring (3.2.11) mvc application such context:
<mvc:annotation-driven /> <context:component-scan base-package="spring.test" /> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/views/"></property> <property name="suffix" value=".jsp"></property> </bean>
having 2 dtos: pagedto , sessiondto, within these 2 dtos same, containing 1 property - address address , get/set methods.
package spring.test.model; public class pagedto implements java.io.serializable { private address address; // getters / setters omitted } bundle spring.test.model; public class sessiondto implements java.io.serializable { private address address; // getters / setters omitted } bundle spring.test.model; public class address implements java.io.serializable { private string street; private string houseno; private string city; private string zip; // getters/setters omitted }
having addressform.jsp 1 form:
<c:url var="actionurl" value="/processform"/> <form:form method="post" modelattribute="pagedto" action="${actionurl}"> <form:label path="address.street">street: </form:label> <form:input path="address.street"/> <form:label path="address.houseno">house no: </form:label> <form:input path="address.houseno"/> <form:label path="address.city">city: </form:label> <form:input path="address.city"/> <form:label path="address.zip">zip: </form:label> <form:input path="address.zip"/> <input type="submit" name="searchaddress" value="submit" /> </form:form>
having controller:
package spring.test.controller; // imports omitted @sessionattributes("sessiondto") @controller public class formcontroller { @initbinder protected void initbinder(webdatabinder binder) { binder.registercustomeditor(string.class, new stringtrimmereditor(true)); } @modelattribute("pagedto") public pagedto initpagedto() { homecoming new pagedto(); } @modelattribute("sessiondto") public sessiondto initsessiondto() { homecoming new sessiondto(); } @requestmapping(value = "/", method = requestmethod.get) public modelandview renderhome(@modelattribute("pagedto") pagedto pagedto, @modelattribute("sessiondto") sessiondto sessiondto) { homecoming new modelandview("addressform", "pagedto", pagedto); } @requestmapping(value = "/processform", method = requestmethod.post) public modelandview processform(@modelattribute("pagedto") pagedto pagedto, @modelattribute("sessiondto") sessiondto sessiondto) { if(pagedto != null && pagedto.getaddress() != null && sessiondto != null && sessiondto.getaddress() != null) { system.out.println("pagedto.getaddress().gethouseno(): " + pagedto.getaddress().gethouseno()); system.out.println("sessiondto.getaddress().gethouseno(): " + sessiondto.getaddress().gethouseno()); } homecoming new modelandview("addressform", "pagedto", pagedto); } }
and here comes problem. when submit address form, both of dtos filled in info form. expect model attribute "pagedto" name mentioned in form tag in jsp filled in.
log entry form-submit event:
[10/24/14 11:23:09:382 cest] 0000002b systemout o pagedto.getaddress().gethouseno(): 41 [10/24/14 11:23:09:382 cest] 0000002b systemout o sessiondto.getaddress().gethouseno(): 41
when alter processform method in controller this:
@requestmapping(value = "/processform", method = requestmethod.post) public modelandview processform(@modelattribute("pagedto") pagedto pagedto, httpsession httpsession) { sessiondto sessiondto = (sessiondto) httpsession.getattribute("sessiondto"); if(pagedto != null && pagedto.getaddress() != null && sessiondto != null && sessiondto.getaddress() != null) { system.out.println("pagedto.getaddress().gethouseno(): " + pagedto.getaddress().gethouseno()); system.out.println("sessiondto.getaddress().gethouseno(): " + sessiondto.getaddress().gethouseno()); } homecoming new modelandview("addressform", "pagedto", pagedto); }
...it works desired - sessiondto not filled in info form, no entry appears in log (sessiondto.getaddress() == null).
motivation have 2 dtos, 1 stored in session:
not properties of model displayed on form, have utilize input type=hidden not loose data i need able find out properties changed user when submitting formany ideas appreciated!
at submit time, name of modelattribute
has cosmetic effect. request parameters consist in hash keys paths of <form:input>
elements sent previous response (*), , values comes user typed in form. name of modelattribute
not in request , spring has no way guess it.
it takes @modelattribute
parameters of method , tries set attributes corresponding keys of request parameters, , when it's done, puts them in model
with name supplied.
so name used prepare response , not parse request.
(*) if request comes submitting form, produced other way - spring mvc expects request parameters next convention
java spring spring-mvc modelattribute
No comments:
Post a Comment