My UserController.java
@Controller
@RequestMapping("/userRegistration")
public class UserController{
@RequestMapping(method = RequestMethod.GET)
public String showForm(ModelMap model) {
User user = new User();
model.addAttribute(user);
return "userForm";
}
@RequestMapping(method = RequestMethod.POST)
public String onFormSubmit(@ModelAttribute("user") User user) { // **#1**
return "redirect:userSuccess.htm"; // **#2**
}
}
My SuccessController.java
@Controller
public class SuccessController {
@RequestMapping("/userSuccess.htm")
public String getSuccessPage(@ModelAttribute("user") User user){ // **#3**
return "userSuccess";
}
}
I am not getting "user"
model in SuccessController
and so when I use ${user.name}
in userSuccess.jsp
, I don't get any value.
When I set user model at Line 1, why don't I get this value in another controller? If I keep this model in Session using @SessionAttributes
, I can access it in another controller.
Then what is the scope of models formed using @ModelAttribute
?
It's in the request, unless modified with a @SessionAttributes
as described here. (Delta the other cases described here.) You're doing a redirect--request attributes are lost; it's a new request.
You answered your own question in your text: when you keep it in session, it's available across controllers and redirects, because it's in the session. If you don't keep it in session, it's not.