I am new in Spring and registering a user.I did like this.
@RequestMapping("/register")
public String register(@ModelAttribute User user,BindingResult result){
if(!result.hasErrors()){
userSerive.register(user);
}
return "welcome";
}
This worked fine,but problem here is I don't need this user
object in my welcome.jsp
page,so why make model object heavier.So I tried without @ModelAttribute
, this also works for me like below.
@RequestMapping("/register")
public String register(User user,BindingResult result){
if(!result.hasErrors()){
userSerive.register(user);
}
return "welcome";
}
So I just want to know what are pros & cons of both and which is the best practice if I really don't need user
object in jsp
. Is @ModelAttribute
does any other thing apart from adding object to Model,which spring implicit binding not does.Is @ModelAttribute
safer way of binding or else?
I want to categories my query in following 4 type of request.what would be difference with and without @ModelAttribute
if I need not to send data in view and my request is any of-
- query string ie form data in GET
- request payload or body ie form data in POST
- json data in ajaxified GET requst
- json data in POST requst- I guess this would not be bind in any of both.
@RequestBody
is required.