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
Model , ModelMap , and ModelAndView are used to define a model in a Spring MVC application. Model defines a holder for model attributes and is primarily designed for adding attributes to the model. ModelMap is an extension of Model with the ability to store attributes in a map and chain method calls.
Method level ModelAttribute annotation cannot be mapped directly with any request. Let's take a look at the following example for better understanding. We have 2 different methods in the above example.
To add new custom attributes to an entity, from the Edit Business Entity page, expand the Data Model section by clicking on it, then click the Insert button. The Add Attribute page appears where you will provide the properties.
5. ModelAndView. The final interface to pass values to a view is the ModelAndView.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With