SessionAttributes不能与spring boot一起使用

时间:2015-11-26 05:59:16

标签: java spring spring-boot spring-session

我遇到问题,要通过会话保持电子邮件实时数据跨页面我已经尝试过这个

@Controller
@RequestMapping(value = "/sec")
@SessionAttributes("reset_user_email")
public class SecurityController {}
  1. 从“/ sec / forget”

    接收变量
    @RequestMapping(value = "/forget/", method = RequestMethod.POST)
    public ModelAndView forget_page_d(@ModelAttribute User user, ModelAndView mv) 
        throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException
    {
    mv.addObject("user", user);
    
    mv.addObject("reset_user_email", user.getEmail());
    
    //etc
    }
    
  2. 在“/ sec / forget / user / a”中使用reset_user_email

    @RequestMapping(value = "/forget/user/a", method = RequestMethod.GET)
    public ModelAndView forget_page_p(@ModelAttribute String reset_user_email) {
         ModelAndView mv =new ModelAndView();
    
        mv.setViewName("security/forget_renew");
        mv.addObject("user",reset_user_email);
        System.out.println("a GET user mail = " +reset_user_email);
    
  3. 控制台输出:

    a GET user mail = 
    

    让我的reset_user_email在应用程序的任何地方都可用时会出现什么问题?

    提前谢谢

2 个答案:

答案 0 :(得分:3)

您可以通过以下方式操纵会话属性:

  1. 在方法定义中包含一个HttpSession:

    @RequestMapping("/request")
    public ModelAndView getRequest(@ModelAttribute User user, ModelAndView mv, HttpSession session) {
         mv.addObject("reset_user_email", user.getEmail());
    
         session.setAttribute("reset_user_email", user.getEmail());
         String resetUserEmail = session.getAttribute("reset_user_email");         
    }
    
  2. 使用WebUtils类:

    @RequestMapping("/request")
    public ModelAndView getRequest(@ModelAttribute User user, ModelAndView mv, HttpServletRequest request) {
         mv.addObject("reset_user_email", user.getEmail());
    
         WebUtils.setSessionAttribute(request, "reset_user_email", user.getEmail());
         String resetUserEmail = WebUtils.getSessionAttribute(request, "reset_user_email");  
    }
    

答案 1 :(得分:0)

尝试java.util.Map,而不是ModelAndView,尝试将会话值放在Map中,map.put("your session name",value);

并尝试按String email=(String)map.get("your session name");

获取值