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 …') |
m (correct highlight (via JWB)) |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
{{ZKDevelopersGuidePageHeader}} | {{ZKDevelopersGuidePageHeader}} | ||
− | Simply declare the < | + | There are two ways to access Spring-Managed beans in your ZUML page. One is using <code>variable-resolver</code>, and the other is using <code>SpringUtil</code>. Which to use depends on your usage, in the ZUML page, we suggest you to use <code>variable-resolver</code>. |
+ | |||
+ | === Using variable-Resolver === | ||
+ | Simply declare the <code>variable-resolver</code> for <code>org.zkoss.zkplus.spring.DelegatingVariableResolver</code> 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. | ||
<source lang="xml" > | <source lang="xml" > | ||
Line 16: | Line 19: | ||
</source> | </source> | ||
− | < | + | <code>variable-resolver </code>will look-up the bean named <code>DataSource</code> automatically for you, and returned a list to the <code>forEach</code> loop. |
+ | === Using SpringUtil === | ||
+ | <code>org.zkoss.zkplus.spring.SpringUtil</code> 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 <code>forEach</code> loop is looping over the collection to print the <code>${each}</code> attribute on each object in the collection. | ||
{{ ZKDevelopersGuidePageFooter}} | {{ ZKDevelopersGuidePageFooter}} |
Latest revision as of 10:38, 19 January 2022
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.