From Documentation

Enterprise JavaBeans (EJB) technology is the server-side component architecture for Java EE. Here we describe how to use it in a ZUML document.

Here we use JBoss as the example. The configuration of the server might vary from one server to another, but the ZUML document is the same.

Notice that if you would like to access EJB in Java (such as in a composer or in a richlet), you could skip this section (since you could use the approach described in any EJB guide).

Use JndiVariableResolver to Resolve EJB in EL Expressions

Referencing an EJB in an EL expression is straightforward: specifying JndiVariableResolver in the variable-resolver directive. For example,

<?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver" ?>

Depending your configuration, you might have to pass extra information about JNDI to it such as:

<?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver"
  arg1="mail=java:comp/env/mailing,sec=java:comp/security/module" ?>
    arg0: prepend - the prepended part of JDNDI name
    arg1: mapping - the key-value pairs for JNDI names and the corresponding variable names

JndiVariableResolver will resolve variables in the following order:

  1. java:comp/env
  2. java:comp
  3. java:
  4. Variable could be found as a session beans with the prepend argument (arg0).
  5. The key-value pairs which is defined in the mapping argument (arg1)

Example: Retrieve Session Beans

The session beans are bound to the java:comp/env configured by jboss-web.xml and web.xml. For example, suppose we have them as follows:





Then, we could access them as follows.

<?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver" ?>
<listbox width="600px">
    <listhead sizable="true">
        <listheader label="name" sort="auto"/>
        <listheader label="email" sort="auto"/>
    <listitem forEach="${personLocalBean.allPersons}"> <!-- resolve personLocalBean from JNDI -->
        <listcell label="${each.name}"/>
        <listcell label="${each.email}"/>

The variables provided by a variable resolver is also available to the Java code in zscript. For example,


Example: Retrieve EntityManagerFactory

Persistence units are not bound into JNDI by default, so we have to define JBoss specific properties in persistence.xml to bind them into JNDI. For example,

        <property name="jboss.entity.manager.factory.jndi.name" value="java:comp/entityManagerFactory"/>

Then, we could retrieve the entity manager factory by use of JndiVariableResolver.

Source Code

You can get all source code mentioned in this section at github

Version History

Version Date Content

Last Update : 2022/07/08

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.