2011-01-28 7 views
4

ConversationScoped beanをサーブレットに挿入する必要があります。私は、標準のシンプルな@Injectタグを使用して、私は、CIDパラメータを使用してサーブレットを呼び出すが、それは、私は次のエラーを取得するに注射Bean内の任意のメソッドを呼び出すとき:サーブレットにConversationScoped Beanを挿入する方法

org.jboss.weld.context.ContextNotActiveException : WELD-001303 No active contexts for scope type javax.enterprise.context.ConversationScoped

私はサーブレットでこれらのBeanを注入することができるか、私は注入することができセッションBeanと要求スコープBeanのみ?

+0

@ConversationScoped Beanを使用しようとすると、会話が現れないように見えます。 doGet/doPostメソッドで使用していますか?そうであれば、リクエストのために自動的に作成された会話が必要になります。 – amorfis

答えて

1

サーブレットでは、コンテキストとはアプリケーションのコンテキストで、会話の範囲が緩いのです。ここ はConversationContextの相当で前の回答で提案されている何

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 

import org.jboss.weld.Container; 
import org.jboss.weld.context.ContextLifecycle; 
import org.jboss.weld.context.ConversationContext; 
import org.jboss.weld.servlet.ConversationBeanStore; 


public abstract class ConversationalHttpRequest { 
    protected HttpServletRequest request; 


    public ConversationalHttpRequest(HttpServletRequest request) { 
     this.request = request; 
    } 

    public abstract void process() throws Exception; 

    public void run() throws ServletException { 
     try { 
      initConversationContext(); 
      process(); 
     } catch (Exception e) { 
      throw new ServletException("Error processing conversational request", e); 
     } finally { 
      cleanupConversationContext(); 
     } 
    } 

    private void initConversationContext() { 
     ConversationContext conversationContext = Container.instance().deploymentServices().get(ContextLifecycle.class).getConversationContext(); 
     conversationContext.setBeanStore(new ConversationBeanStore(request.getSession(), request.getParameter("cid"))); 
     conversationContext.setActive(true); 
    } 

    private void cleanupConversationContext() { 
     ConversationContext conversationContext = Container.instance().deploymentServices().get(ContextLifecycle.class).getConversationContext(); 
     conversationContext.setBeanStore(null); 
     conversationContext.setActive(false); 
    } 

} 
+0

問題は、まずCDIのWeld実装に依存していることです(アプリケーションサーバ実装を必ずしも使用しないでください)。次に、Container.instance()。deploymentServicesは 'private'です。 –

0

...あなたが匿名クラスとして使用し、サーブレットに対話スコープのサポートをしたい場合に、要求を包むことができる小さなユーティリティクラスですJava EEではWeldを使用しないのですか?

関連する問題