Java Spring MVC 将属性设置为 request/model/modelMap
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18983935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC set attribute to request/model/modelMap
提问by Oleksandr H
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:
我使用 Spring MVC。我需要向请求或其他对象添加属性。它应该是将显示在屏幕上的消息。例如,如果我使用纯 Servlets,我可能只是:
request.setAttribute("message", "User deleted");
and than on JSP page
而不是在 JSP 页面上
<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 -
HttpServletRequest -
request.setAttribute("message", "User deleted");
nothing displays. But in my browser I see: http:// localhost : 8081 /project/index?message=User+deleted
没有显示。但在我的浏览器中,我看到: 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:
在我的 JSP 中,我也以这种方式显示用户登录信息:
${user.login}
it takes user from Session and I see it login
它从会话中获取用户,我看到它登录
采纳答案by Sotirios Delimanolis
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 newrequest.
有了你的新信息,问题是redirect:
。当您进行重定向时,您会发送一个带有 302(或 301)响应代码的 HTTP 响应,其中包含一个Location
指向新 url的标头。浏览器将向该位置发出新的 HTTP 请求。因此,您的请求属性(和模型属性)不再好,它们不存在于新请求中。
Consider use flash attributes. The RedirectAttributes
class is the way to go. The javadoc has a good example.
考虑使用 flash 属性。该RedirectAttributes
班是要走的路。javadoc 有一个很好的例子。
A Model
attribute is added to the request attributes much later during request processing. You therefore won't see it directly doing this
Model
稍后在请求处理期间将属性添加到请求属性中。因此,您不会直接看到它这样做
@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.
只要相信它最终会在请求属性中,因此在您的 jsp 中可用。
回答by Debojit Saikia
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.
当您redirecting
访问一个新 URL 时,浏览器实际上是在向redirect URL
. 并且请求属性map.addAttribute("message", "User " + login + " deleted");
不存在于新请求中。
You can use RedirectAttributes
to show the message
to the user:
您可以使用RedirectAttributes
向message
用户显示:
@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).
redirectAttributes.addAttribute
从您的属性中构造请求参数,并使用请求参数重定向到所需的页面。并将addFlashAttribute
属性存储在 flashmap 中(在用户会话中维护并在下一个重定向请求得到满足后删除)。
回答by Jimmy
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.
您以错误的方式重定向它。而不是 return "redirect:"+"index" 使用 return "redirect:/index"。将其重定向到您的 get 方法。因为重定向属性是 post/redirect/get 属性。试试这个,你会在屏幕上收到一条闪现信息。而不是模型使用重定向属性。
redirectAttributes.addFlashAttribute("errormsg", "errormessage"); return "redirect:/index.do";
redirectAttributes.addFlashAttribute("errormsg", "errormessage"); 返回“重定向:/index.do”;