Is there a way to inject HttpSession with Spring in JSF application?
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.Cur开发者_StackOverflowrent.Session in ASP.NET), it will also be of good use. Thanks in advance.
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.
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...
}
精彩评论