17

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

4
  • How are you using the request attributes? In a JSP? Commented Sep 24, 2013 at 14:14
  • What is PATH_REDIRECT's value? Commented Sep 24, 2013 at 14:24
  • it is "redirect:". I think that problem is in this redirection. So how I can redirect and send message? Commented Sep 24, 2013 at 14:29
  • Yes, that changes things. See the answers below. Commented Sep 24, 2013 at 14:32

3 Answers 3

20

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes!! Thats it! i use attr.addFlashAttribute("message", "User deleted") of RedirectAttributes object. Thanks
I have a question. How I can do the same in pure Servlets? (without any Spring)
@OleksandrHubachov The underlying implementation uses HttpSession attributes. With pure Servlets you could use a Filter to add and remove from the HttpSession attributes on each request. There's an example here.
8

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).

6 Comments

unfortunately, this does not work... I do not know why. As well as last time, I see in browser http:// localhost : 8081 /project/index?message=User+ololo+deleted
try this : <c:out value="${message}" />
@DebojitSaikia You need to use addFlashAttribute.
Thanks for your answer. But in my case I need to call addFlashAttribute method of RedirectAttributes class
@DebojitSaikia ya it helped, but what about if I want now to delete this flashAttribute? how do I delete it? I added null on this FlashAttribute... it works but it's kinda nasty
|
0

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";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.