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>
相关推荐
《Pro CDI 2 in Java EE 8》这本书深入探讨了Java企业版8中的Contexts and Dependency Injection(CDI)2.0规范,它是Java应用中管理组件和服务的核心技术之一。CDI允许开发者以声明式的方式管理对象的生命周期和...
Java EE 7(Java Platform, Enterprise Edition 7)是Java平台的一个版本,专注于为企业级应用程序开发提供服务。这个教程中的examples代码是学习Java EE 7技术的重要资源,它与官方教程紧密配合,帮助开发者深入...
本书《Beginning JAVA EE 7经典》是一本专注于Java EE(Java Platform, Enterprise Edition)7规范的入门到高级技术指导书籍,涵盖了Java EE核心技术如JSF(JavaServer Faces)、CDI(Contexts and Dependency ...
Java EE(Java Platform, Enterprise Edition)是Oracle公司提供的一个企业级应用开发平台,它构建在Java SE(标准版)的基础上,为开发和部署分布式、多层的企业级应用程序提供了丰富的API和工具支持。Java EE旨在...
Java EE,全称为Java Platform, Enterprise Edition,是Java平台针对企业级应用开发的版本,它提供了丰富的组件和服务,用于构建分布式、多层架构的企业应用程序。本资料包包含"精通Java EE:精通Java EE 整合应用...
12. **CDI(Contexts and Dependency Injection)**:Java EE中的依赖注入框架,用于管理对象的生命周期和依赖关系。 13. **WebSocket**:Java EE 7引入的新特性,支持双向通信的网络协议,用于实时Web应用。 14. ...
10. **CDI (Contexts and Dependency Injection)**: CDI是Java EE中的依赖注入框架,简化了组件之间的依赖管理。理解怎样声明和注入bean,以及使用不同范围(如request、session和application)的bean,对于解答相关...
本章内容可能涵盖多个Java EE技术的综合运用,如Servlet、JSP、EJB、JPA、JSF、CDI等,以及如何将它们集成到实际的企业级解决方案中。 Java EE(Java Platform, Enterprise Edition),原名为Java 2 Platform, ...
CDI是Java EE中的依赖注入框架,允许开发者声明性地管理对象的生命周期和依赖关系,提高了代码的可测试性和可维护性。 8. **JMS(Java Message Service)** JMS是Java EE中的消息中间件API,用于创建、发送、接收...
2. **轻量级容器**:为了适应轻量级应用的需求,Java EE 6引入了Web Profile,它包含了一组基础的Java EE组件,如Servlet、JSP、JSF(JavaServer Faces)、CDI(Contexts and Dependency Injection)、JPA等,适合...
包括资源创建、资源注入和打包,还涵盖了多项相关技术,包括JavaServer Faces(JSF)、Java Servlet、WebSocket Java API、JSON处理Java API(JSON—P)、国际化和本地化、bean验证、Java EE上下文和依赖注入(CDI)以及...
11. **CDI(Contexts and Dependency Injection)**:CDI为Java EE提供依赖注入和上下文管理,简化了组件的装配和管理。 12. **WS(Web Services)**:讲解如何创建和消费SOAP或RESTful Web服务,包括WSDL、JAX-WS...
In Pro CDI 2 in Java EE 8, use CDI and the CDI 2.0 to automatically manage the lifecycle of your enterprise Java, Java EE or Jakarta EE application’s beans using predefined scopes and define custom ...
卷二可能深入讨论了Java EE7的高级特性,例如CDI(Contexts and Dependency Injection)1.1、JPA(Java Persistence API)2.1、JMS(Java Message Service)2.0、Batch Applications for the Java Platform以及Java ...
在IT行业中,CDI(Contexts and Dependency Injection)和JSF(JavaServer Faces)是Java EE 6框架中的核心组件,而Weld是CDI的一个实现。这些技术为开发高效、可维护的Web应用程序提供了强大的支持。让我们深入探讨...
Java EE(Java Platform, Enterprise Edition)是Oracle公司提供的一个企业级应用开发平台,它扩展了Java SE(标准版)的功能,专为构建分布式、多层的企业级应用程序而设计。Java EE SDK包含了运行和开发Java EE...
Contexts and Dependency Injection (CDI) 是Java EE中的一个核心规范,提供了依赖注入和上下文管理功能,使得组件之间的关系更加松耦合,易于测试和维护。 8. **WebSocket** Java EE 7引入了WebSocket API,允许...