【问题标题】:Spring MVC - getting model data after a redirectSpring MVC - 重定向后获取模型数据
【发布时间】:2018-09-11 07:05:05
【问题描述】:

我想在重定向后将数据传递给视图。例如,我按下一个按钮,它会将我重定向到包含来自控制器的数据的页面。我正在尝试使用每个人都建议的 RedirectAttribute,但我无法让它工作。任何帮助表示赞赏。

index.jsp:

<a href="user.htm">Display All Users</a>

控制器:

@RequestMapping(value = "/user.htm")
public ModelAndView addUser(RedirectAttributes redirAtt) {
    ModelAndView mv = new ModelAndView("redirect:user");
    String out = "All User Details: ";
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
        List result = session.createQuery("from Users").list();
        mv.addObject("users", result);
        session.getTransaction().commit();

    } catch (Exception e) {
        e.printStackTrace();
    }

    redirAtt.addFlashAttribute("message", out);
    return mv;
}

我希望数据显示在的jsp:user.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>${message}</title>
    </head>
    <body>
        <h1>${message}</h1><br>
        <table>
            <tr>
                <th>Username</th>
                <th>Nickname</th>
                <th>Email</th>
                <th>Password</th>
            </tr>
            <c:forEach items="${users}"  var="user">
                <tr>
                    <td><c:out value="${user.username}"/></td>
                    <td><c:out value="${user.nickname}"/></td>
                    <td><c:out value="${user.email}"/></td>
                    <td><c:out value="${user.password}"/></td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>

页面重定向时不显示数据。

我的控制器也是这样设计的:

@RequestMapping(value = "/test.htm")
public String addUser(RedirectAttributes redirectAttributes) {
    String out = "All User Details: ";
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
        List result = session.createQuery("from Users").list();
        session.getTransaction().commit();

        redirectAttributes.addAttribute("message", "testing testing tesing");

    } catch (Exception e) {
        e.printStackTrace();
    }
    return "redirect:/user.jsp";
}

@RequestMapping(value = "/user.jsp")
public ModelAndView test(@ModelAttribute("message") String myMessage) {

    ModelAndView mv = new ModelAndView("user");
    mv.addObject("message", myMessage);
    return mv;

}

当我尝试这个时,重定向的 url 是:

http://localhost:8080/projname/user.jsp?message=testing+testing+tesing

所以我认为属性被传递了,但也许我输出错误?

【问题讨论】:

  • 如果你使用redirect,你可以通过URL传递参数,这意味着值来自你在浏览器中看到的url,你可以使用它放置单个对象,但如果传递多个对象,url大小可能会达到GET方式的限制,建议换一种方式:redirect后查询对象
  • 我现在只想得到一个消息值。我该怎么做?
  • 你可以使用 RedirectAttrbutes addAttribute 方法来做,见docs.spring.io/spring-framework/docs/current/javadoc-api/org/…

标签: java spring jsp spring-mvc controller


【解决方案1】:

处理重定向属性有三种可能性 1. 在 RedirectAttributes 中添加 flash/model-attributes 2. 将请求参数('动态'属性)添加到 RedirectAttributes 3. 向RedirectView添加request-parameter('动态'属性)

我在以下 get 方法中结合了这三种可能性。

@GetMapping("/redirectme")
public RedirectView redirectme(final RedirectAttributes redirectAttributes) {
    // the following attribute is a ModelAttribute
    redirectAttributes.addFlashAttribute("messageA", "A");
    final RedirectView redirectView = new RedirectView("/redirectedpage", true);
    // the following attributes are request-parameter (dynamic Attributes)
    redirectAttributes.addAttribute("messageB", "B");
    redirectView.getAttributesMap().put("messageC", "C");
    return redirectView;
}

@GetMapping("/redirectedpage")
public ModelAndView redirectedPage(
        // access FlashAttributes
        final Model model, @ModelAttribute("messageA") final String messageA,
        // access 'dynamic' Attributes
        @RequestParam("messageB") final String messageB, @RequestParam("messageC") final String messageC) {
    final ModelAndView modelAndView = new ModelAndView("red");
    modelAndView.addObject("caption", "App");
    // access FlashAttributes
    modelAndView.addObject("messageA_1", model.asMap().get("messageA"));
    modelAndView.addObject("messageA_2", messageA);
    // access 'dynamic' Attributes
    modelAndView.addObject("messageB", messageB);
    modelAndView.addObject("messageC", messageC);
    return modelAndView;
}

【讨论】:

    【解决方案2】:

    确保将 @Controller 放在控制器类的顶部。

    redirect:/user.jsp -> 这应该是 jsp 文件的相对路径。

    【讨论】:

    • 是的,控制器工作正常,我不知道为什么值不会显示
    • 试试这个:在 @RequestMapping(value = "/user.jsp") 里面,在创建 mv 对象之后。 mv.setViewName("user");
    【解决方案3】:

    如果你想重定向到直接jsp然后使用模型属性绑定“消息”参数。

    ModelAndView mv = new ModelAndView("user");
    mv.addObject("message", out);
    

    或者如果你想在另一个控制器上重定向 然后使用 RedirectAttributes 绑定“消息”参数,如:

    ModelAndView mv = new ModelAndView("redirect:/user.abc");
    redirAtt.addFlashAttribute("message", out);
    

    【讨论】:

      猜你喜欢
      • 2019-12-11
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      相关资源
      最近更新 更多