Tuesday, 15 July 2014

java - Exclude Spring Controller method from model binding through annotated @ModelAttribute method -



java - Exclude Spring Controller method from model binding through annotated @ModelAttribute method -

i'm getting invalid request method getindex because request parameter id missing. can specify don't want model binding requestmapping method?

@controller @requestmapping ("/admin/admins") public class adminusercontroller { @requestmapping (method = requestmethod.get) public string getindex(modelmap model) { model.addattribute("admins",userservice.findalladmins()); homecoming "admin/admins/list"; } @modelattribute("user") public adminuser getadminuser(@requestparam("id") integer id) { homecoming userservice.findadminbyid(id); } @requestmapping (method = requestmethod.post) public string registeradmin(@valid @modelattribute("user") adminuser user, bindingresult bindingresult, modelmap model) { model.addattribute("roles", userroleservice.findall()); if (bindingresult.haserrors()) { homecoming "admin/admins/form"; } else if (!user.getpassword().equals(user.getconfirmpassword())) { bindingresult.adderror(new fielderror("user","confirmpassword", "passwords don't match")); homecoming "admin/admins/form"; } else { user.setpassword(passwordencoder.encodepassword(user.getpassword(), null)); seek { userservice.save(user); homecoming "redirect:/admin/admins"; } catch(applicationexception ce) { bindingresult.adderror(new fielderror("user", "email", "email registered")); homecoming "admin/admins/form"; } } }

you create id parameter optional

@modelattribute("user") public adminuser getadminuser(@requestparam(value="id", required=false) integer id) { if(id==null){ homecoming new adminuser(); } homecoming userservice.findadminbyid(id); }

edit: or loose modelmap paramter:

@requestmapping (method = requestmethod.get) public modelandview getindex() { homecoming new modelandview("admin/admins/list", "admins",userservice.findalladmins()); }

java spring spring-mvc

No comments:

Post a Comment