Spring MVC keep value of unexposed property model -
i have model class:
public class user{ public string title; public string username; /* setter getter here */ public string tostring(){ /* output title , username */ } }
controller:
@requestmapping("/dummy") public class dummycontroller { private log = logger.getlogger(this.getclass().getname()); @requestmapping(value="binder") public string binderinput(model model){ userlogin userinit=userlogin.finduserlogin(1l); model.addattribute("userlogin",userinit); return "binderinput"; } @requestmapping(value="binderresult") public string binderresult(@modelattribute("userlogin") userlogin userlogin,bindingresult br){ log.debug("userlogin:"+userlogin); return "binderresult"; } }
and views (jspx) binderinput:
<form:form action="dummy/binderresult" method="post" modelattribute="userlogin"> title:<form:input path="title" /> <input type="submit"/> </form:form>
the dao in userlogin.finduserlogin(1l);
return user title 'mr' , username 'smith'. browse localhost:8080/myweb/dummy
, views showed 'mr' in title field, , update title 'sir' , submit form.
in binderresult expect see
title:sir, username:smith
but got
title:sir, username:null
is there way keep property value(s) dao not edited (for field(s) not showed in jspx view) ?
i think of hidden field, username can viewed when looking @ html source , can manipulated think cant solution.
i have tried
@sessionattribute
, thought make sense model keeped in session , springmodelattribute
fill properties received views session model. still same, title change sir username null.i read
@initbinder
, try
databinder.setallowedfields(new string[]{"title"});
but not help, filters properties , not keep value of other not allowed fields
i start think have manually this:
@requestmapping(value="binderresult") public string binderresult(@modelattribute("userlogin") userlogin userlogin,bindingresult br,httpsession session){ /* after put dao model in session in binderinput */ userlogin userloginsession = session.getattribute("userloginsession"); userloginsession.setusername(userlogin.getusername()); //further process or save userloginsession log.debug("userlogin:"+userloginsession); return "dummy/binderresult"; }
there way keep not edited property in spring expect in first place?
help!
=====================================================
code 2nd point, asked gouki:
@requestmapping("/dummy") @sessionattributes({"usersession"}) public class dummy2controller { private log = logger.getlogger(this.getclass().getname()); @requestmapping(value="binder") public string binderinput(model model,httpsession session){ userlogin userinit=userlogin.finduserlogin(1l); model.addattribute("usersession",userinit); return "binderinput"; } @requestmapping(value="binderresult") public string binderresult(@modelattribute userlogin usersession,bindingresult br,httpsession session){ log.debug("usersession:"+usersession); //further process or save usersession db return "binderresult"; } }
and change modelattribute usersession in views:
<form:form action="dummy/binderresult" method="post" modelattribute="usersession">
=====================================================
problem solved
link read related accepted answer in question:
you can add method, populate object before invocation of every controller method:
@modelattribute("userlogin") public userlogin getuserlogin() { return userlogin.finduserlogin(1l); }
after loading changed fields submitted form override old values , in controller you'll have refreshed object.
Comments
Post a Comment