CDI ConversationScoped long-running Bean not working

前端 未结 1 1322
梦毁少年i
梦毁少年i 2021-01-03 12:49

I got some problems understanding the Conversation scope of Weld or CDI.

In my JSF Faclets page i call:

        
            

        
相关标签:
1条回答
  • 2021-01-03 13:34

    Short answer: Yes, this is the correct behavior.

    Long answer: A conversation represents a "unit of work", which a such has to be demarcated explicitly. This is done with the explicit call of conversation.begin() - as you are doing already. Should you want to use the same conversation over more than one request, you have to propagate it - this is what you are not doing :-)

    When you propagate a conversation, a conversation-id is appended to the request. This tells the container which conversation is wanted. When you just hit the refresh button without a conversation-id in your request a new conversation is generated for each request.

    From the documentation:

    The conversation context automatically propagates with any JSF faces request (JSF form submission) or redirect. It does not automatically propagate with non-faces requests, for example, navigation via a link.

    If you need to propagate it manually, just add the conversation-id to the request:

    <h:link outcome="/addProduct.xhtml" value="Add Product">
       <f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/>
    </h:link>
    

    All that and much more is explained here.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题