Java EE CDI ConversationScoped
5125 단어 CDIactionjsf2pageconversation
The conversation scope holds state associated with a user of the system, and spans multiple requests to the server.A conversation represents a task—a unit of work from the point of view of the user.
The conversation context is active during any JSF request. Most conversations are destroyed at the end of the request. If a conversation should hold state across multiple requests, it must be explicitly promoted to a long-running conversation.
Conversation demarcation
CDI provides a built-in bean for controlling the lifecycle of conversations in a JSF application. This bean may be obtained by injection:
@Inject
Conversation conversation;
To promote the conversation associated with the current request to a long-running conversation, call the begin() method from application code. To schedule the current long-running conversation context for destruction at the end of the current request, call end().
public void beginConversation() {
if (conversation.isTransient()) {
conversation.begin();
conversation.setTimeout(1000 * 60 * 30);
}
}
public void endConversation() {
conversation.end();
}
The container is permitted to destroy a conversation and all state held in its context at any time in order to conserve resources. The Conversation object provides a method to set the timeout. The timeout is the period of inactivity before the conversation is destroyed (as opposed to the amount of time the conversation is active).
The method beginConversation() may be called when the page is generated by the means of the JSF preRenderView event.
...
<f:event type="preRenderView" listener="#{action.beginConversation}"/>
<h:head>
...
</h:head>
...
Conversation propagation
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.
We can force the conversation to propagate with a non-faces request by including the unique identifier of the conversation as a request parameter. The CDI specification reserves the request parameter named cid for this use. The unique identifier of the conversation may be obtained from the Conversation object, which has the EL bean name javax.enterprise.context.conversation.
Therefore, the following link propagates the conversation:
<a href="/addProduct.jsp?cid=#{javax.enterprise.context.conversation.id}">Add Product</a>
It's probably better to use one of the link components in JSF 2:
<h:link outcome="/addProduct.xhtml" value="Add Product">
<f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/>
</h:link>
In order to keep the code simple, you can define custom tags beginConversation and conversationId:
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<f:event type="preRenderView" listener="#{action.beginConversation}"/>
</ui:composition>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/>
</ui:composition>
Difference between JSF FlowScoped and CDI ConversationScoped
A flow is a group of views related by navigation rules. Flow-scoped beans stay alive as long as you navigate within this flow, and they die when you leave the flow.Conversation-scoped beans can also survive navigation, but their lifetime is delimited programmatically by calling Conversation.begin() and Conversation.end(). They are not directly related to a specific group of views.You can use conversation scoped beans without using JSF.
Reference
Java EE CDI ConversationScoped example
Java EE CDI bean scopes
Java EE 7 CDI bean scopes
Java EE CDI ViewScoped example
Java EE CDI TransactionScoped example
JSF Flow example
Weld reference documentation
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
GitHub 작업을 사용하여 PyPI에 패키지 게시 및 릴리스 버전 자동화버전 생성은 릴리스를 생성하고 패키지를 PyPI 라이브러리에 게시하는 데 동일한 버전을 사용해야 함을 의미합니다. 이 기사에서는 이 프로세스를 자동화할 것입니다. 이제 동일한 것을 GitHub 리포지토리 비밀에 추가...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.