如何重定向控制器并在jsp中获取值

时间:2011-09-24 07:06:00

标签: jsp spring-mvc jstl

http://localhost:8080/LACASServer/message.jsp?forgotUser=Mail+has+been+sent+to+your+mail+address

这里forgetUser是地图的关键,我在控制器的方法中设置,重定向到message.jsp,现在我如何在message.jap中使用它来显示该地图的值。我正在使用jstl库

控制方法如下:

@RequestMapping(value = "/forgotPWD",params="username", method = RequestMethod.POST)
            public String forgotPassword(@RequestParam(value = "username", required = false) String username,Map<String, Object> map) {
                System.out.println("forgotPasswordUser"+username);
                ResetPasswordLog resetPasswordLog;
                User forgotPasswordUser = usersService.findUser(username);
                map.put("forgotUser","Mail has been sent to your mail address");
                if(forgotPasswordUser==null){
                    return "redirect:/login.jsp?login_error=1";
                }
                else
                {   



                    Integer uid=forgotPasswordUser.getId();
                    resetPasswordLog=   usersService.setTempHash(uid);
                    String TEMPHASH= resetPasswordLog.getTempHash();
                    String url=Utility.serverURL+"forgot/index?uid="+uid+"&token="+TEMPHASH;
                    System.out.println(url);
                    System.out.println(Utility.mailResetSubject);
                    mailSender.sendMail(Utility.mailFrom,"romijain3186@gmail.com",Utility.mailResetSubject, url);
                    return "redirect:/message.jsp";


                }

            }

1 个答案:

答案 0 :(得分:0)

您需要使用控制器方法(上面显示的方法)来指定“视图”本身(不使用重定向,就像它当前那样)。因此返回值应该是与message.jsp的视图名称对应的String。然后,您可以将映射添加到模型中,它将在JSP中可用。 E.g。

@RequestMapping(value = "/forgotPWD",params="username", method = RequestMethod.POST)
public String forgotPassword(@RequestParam(value = "username", required = false) String username,
         Map<String, Object> map, Model model) {
    [snip]
    map.put("forgotUser","Mail has been sent to your mail address");
    model.addAttribute("userMap", map);
    [snip]
    return "message.jsp"; // or just "message" depending on Spring settings
}

然后在JSP中通过JSTL访问地图:$ {userMap.forgotUser}