java - @ModelAttribute in a method -
imagine code one:
@requestmapping(value="/users", method=requestmethod.get) public string list(model model) { ... } @initbinder("user") public void initbinder(webdatabinder binder) { binder.setdisallowedfields("password"); // don't allow user override value } @modelattribute("user") public user prepareuser(@requestparam("username") string username){ ... } @requestmapping(value="/user/save", method=requestmethod.post) public string save(@modelattribute("user") user user, model model) { ... }
i use init binder avoid field can binded , mark method (prepareuser()) @modelattribute prepare user object before binded. when invoke /user/save initbinder() , prepareuser() executed.
i have set "user" in both @initbinder , @modelattribute spring-mvc understand methods should applied before executing method @modelattribute("user").
the problem method annotated @modelattribute("user") executed before every mapped method of controller. example if invoke /users prepareuser executed before list() method. how can make preparer executed before save() method having methods in same controller?
thanks
that's not @modelattribute
for. if use method parameter, puts annotated parameter model (that's fine). if put on method, it's called every time provide reference data every method in controller should have access to.
if want take control of building user object, have several options. 2 obvious me are:
- use initbinder method add new custom editor (a propertyeditor class) building user objects,
- use conversion service in spring 3 convert string usernames user objects.
Comments
Post a Comment