Reputation: 3015
I use Spring MVC. I need to add attribute to request or other object. It should be message that will display on screen. For example, if I use pure Servlets I may just:
request.setAttribute("message", "User deleted");
and than on JSP page
<div id="message">${message}</div>
but when I try to do something like this in method:
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String deleteUser(@RequestParam("login") String login,
ModelMap map, HttpServletRequest request)
Model object -
model.addAttribute("message", "User deleted");
Map -
map.put("message", "User deleted");
ModelMap -
map.put("message", "User deleted");
HttpServletRequest -
request.setAttribute("message", "User deleted");
nothing displays. But in my browser I see: http:// localhost : 8081 /project/index?message=User+deleted
How to solve this little problem? Thanks for your answers
Updated:
for clear understanding I try to do this:
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String deleteUser(@RequestParam("login") String login,
Model model) {
dao.delete(login); // there is NO exeptions
map.addAttribute("message", "User " + login + " deleted");
return "redirect:" + "index";
}
in my JSP I also display user login this way:
${user.login}
it takes user from Session and I see it login
Upvotes: 17
Views: 77177
Reputation: 1875
You are redirecting it in a wrong way. instead of return "redirect:"+"index" use return "redirect:/index". redirect it to your get method. as redirectattributes are post/redirect/get attribute. try this and you will get a flash message on your screen. instead of Model use Redirectattributes.
redirectAttributes.addFlashAttribute("errormsg", "errormessage"); return "redirect:/index.do";
Upvotes: 0
Reputation: 10632
As you are redirecting
to a new URL, browser is actually sending a new request to the redirect URL
. And the request attribute map.addAttribute("message", "User " + login + " deleted");
is not present in the new request.
You can use RedirectAttributes
to show the message
to the user:
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String deleteUser(@RequestParam("login") String login,
Model model,RedirectAttributes redirectAttributes) {
dao.delete(login); // there is NO exeptions
//map.addAttribute("message", "User " + login + " deleted");
redirectAttributes.addFlashAttribute("message", "User " + login + " deleted");
return "redirect:" + "index";
}
redirectAttributes.addAttribute
constructs request parameters out of your attributes and redirects to the desired page with the request parameters. And addFlashAttribute
stores the attributes in a flashmap (maintained in the users session and removed once the next redirected request gets fulfilled).
Upvotes: 8
Reputation: 279970
With your new information, the problem is redirect:
. When you do a redirect, you send an HTTP response with a 302 (or 301) response code with a Location
header pointing to the new url. The browser will make a new HTTP request to that location. As such, your request attributes (and model attributes) are no longer good, they don't exist in the new request.
Consider use flash attributes. The RedirectAttributes
class is the way to go. The javadoc has a good example.
A Model
attribute is added to the request attributes much later during request processing. You therefore won't see it directly doing this
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String deleteUser(@RequestParam("login") String login,
ModelMap map, HttpServletRequest request)
map.put("message", "User deleted");
String message = (String) request.getAttribute("message"); // will return null
...
}
Just trust that it will eventually be in the request attributes and therefore available in your jsp.
Upvotes: 20