简体   繁体   English

会话重定向到URL时到期

[英]Session expiring when redirecting to URL

It seems that when I attempt to call a URL with ModelAndView(), my session is ended and when the view is loaded, a new session is created without any of the data I hope to persist. 似乎当我尝试使用ModelAndView()调用URL时,我的会话结束,并且在加载视图时,将创建一个新会话,而没有任何我希望保留的数据。

     return new ModelAndView("redirect:http//.....)

Is there a better way to handle sessions besides @SessionAttributes when moving to different controllers? 当转移到不同的控制器时,除了@SessionAttributes之外,还有更好的方法来处理会话吗?

@SessionAttributes session objects are not meant to be shared among controllers. @SessionAttributes会话对象并不意味着在控制器之间共享。 Here are some alternatives: 以下是一些替代方案:

You can store attributes directly into HttpSession object. 您可以将属性直接存储到HttpSession对象中。 You can obtain HttpSession object by adding it to controller method as parameter: 您可以通过将HttpSession对象作为参数添加到控制器方法中来获取它:

public ModelAndView myControllerMethod(..., HttpSession session) {
  ...
  session.setAttribute("someAttribute", someValue);
  ...
}

You can also autowire HttpSession object and then use it in controller methods: 您还可以自动装配HttpSession对象,然后在控制器方法中使用它:

@Autowired
private HttpSession httpSession;

You can create a session scoped bean and inject that bean into controllers that need it: 您可以创建一个会话范围的bean,并将该bean注入需要它的控制器中:

<bean id="mySessionDataBean" class="com.example.MySessionDataBeanImpl" scope="session">
    <aop:scoped-proxy />
</bean>

And then use it in controllers (MySessionDataBeanImpl implements SessionDataBean interface in this example): 然后在控制器中使用它(在此示例中,MySessionDataBeanImpl实现了SessionDataBean接口):

public class MyController {
    ....
    @Autowired
    SessionDataBean sessionDataBean;

    @RequestMapping(...)
    public ModelAndView controllerMethod(...) {
         ...
         sessionDataBean.setSomeValue(someValue);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM