【问题标题】:Spring MVC set attribute to request/model/modelMapSpring MVC 将属性设置为 request/model/modelMap
【发布时间】:2023-04-10 16:50:01
【问题描述】:

我使用 Spring MVC。我需要向请求或其他对象添加属性。它应该是将显示在屏幕上的消息。例如,如果我使用纯 Servlet,我可能只是:

request.setAttribute("message", "User deleted");

比在 JSP 页面上

<div id="message">${message}</div>

但是当我尝试在方法中做这样的事情时:

@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String deleteUser(@RequestParam("login") String login,
        ModelMap map, HttpServletRequest request)

模型对象 -

model.addAttribute("message", "User deleted");

地图 -

map.put("message", "User deleted");

模型图 -

map.put("message", "User deleted");

HttpServletRequest -

request.setAttribute("message", "User deleted");

什么都不显示。但是在我的浏览器中我看到: http:// localhost : 8081 /project/index?message=User+deleted

如何解决这个小问题? 谢谢你的回答

更新:

为了清楚起见,我尝试这样做:

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

在我的 JSP 中,我也以这种方式显示用户登录:

${user.login}

它从会话中获取用户,我看到它登录

【问题讨论】:

  • 你是如何使用请求属性的?在 JSP 中?
  • PATH_REDIRECT 的值是多少?
  • 它是“重定向:”。我认为问题出在此重定向中。那么我该如何重定向和发送消息呢?
  • 是的,这改变了一切。请参阅下面的答案。

标签: java spring-mvc


【解决方案1】:

有了你的新信息,问题是redirect:。当您进行重定向时,您会发送一个带有 302(或 301)响应代码的 HTTP 响应,其中带有指向新 URL 的 Location 标头。浏览器将向该位置发出新的 HTTP 请求。因此,您的请求属性(和模型属性)不再有效,它们不存在于 new 请求中。

考虑使用 flash 属性。 RedirectAttributes 类是要走的路。 The javadoc has a good example.


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

相信它最终会出现在请求属性中,因此可以在您的 jsp 中使用。

【讨论】:

  • 是的!而已!我使用了 RedirectAttributes 对象的 attr.addFlashAttribute("message", "User deleted")。谢谢
  • 我有一个问题。我如何在纯 Servlet 中做同样的事情? (没有任何弹簧)
  • @OleksandrHubachov 底层实现使用HttpSession属性。使用纯 Servlet,您可以使用 Filter 在每个请求的 HttpSession 属性中添加和删除。有一个例子here
【解决方案2】:

当您将redirecting 发送到一个新的 URL 时,浏览器实际上正在向redirect URL 发送一个新请求。并且新请求中不存在请求属性map.addAttribute("message", "User " + login + " deleted");

您可以使用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 从您的属性中构造请求参数,并使用请求参数重定向到所需的页面。 addFlashAttribute 将属性存储在 flashmap 中(在用户会话中维护,并在下一个重定向请求得到满足后删除)。

【讨论】:

  • 不幸的是,这不起作用...我不知道为什么。和上次一样,我在浏览器中看到 http://localhost:8081 /project/index?message=User+ololo+deleted
  • 试试这个:&lt;c:out value="${message}" /&gt;
  • @DebojitSaikia 你需要使用addFlashAttribute
  • 感谢您的回答。但在我的情况下,我需要调用 RedirectAttributes 类的 addFlashAttribute 方法
  • @DebojitSaikia 有帮助,但是如果我现在想删除这个 flashAttribute 怎么办?我该如何删除它?我在这个 FlashAttribute 上添加了 null ......它可以工作,但它有点讨厌
【解决方案3】:

您以错误的方式重定向它。而不是 return "redirect:"+"index" 使用 return "redirect:/index"。将其重定向到您的 get 方法。因为redirectattributes 是post/redirect/get 属性。试试这个,你会在你的屏幕上看到一条闪光信息。 而不是模型使用重定向属性。

redirectAttributes.addFlashAttribute("errormsg", "errormessage"); 返回“重定向:/index.do”;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    相关资源
    最近更新 更多