|
|
Line 9: |
Line 9: |
| <references/> | | <references/> |
| | | |
− | ==Access Java Bean==
| |
| | | |
− | In the following example
| + | {{ZKDevelopersGuideHeadingToc}} |
| | | |
− | <source lang="xml" >
| |
− | <element attr1=${bean.property}/>
| |
− | </source>
| |
− |
| |
− | <tt>${bean.property}</tt> will be autowired to <tt>bean.getProperty()</tt>.
| |
− |
| |
− | As you can see, developer can access Java Beans with EL intuitively.
| |
− | [[sample code for el access java bean | A full example]]
| |
− |
| |
− | ==Access ZUML Component==
| |
− |
| |
− | The following is an example for referencing a component in an EL expression.
| |
− |
| |
− | <source lang="xml" >
| |
− | <window>
| |
− | <textbox id="source" value="ABC"/>
| |
− | <label value="${source.value}"/>
| |
− | </window>
| |
− | </source>
| |
− |
| |
− | You have to assign <tt>id</tt> for referenced component.
| |
− |
| |
− | ==Access Implicit Object==
| |
− |
| |
− | In the following example, you can easily access implicit object like <tt>session</tt>
| |
− |
| |
− | <source lang="xml" >
| |
− | <window>
| |
− | <label value="${session.deviceType}"/>
| |
− | </window>
| |
− | </source>
| |
− |
| |
− | The browser shows the result: <tt>ajax</tt>.
| |
− |
| |
− | ==More EL Examples==
| |
− | <source lang="xml" >
| |
− | ${empty myMap}
| |
− | ${myMap[entry]}
| |
− | ${3+counter}
| |
− |
| |
− | </source>
| |
− |
| |
− | Tip: <tt>empty</tt> is an operator used to test whether a map, a collection, an array or a string is null or empty.
| |
− |
| |
− | Tip: <tt>myMap[entry]</tt> is a way to access an element of a map. In other words, it is the same as <tt>myMap.get(entry)</tt> in Java.
| |
− |
| |
− | When an EL expression is used as an attribute value, it could return any kind of objects as long as the component accepts it. For example, the following expression will be evaluated to a Boolean object.
| |
− |
| |
− | <source lang="xml" >
| |
− | <window if="${some > 10}">
| |
− | </source>
| |
− |
| |
− | Standard implicit objects, such as <tt>param</tt> and <tt>requestScope</tt>, and ZK implicit objects, such as <tt>self</tt> and <tt>page</tt>, are supported to simplify the use.
| |
− |
| |
− | <source lang="xml" >
| |
− | <textbox value="${param.who} does ${param.what}"/>
| |
− | </source>
| |
− |
| |
− | ==Import Java Methods==
| |
− | You have two ways to import java methods to EL. Through <tt>xel-method</tt>, or <tt>taglib</tt>.
| |
− |
| |
− | ===Through <tt>xel-method</tt>===
| |
− | you can use a processing instruction called the <tt>xel-method</tt> as follows.
| |
− |
| |
− | <source lang="xml" >
| |
− | <?xel-method prefix="c" name="forName"
| |
− | class="java.lang.Class"
| |
− | signature="java.lang.Class forName(java.lang.String)"?>
| |
− | <textbox value="${c:forName('java.util.List')}"/>
| |
− | </source>
| |
− |
| |
− | In example above, <tt>Class.forName("java.util.List")</tt> is called.
| |
− |
| |
− | ===Through <tt>taglib</tt>===
| |
− | To import EL functions from TLD(TagLib Definition)<ref>http://java.sun.com/products/jsp/tutorial/TagLibraries17.html</ref> files, you could use a processing instruction called <tt>taglib</tt>
| |
− | as follows.<ref>http://www.zkoss.org/dsp/web/core is not really an URL. It's a key to tell ZK loader to find the tld file inside ZK jar files.</ref>
| |
− |
| |
− | <source lang="xml" >
| |
− | <?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c" ?>
| |
− | </source>
| |
− |
| |
− | In the following example, we use function <tt>l</tt> to get the property <tt>app.title</tt> defined in resource file.<ref>It's a way to retrieve resources for different locale. Please refer to Chapter. [[Internationalization | Internationalization]]</ref>
| |
− | <source lang="xml" >
| |
− | <window title="${c:l('app.title')}">
| |
− | </source>
| |
− |
| |
− |
| |
− | Inside core.dsp.tld, you can find the definition of function <tt>l</tt>.
| |
− | <source lang="xml" >
| |
− | <function>
| |
− | <name>l</name>
| |
− | <function-class>org.zkoss.xel.fn.CommonFns</function-class>
| |
− | <function-signature>java.lang.String getLabel(java.lang.String)</function-signature>
| |
− | <description>
| |
− | Returns the label of the specified key.
| |
− | </description>
| |
− | </function>
| |
− | </source>
| |
− |
| |
− |
| |
− |
| |
− | '''Notes'''
| |
− | :The Developer's Reference provides more details on EL expressions. Or, you might refer to JSP 2.0 tutorials or guides for more information about EL expressions.
| |
− |
| |
− | <references/>
| |
− |
| |
− | == Quiz ==
| |
− |
| |
− | 1.Execute the following code, and tell what's wrong with the following code? What operator does EL accept?
| |
− | <source lang="xml" >
| |
− | <window>
| |
− | <button id="btn" label="OK"/>
| |
− | ${btn.label=10}
| |
− | </window>
| |
− | </source>
| |
− |
| |
− |
| |
− | 2.Rewrite [[sample code for el access java bean | A full example]] in Access Java Bean, add field age and gender.
| |
| | | |
| {{ ZKDevelopersGuidePageFooter}} | | {{ ZKDevelopersGuidePageFooter}} |