5

JSFアプリケーションを実行していて、アプリケーションスコープのバッキングBeanを宣言しました(common-beans.xmlまたは @ManagedBean and @ApplicationScoped アノテーションを使用)。

どうすればこれらのBeanに内部からアクセスできますjavax.servlet.http.HttpSessionListener か?

FacesContext セッションリスナーではが利用できないことを理解している ので、以下を使用します。

public class AnHTTPSessionListener implements HttpSessionListener {
    ...
    public void sessionDestroyed(HttpSessionEvent e) {
        AppBean appBean = (AppBean) FacesContext.getCurrentInstance()
                                                .getExternalContext()
                                                .getApplicationMap().get("appBean")
       ...
    }

...期待どおりにNPEをスローしました。

アップデート:

(BalusCの回答前)

最終的には、(アプリケーションスコープのBeanを使用する代わりに) env-entry要素を使用してweb.xmlでアクセスする必要のあるアプリケーション全体の情報を宣言し、次を使用してその情報を取得しました。

   InitialContext ic = new InitialContext();
   Context env = (Context) ic.lookup("java:comp/env");
   appName = (String) env.lookup("appBeanValue");

それは私が考えていたものではありませんが、回避策です。

4

1 に答える 1

9

JSFは、アプリケーションスコープのマネージドBeanをの属性として格納しますServletContext

したがって、これは次のことを行う必要があります。

public void sessionDestroyed(HttpSessionEvent e) {
    AppBean appBean = (AppBean) e.getSession().getServletContext().getAttribute("appBean");
    // ...
}

参照:

于 2012-11-29T11:36:07.347 に答える