model view controller - Migrating to Spring MVC 4 -


we migrating our mvc code spring 4. had a method formbackingobject converted method initform. trouble - in previous controller extending simpleformcontroller, formbackingobject getting called before submit method. have removed simpleformcontroller. initform getting called only once on page load. doesn't called before submit. , there custom logic of creating user object , adding userprofileform object.

have faced similar issue.

old code

    protected object formbackingobject(httpservletrequest request) throws exception {            final userprofileform userprofileform = new userprofileform();     final string id = request.getparameter("id");     if (id != null && !id.trim().equals("")) {         final user user = authenticationservices.finduser(servletrequestutils.getlongparameter(request, "id"));         userprofileform.setuser(user);     } else {         final user user = new user();         userprofileform.setuser(user);     }     return userprofileform; } 

new code

@requestmapping(method = requestmethod.get) public string initform(httpservletrequest request, modelmap model) throws exception{     final userprofileform userprofileform = new userprofileform();     final string id = request.getparameter("id");     if (id != null && !id.trim().equals("")) {         final user user = authenticationservices.finduser(servletrequestutils.getlongparameter(request, "id"));         userprofileform.setuser(user);     } else {         final user user = new user();         userprofileform.setuser(user);     }     addtomodel(request, model);     model.addattribute("userprofileform", userprofileform);     return "user-management/user-profile"; } 

create method annotated @modelattribute fill model.

@modelattribute("userprofileform"); public userprofileform formbackingobject(@requestparam(value="id", required=false) long id) throws exception{     final userprofileform userprofileform = new userprofileform();     if (id != null) {         final user user = authenticationservices.finduser(id);         userprofileform.setuser(user);     } else {         final user user = new user();         userprofileform.setuser(user);     }     return userprofileform;  }  @requestmapping(method = requestmethod.get) public string initform() {     return "user-management/user-profile"; } 

this way can use @requestparam annotation instead of pulling out parameters yourself.

see the reference guide more information on subject.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -