Accessing Spring Bean in the ZUML page"
m (correct highlight (via JWB)) |
|||
Line 1: | Line 1: | ||
{{ZKDevelopersGuidePageHeader}} | {{ZKDevelopersGuidePageHeader}} | ||
− | There are two ways to access Spring-Managed beans in your ZUML page. One is using < | + | 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 === | === Using variable-Resolver === | ||
− | Simply declare the < | + | 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 19: | 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 === | === 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" > | <source lang="xml" > | ||
Line 45: | Line 45: | ||
</source> | </source> | ||
− | Where the < | + | 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.