Accessing Spring Bean in the ZUML page"
(Created page with '{{ZKDevelopersGuidePageHeader}} Simply declare the <tt>variable-resolver</tt> for <tt>org.zkoss.zkplus.spring.DelegatingVariableResolver</tt> on top of your ZUML page, then, in …') |
|||
Line 1: | Line 1: | ||
{{ZKDevelopersGuidePageHeader}} | {{ZKDevelopersGuidePageHeader}} | ||
+ | There are two ways to access Spring-Managed beans in your ZUML page. One is using <tt>variable-resolver</tt>, and the other is using <tt>SpringUtil</tt>. Which to use depends on your usage, in the ZUML page, we suggest you to use <tt>variable-resolver</tt>. | ||
+ | |||
+ | === Using variable-Resolver === | ||
Simply declare the <tt>variable-resolver</tt> for <tt>org.zkoss.zkplus.spring.DelegatingVariableResolver</tt> on top of your ZUML page, then, in the rest of your page, you can access any Spring-Managed beans directly using its bean-id. | Simply declare the <tt>variable-resolver</tt> for <tt>org.zkoss.zkplus.spring.DelegatingVariableResolver</tt> on top of your ZUML page, then, in the rest of your page, you can access any Spring-Managed beans directly using its bean-id. | ||
Line 18: | Line 21: | ||
<tt>variable-resolver </tt>will look-up the bean named <tt>DataSource</tt> automatically for you, and returned a list to the <tt>forEach</tt> loop. | <tt>variable-resolver </tt>will look-up the bean named <tt>DataSource</tt> automatically for you, and returned a list to the <tt>forEach</tt> loop. | ||
+ | === Using SpringUtil === | ||
+ | <tt>org.zkoss.zkplus.spring.SpringUtil</tt> is a utility class which allows you to get Spring-managed beans with ease. | ||
+ | |||
+ | <source lang="xml" > | ||
+ | <window> | ||
+ | <zscript><![CDATA[ | ||
+ | import org.zkoss.zkplus.spring.SpringUtil; | ||
+ | import test.*; | ||
+ | |||
+ | DataSource dataSource = SpringUtil.getBean("DataSource"); | ||
+ | List list = dataSource.getElementsList(); | ||
+ | ]]> | ||
+ | </zscript> | ||
+ | |||
+ | <grid> | ||
+ | <rows> | ||
+ | <row forEach="${list}"> | ||
+ | <label value="${each}"/> | ||
+ | </row> | ||
+ | </rows> | ||
+ | </grid> | ||
+ | </window> | ||
+ | </source> | ||
+ | |||
+ | Where the <tt>forEach</tt> loop is looping over the collection to print the <tt>${each}</tt> attribute on each object in the collection. | ||
{{ ZKDevelopersGuidePageFooter}} | {{ ZKDevelopersGuidePageFooter}} |
Revision as of 06:05, 19 July 2010
This documentation is for an older version of ZK. For the latest one, please click here.
There are two ways to access Spring-Managed beans in your ZUML page. One is using variable-resolver, and the other is using SpringUtil. Which to use depends on your usage, in the ZUML page, we suggest you to use variable-resolver.
Using variable-Resolver
Simply declare the variable-resolver for org.zkoss.zkplus.spring.DelegatingVariableResolver on top of your ZUML page, then, in the rest of your page, you can access any Spring-Managed beans directly using its bean-id.
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window>
<grid>
<rows>
<row forEach="${DataSource.elementsList}">
<label value="${each}"/>
</row>
</rows>
</grid>
</window>
variable-resolver will look-up the bean named DataSource automatically for you, and returned a list to the forEach loop.
Using SpringUtil
org.zkoss.zkplus.spring.SpringUtil is a utility class which allows you to get Spring-managed beans with ease.
<window>
<zscript><![CDATA[
import org.zkoss.zkplus.spring.SpringUtil;
import test.*;
DataSource dataSource = SpringUtil.getBean("DataSource");
List list = dataSource.getElementsList();
]]>
</zscript>
<grid>
<rows>
<row forEach="${list}">
<label value="${each}"/>
</row>
</rows>
</grid>
</window>
Where the forEach loop is looping over the collection to print the ${each} attribute on each object in the collection.