Spring MVC - 重定向后获取模型数据

B K*_*B K 1 java spring jsp controller spring-mvc

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

索引.jsp:

<a href="user.htm">Display All Users</a>
Run Code Online (Sandbox Code Playgroud)

控制器:

@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;
}
Run Code Online (Sandbox Code Playgroud)

我想要在上面显示数据的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>
Run Code Online (Sandbox Code Playgroud)

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

我也这样做了我的控制器:

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

}
Run Code Online (Sandbox Code Playgroud)

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

http://localhost:8080/projname/user.jsp?message=testing+testing+tesing
Run Code Online (Sandbox Code Playgroud)

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

小智 5

处理重定向属性有三种可能性

  1. 将 flash/model-attributes 添加到 RedirectAttributes
  2. 将请求参数(“动态”属性)添加到 RedirectAttributes
  3. 将请求参数(“动态”属性)添加到 RedirectView

我在以下 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;
}
Run Code Online (Sandbox Code Playgroud)