有没有办法在JSF应用程序中用Spring注入HttpSession? [英] Is there a way to inject HttpSession with Spring in JSF application?

查看:80
本文介绍了有没有办法在JSF应用程序中用Spring注入HttpSession?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Spring用J​​SF2编写的Web应用程序.我需要创建一个作为JSF HTTP会话包装器的bean,并在内部使用它.在我看来,将HttpSession对象注入该bean的定义中,并将bean的作用域限定为该会话是合乎逻辑的.但是,我找不到在context.xml文件中定义注入的方法.是否可以这样做,是否适得其反,还有更好的方法吗?我想要的是将当前会话包含在该bean中.如果有一个静态方法来获取会话(类似于ASP.NET中的HttpContext.Current.Session),它也将很有用.预先感谢.

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.

推荐答案

我不确定Spring部分(我不使用它,我只使用标准的Java EE 6 API),但是您可以获取通过HttpSession > ExternalContext#getSession() 如下:

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

您可以在bean的(后)构造函数中执行此操作.

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

这篇关于有没有办法在JSF应用程序中用Spring注入HttpSession?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆