Is there a way to inject HttpSession with Spring in JSF application?

☆樱花仙子☆ 提交于 2019-12-11 16:49:30

问题


I have a web application written in JSF2 that uses Spring. I need to create a bean that is a wrapper of JSF HTTP session and use it internally. It seems logical to me to inject the HttpSession object into that bean's definition and to scope the bean to the session. However I could not find a way to define the injection in the context.xml file. Is it possible to do this, could it backfire and is there a better way? All I want is to have the current session inside that bean. If there is a static method to get the session (similar to HttpContext.Current.Session in ASP.NET), it will also be of good use. Thanks in advance.


回答1:


I'm not sure about the Spring part (I don't use it, I use just the standard Java EE 6 API), but you can get the HttpSession statically in JSF context by ExternalContext#getSession() as follows:

HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);

You could do that in the bean's (post)constructor.




回答2:


If you are in a Spring-managed handler method, you can simply add the HttpSession object to your handler's method signature and Spring will automatically inject it, as follows:

@RequestMapping("/myhandler.do")
public String myHandler(HttpSession session) {
    ...foo...
}


来源:https://stackoverflow.com/questions/7712482/is-there-a-way-to-inject-httpsession-with-spring-in-jsf-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!