CDI"
(moved ZK Developer's Reference/Integration/CDI to ZK Developer's Reference/Integration/Middleware Layer/CDI) |
m (remove empty version history (via JWB)) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | {{ZKDevelopersReferencePageHeader}} | |
+ | |||
+ | CDI ([http://jcp.org/en/jsr/detail?id=299 JSR-299]) is an emerging standard for contexts and dependency injection for Java EE. | ||
+ | |||
+ | Here we discuss how to use CDI with ZK, especially the use of <javadoc>org.zkoss.zkplus.cdi.DelegatingVariableResolver</javadoc>. It provides the basic support of CDI which allow a ZUML document to access variables defined in CDI. For more comprehensive support, please refer to another product: [http://code.google.com/p/zkcdi/ ZK CDI]. | ||
+ | |||
+ | For more information, please refer to the following blogs: | ||
+ | |||
+ | * [http://blog.zkoss.org/index.php/2010/01/07/integrate-zk-and-jsr-299weld/ Integrate ZK and JSR-299] | ||
+ | * [http://blog.zkoss.org/index.php/2010/02/11/handling-zk-events-using-cdi-event-notification-model/ Handling ZK Events using CDI event notification model] | ||
+ | |||
+ | =Example= | ||
+ | |||
+ | Here is a ''Hello World'' example. Suppose we have a Java class called HelloWorld as shown below. | ||
+ | |||
+ | <source lang="java"> | ||
+ | @Named | ||
+ | @SessionScoped | ||
+ | public class HelloWorld implements Serializable { | ||
+ | private final String text = "Hello World"; | ||
+ | public String getText() { | ||
+ | return text; | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | Then, we could access it by specifying the variable resolver: <javadoc>org.zkoss.zkplus.cdi.DelegatingVariableResolver</javadoc> as shown below: | ||
+ | |||
+ | <source lang="xml" highlight="1"> | ||
+ | <?variable-resolver class="org.zkoss.zkplus.cdi.DelegatingVariableResolver"?> | ||
+ | <window title="ZK + CDI: Hello World" width="300px"> | ||
+ | My weld-injected bean says: ${helloWorld.text} | ||
+ | </window> | ||
+ | </source> | ||
+ | |||
+ | <javadoc>org.zkoss.zkplus.cdi.DelegatingVariableResolver</javadoc> resolves all variables defined by CDI (with Java annotations). In other words, it makes them visible to the ZUML document, including EL expressions, data binding and zscript. | ||
+ | |||
+ | =Setup Tomcat + Weld= | ||
+ | [http://docs.jboss.org/weld/reference/1.0.0/en-US/html/ Weld] is an implementation of CDI. Here is a brief installation instructions: | ||
+ | |||
+ | * Copy <code>weld-servlet.jar</code> to your application's <code>WEB-INF/lib</code> folder. You can find the jar file in [[https://sourceforge.net/projects/jboss/files/Weld/1.0.0.SP1/weld-1.0.0.SP1.zip/download Weld 1.0 SP1]. | ||
+ | |||
+ | * Add in your application's <code>WEB-INF/web.xml</code> the following listener. This makes Weld bind with Tomcat. | ||
+ | <source lang="xml"> | ||
+ | <listener> | ||
+ | <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class> | ||
+ | </listener> | ||
+ | </source> | ||
+ | |||
+ | * In your application's <code>META-INF</code> folder, creates a <code>context.xml</code> file with following contents. This provides a JNDI reference <code>java:comp/env/BeanManager</code> for the accessing to the Weld bean manager. The ZK CDI variable resolver will need this. | ||
+ | <source lang="xml"> | ||
+ | <Context> | ||
+ | <Resource name="BeanManager" auth="Container" | ||
+ | type="javax.enterprise.inject.spi.BeanManager" | ||
+ | factory="org.jboss.weld.resources.ManagerObjectFactory"/> | ||
+ | </Context></source> | ||
+ | |||
+ | * Add '''WEB-INF/beans.xml''' with empty content to the web root. | ||
+ | |||
+ | |||
+ | |||
+ | {{ZKDevelopersReferencePageFooter}} |
Latest revision as of 04:33, 5 February 2024
CDI (JSR-299) is an emerging standard for contexts and dependency injection for Java EE.
Here we discuss how to use CDI with ZK, especially the use of DelegatingVariableResolver. It provides the basic support of CDI which allow a ZUML document to access variables defined in CDI. For more comprehensive support, please refer to another product: ZK CDI.
For more information, please refer to the following blogs:
Example
Here is a Hello World example. Suppose we have a Java class called HelloWorld as shown below.
@Named
@SessionScoped
public class HelloWorld implements Serializable {
private final String text = "Hello World";
public String getText() {
return text;
}
}
Then, we could access it by specifying the variable resolver: DelegatingVariableResolver as shown below:
<?variable-resolver class="org.zkoss.zkplus.cdi.DelegatingVariableResolver"?>
<window title="ZK + CDI: Hello World" width="300px">
My weld-injected bean says: ${helloWorld.text}
</window>
DelegatingVariableResolver resolves all variables defined by CDI (with Java annotations). In other words, it makes them visible to the ZUML document, including EL expressions, data binding and zscript.
Setup Tomcat + Weld
Weld is an implementation of CDI. Here is a brief installation instructions:
- Copy
weld-servlet.jar
to your application'sWEB-INF/lib
folder. You can find the jar file in [Weld 1.0 SP1.
- Add in your application's
WEB-INF/web.xml
the following listener. This makes Weld bind with Tomcat.
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
- In your application's
META-INF
folder, creates acontext.xml
file with following contents. This provides a JNDI referencejava:comp/env/BeanManager
for the accessing to the Weld bean manager. The ZK CDI variable resolver will need this.
<Context>
<Resource name="BeanManager" auth="Container"
type="javax.enterprise.inject.spi.BeanManager"
factory="org.jboss.weld.resources.ManagerObjectFactory"/>
</Context>
- Add WEB-INF/beans.xml with empty content to the web root.