Conversation scope
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>
相关推荐
因此,对于部署在Java EE 6兼容的应用服务器上的JSF应用,推荐使用CDI而不是JSF的标准注解。 存在两个标准和两套注解可能会引起混淆。Gavin King解释了为何会有两个规范,并建议在有CDI环境的情况下使用CDI。@...
JBoss Weld CDI是Java平台上的一个核心组件,它实现了Java企业版(Java EE)的 Contexts and Dependency Injection (CDI) 规范。CDI是Java EE中的一个关键部分,它提供了一种管理和注入依赖关系的方式,使得开发者...
CDI(Contexts and Dependency Injection for the Java EE Platform)是Java企业版(Java EE)中的一个核心组件,它提供了一种在应用程序中管理和注入依赖关系的标准化方式。CDI允许开发者通过声明式的方式管理对象...
- JSF 2.0集成了Java EE 6的CDI(Contexts and Dependency Injection),使得组件之间可以通过注解进行依赖注入,提高了代码的可测试性和可维护性。 - 例如,`@ManagedProperty` 注解可以用来注入其他bean的属性,...
JBoss Seam 2.0 是一个开源的Java EE框架,它将企业级服务与富互联网应用(RIA)开发紧密结合,简化了构建复杂Web应用的过程。Seam的核心特性之一是其对上下文(Contexts)和依赖注入(CDI)的支持,这使得开发者...
WebBeans,也称为Weld,是Java企业版(Java EE)中的一个核心组件,它是JSR-299规范的实现,主要负责依赖注入(Dependency Injection, DI)和上下文及会话管理(Contexts and Dependency Injection, CDI)。...
Seam是一个Java EE框架,主要用于构建富互联网应用程序(Rich Internet Applications)。它整合了JSF、EJB、CDI、Hibernate等技术,为开发者提供了一种更高效、更灵活的开发方式。性能测试是确保Seam应用能够高效...