gpt4 book ai didi

java - SessionAttributes 不适用于 spring boot

转载 作者:行者123 更新时间:2023-11-29 04:55:28 25 4
gpt4 key购买 nike

我在通过 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);

控制台输出:

a GET user mail = 

让我的 reset_user_email 在应用程序中随处可用有什么麻烦?

提前谢谢你

最佳答案

您可以通过以下方式操作 session 属性:

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

关于java - SessionAttributes 不适用于 spring boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33931551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com