Using Spring Variable Resolver"

From Documentation
 
(16 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
===Purpose===
 
===Purpose===
 
Access Spring managed bean within ZK framework
 
Access Spring managed bean within ZK framework
 +
 +
We assume you are already familiar with the Spring Framework - If not please '''refer to the official and extensive [https://docs.spring.io/spring/docs/current/spring-framework-reference/ Spring Framework Documentation].
 +
'''
 +
 
===DelegatingVariableResolver===
 
===DelegatingVariableResolver===
 +
 
You can access any spring managed beans by its id within ZK, for example, on the ZUML page by declaring <code>variable-resolver</code> for <javadoc>org.zkoss.zkplus.spring.DelegatingVariableResolver</javadoc> at the top of your ZUML page.
 
You can access any spring managed beans by its id within ZK, for example, on the ZUML page by declaring <code>variable-resolver</code> for <javadoc>org.zkoss.zkplus.spring.DelegatingVariableResolver</javadoc> at the top of your ZUML page.
  
Line 9: Line 14:
 
<source lang="java">
 
<source lang="java">
 
package org.zkoss.zkspringessentials.beans;
 
package org.zkoss.zkspringessentials.beans;
 
 
public class SimpleBean {
 
public class SimpleBean {
 
 
         private String message;
 
         private String message;
 
          
 
          
        public SimpleBean() {
 
        }
 
 
         public SimpleBean(String msg) {
 
         public SimpleBean(String msg) {
 
                 this.message = msg;
 
                 this.message = msg;
Line 21: Line 22:
 
         public String getMessage() {
 
         public String getMessage() {
 
                 return message;
 
                 return message;
        }
 
        public void setMessage(String message) {
 
                this.message = message;
 
 
         }
 
         }
 
}
 
}
 
</source>
 
</source>
Since Spring 3.0 you have different ways of declaring your beans. The first and the most traditional way is to declare this bean in your applicationContext.xml Spring configuration file as below
+
 
 +
Spring offers different ways of declaring your beans. The classical way is to declare this bean in your applicationContext.xml Spring configuration file as below.
 +
 
 
<source lang="xml">
 
<source lang="xml">
 
<bean id="simpleBean" class="org.zkoss.zkspringessentials.beans.SimpleBean">
 
<bean id="simpleBean" class="org.zkoss.zkspringessentials.beans.SimpleBean">
        <constructor-arg value="Hello from a simple bean"></constructor-arg>
+
    <constructor-arg value="Hello from a simple bean"/>
 
</bean>
 
</bean>
 
</source>
 
</source>
  
or you can let Spring scans your beans (normally used for auto-wiring dependencies) automatically by providing a base package. The declaration of the Spring context component scan element is shown below:
+
The alternative Java Config to achieve the same (choose for yourself: Spring and ZK will treat both beans equivalently during runtime):
  
<source lang="xml" high="8,9">
+
<source lang="java">
<beans xmlns="http://www.springframework.org/schema/beans"
+
    @Bean
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
    public SimpleBean simpleBean() {
              xmlns:context="http://www.springframework.org/schema/context"
+
        return new SimpleBean("Hello from a simple bean");
              xsi:schemaLocation="http://www.springframework.org/schema/beans
+
    }
                          http://www.springframework.org/schema/beans/spring-beans.xsd
 
                          http://www.springframework.org/schema/context
 
                          http://www.springframework.org/schema/context/spring-context.xsd">
 
        <context:component-scan  base-package="org.zkoss.zkspringessentials.beans">
 
          </context:component-scan>
 
</beans>
 
 
</source>
 
</source>
  
Now using the <javadoc>org.zkoss.zkplus.spring.DelegatingVariableResolver</javadoc> you can acess this bean in ZSCRIPT, EL expressions and ZK data binding annotations
+
Now using the <javadoc>org.zkoss.zkplus.spring.DelegatingVariableResolver</javadoc> you can access this bean in ZSCRIPT, EL expressions and ZK data binding annotations.
 +
 
 
====Access Spring beans in ZSCRIPT====
 
====Access Spring beans in ZSCRIPT====
 
You can access SimpleBean by its bean id in ZSCRIPT as shown below
 
You can access SimpleBean by its bean id in ZSCRIPT as shown below
<source lang="xml" high="2,5">
+
<source lang="xml" highlight="1,4">
<?xml version="1.0" encoding="UTF-8"?>
 
 
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
 
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
 
<zk>
 
<zk>
Line 60: Line 54:
 
</zscript>
 
</zscript>
 
<window title="Bean in ZScript" width="640px" border="normal" >
 
<window title="Bean in ZScript" width="640px" border="normal" >
     <vbox>
+
     <label value="${msg}"/>
        <hbox>
 
            <label value="${msg}"></label>
 
        </hbox>
 
    </vbox>  
 
 
</window>
 
</window>
 
</zk>
 
</zk>
Line 71: Line 61:
 
====Access Spring beans in EL expressions====
 
====Access Spring beans in EL expressions====
 
Similarly you can also access Spring managed beans in any EL expressions as shown below
 
Similarly you can also access Spring managed beans in any EL expressions as shown below
<source lang="xml" high="2,7">
+
<source lang="xml" highlight="1,4">
<?xml version="1.0" encoding="UTF-8"?>
 
 
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
 
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
 
<zk>
 
<zk>
 
<window title="Bean in EL Expression" width="640px" border="normal" >
 
<window title="Bean in EL Expression" width="640px" border="normal" >
     <vbox>
+
     <label value="${simpleBean.message}"/>
        <hbox>
 
            <label value="${simpleBean.message}"></label>
 
        </hbox>
 
    </vbox>  
 
 
</window>
 
</window>
 
</zk>
 
</zk>
Line 86: Line 71:
  
 
====Access Spring beans in ZK Databinding annotations====
 
====Access Spring beans in ZK Databinding annotations====
Coupling ZK <javadoc>org.zkoss.zkplus.databind.AnnotateDataBinderInit</javadoc> with <javadoc>org.zkoss.zkplus.spring.DelegatingVariableResolver</javadoc> Spring beans can also be used in ZK Databinding expressions as shown below.
+
The same <javadoc>org.zkoss.zkplus.spring.DelegatingVariableResolver</javadoc> also resolves Spring beans in ZK Databinding expressions:
Let's first define a Person bean class with two fields for holding first name and last name String variables with corresponding setters/getters
 
<source lang="java">
 
public class Person {
 
        private String _firstName = "";
 
        private String _lastName = "";
 
 
 
        // getter and setters
 
        public void setFirstName(String firstName) {
 
                _firstName = firstName;
 
        }
 
 
 
        public String getFirstName() {
 
                return _firstName;
 
        }
 
 
 
        public void setLastName(String lastName) {
 
                _lastName = lastName;
 
        }
 
 
 
        public String getLastName() {
 
                return _lastName;
 
        }
 
 
 
        public void setFullName(String f) {
 
                // do nothing.
 
        }
 
 
 
        public String getFullName() {
 
                return _firstName + " " + _lastName;
 
        }
 
}
 
</source>
 
and declare this bean in the applicationContext.xml Spring configuration file as shown below
 
<source lang="xml">
 
<bean id="person" class="org.zkoss.zkspringessentials.beans.Person">
 
        <property name="firstName" value="John"></property>
 
        <property name="lastName" value="Woo"></property>
 
</bean>
 
</source>
 
Now we can populate ZK grid component using ZK databinding mechanism that binds the Person bean first name and last name properies with corresponding textboxes.
 
  
<source lang="xml" high="2,3,8,9,10">
+
<source lang="xml" highlight="1,5">
<?xml version="1.0" encoding="UTF-8"?>
 
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
 
 
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
 
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
 
<zk>
 
<zk>
<window title="Bean in ZK databinding annotations" width="640px" border="normal" >
+
<window title="Bean in Databinding Annotation" width="640px" border="normal"  
     <grid width="400px">
+
     viewModel="...">
        <rows>
+
    <label value="@load(simpleBean.message)"/>
            <row> First Name: <textbox value="@{person.firstName}"/></row>
 
            <row> Last Name: <textbox value="@{person.lastName}"/></row>
 
            <row> Full Name: <label value="@{person.fullName}"/></row>
 
        </rows>
 
    </grid>
 
 
</window>
 
</window>
 
</zk>
 
</zk>
Line 148: Line 86:
 
You can also access Spring beans on a ZUML page without <javadoc>org.zkoss.zkplus.spring.DelegatingVariableResolver</javadoc> by using a utility class <javadoc>org.zkoss.zkplus.spring.SpringUtil</javadoc> method <javadoc method="getBean()">org.zkoss.zkplus.spring.SpringUtil</javadoc>. Here is an example code to demonstrate this
 
You can also access Spring beans on a ZUML page without <javadoc>org.zkoss.zkplus.spring.DelegatingVariableResolver</javadoc> by using a utility class <javadoc>org.zkoss.zkplus.spring.SpringUtil</javadoc> method <javadoc method="getBean()">org.zkoss.zkplus.spring.SpringUtil</javadoc>. Here is an example code to demonstrate this
  
<source lang="xml" high="6">
+
<source lang="xml" highlight="5">
<?xml version="1.0" encoding="UTF-8"?>
 
 
<zk>
 
<zk>
 
<zscript>
 
<zscript>
Line 158: Line 95:
 
</zscript>
 
</zscript>
 
<window title="Example for SpringUtil#getBean" width="640px" border="normal" >
 
<window title="Example for SpringUtil#getBean" width="640px" border="normal" >
     <vbox>
+
     <label value="${msg}"/>
        <hbox>
 
            <label value="${msg}"></label>
 
        </hbox>
 
    </vbox>  
 
 
</window>
 
</window>
 
</zk>
 
</zk>
 
</source>
 
</source>
  
=Version History=
 
{{LastUpdated}}
 
{| border='1px' | width="100%"
 
! Version !! Date !! Content
 
|-
 
| &nbsp;
 
| &nbsp;
 
| &nbsp;
 
|}
 
  
 
{{ZKSpringEssentialsPageFooter}}
 
{{ZKSpringEssentialsPageFooter}}

Latest revision as of 08:45, 13 December 2022

Using Spring Variable Resolver



Purpose

Access Spring managed bean within ZK framework

We assume you are already familiar with the Spring Framework - If not please refer to the official and extensive Spring Framework Documentation.

DelegatingVariableResolver

You can access any spring managed beans by its id within ZK, for example, on the ZUML page by declaring variable-resolver for DelegatingVariableResolver at the top of your ZUML page.

Lets define a simple bean first

package org.zkoss.zkspringessentials.beans;
public class SimpleBean {
        private String message;
        
        public SimpleBean(String msg) {
                this.message = msg;
        }
        public String getMessage() {
                return message;
        }
}

Spring offers different ways of declaring your beans. The classical way is to declare this bean in your applicationContext.xml Spring configuration file as below.

<bean id="simpleBean" class="org.zkoss.zkspringessentials.beans.SimpleBean">
    <constructor-arg value="Hello from a simple bean"/>
</bean>

The alternative Java Config to achieve the same (choose for yourself: Spring and ZK will treat both beans equivalently during runtime):

    @Bean
    public SimpleBean simpleBean() {
        return new SimpleBean("Hello from a simple bean");
    }

Now using the DelegatingVariableResolver you can access this bean in ZSCRIPT, EL expressions and ZK data binding annotations.

Access Spring beans in ZSCRIPT

You can access SimpleBean by its bean id in ZSCRIPT as shown below

<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<zk>
<zscript>
        String msg = simpleBean.message;
</zscript>
<window title="Bean in ZScript" width="640px" border="normal" >
    <label value="${msg}"/>
</window>
</zk>

Access Spring beans in EL expressions

Similarly you can also access Spring managed beans in any EL expressions as shown below

<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<zk>
<window title="Bean in EL Expression" width="640px" border="normal" >
    <label value="${simpleBean.message}"/>
</window>
</zk>

Access Spring beans in ZK Databinding annotations

The same DelegatingVariableResolver also resolves Spring beans in ZK Databinding expressions:

<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<zk>
<window title="Bean in Databinding Annotation" width="640px" border="normal" 
    viewModel="...">
    <label value="@load(simpleBean.message)"/>
</window>
</zk>

SpringUtil

You can also access Spring beans on a ZUML page without DelegatingVariableResolver by using a utility class SpringUtil method SpringUtil.getBean(). Here is an example code to demonstrate this

<zk>
<zscript>
        import org.zkoss.zkplus.spring.SpringUtil;
        import org.zkoss.zkspringessentials.beans.*;
        SimpleBean simple = SpringUtil.getBean("simpleBean");
        String msg = simple.getMessage();
</zscript>
<window title="Example for SpringUtil#getBean" width="640px" border="normal" >
    <label value="${msg}"/>
</window>
</zk>



Last Update : 2022/12/13

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