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



我想在重定向后将数据传递到视图。例如,我按下一个按钮,然后将我重定向到一个带有控制器数据的页面。我正在尝试使用每个人都建议的重定向基因,但我无法正常工作。任何帮助都将不胜感激。

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

我想要显示的数据: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

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

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

  1. 将Flash/Model-Attributes添加到redirectattributes
  2. 将请求参数('Dynamic'属性(添加到redirectAttributes
  3. 将请求参数('Dynamic'属性(添加到RedirectView

我将三种可能性结合在一起。

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

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

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

如果要重定向到直接JSP,则使用模型属性绑定"消息"参数。

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

或要重定向另一个控制器然后使用redirectAttributes绑定"消息"参数,例如:

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

最新更新