spring - Am I using @ModelAttribute wrong in my Controller? -
for years have been using @modelattribute create , initialize command object so:
@requestmapping() public string somehandler(@modelattribute("formbean") formbean formbean) { // } @modelattribute("formbean") public formbean createformbean() { formbean formbean = new formbean(); // sort of initialization return formbean; }
in example, have handler in controller needs formbean, , "create" method gives 1 if 1 isn't in model (or session, if using @sessionattributes). so, when somehandler() method ran, formbean there , populated because createformbean() had ran.
however, colleague claiming that, although works fine, misusing @modelattribute purpose wasn't intended for, namely in creation of command object. in interpretation the javadoc, should use @modelattribute create static data, items used populate dropdown list or such.
i know works creating , initializing command object quite well, using purpose not intended for? breaking cardinal rule here?
@modelattribute("formbean") public formbean createformbean() { formbean formbean = new formbean(); // sort of initialization return formbean; }
this can useful if need initialize model attribute before binding form values view. example, can query object database (to available in current session).
in other cases prefer use method:
@requestmapping public string somehandler(final model model) { formbean formbean = new formbean(); // sort of initialization model.addattribute("formbean", formbean); }
i think more clear understand. don't think "breaking cardinal rule here".
Comments
Post a Comment