Using Spring Variable Resolver"

From Documentation
m
Line 4: Line 4:
 
Access Spring managed bean within ZK framework
 
Access Spring managed bean within ZK framework
 
===DelegatingVariableResolver===
 
===DelegatingVariableResolver===
You can access any spring managed beans by its id within ZK for eg. on ZUML page by declaring <code>variable-resolver</code> for <code>org.zkoss.zkplus.spring.DelegatingVariableResolver</code> at the top of your ZUML page.
+
You can access any spring managed beans by its id within ZK for eg. on ZUML page by declaring <code>variable-resolver</code> for <javadoc>org.zkoss.zkplus.spring.DelegatingVariableResolver</javadoc> at the top of your ZUML page.
  
 
Lets define a simple bean first
 
Lets define a simple bean first
Line 31: Line 31:
 
         </bean>
 
         </bean>
 
</source>
 
</source>
Now using the <code>org.zkoss.zkplus.spring.DelegatingVariableResolver</code> 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 acess this bean in ZSCRIPT, EL expressions and ZK data binding annotations
 
====Acess Spring beans in ZSCRIPT====
 
====Acess 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
Line 67: Line 67:
  
 
====Access Spring beans in ZK Databinding annotations====
 
====Access Spring beans in ZK Databinding annotations====
Coupling ZK <code>AnnotateDataBinderInit</code> with <code>org.zkoss.zkplus.spring.DelegatingVariableResolver</code> Spring beans can also be used in ZK Databinding expressions as shown below.
+
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.
 
Lets first define a Person bean class with two fields for holding first name and last name String variables with corresponding setters/getters
 
Lets 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">
 
<source lang="java">
Line 126: Line 126:
 
</zk>
 
</zk>
 
</source>
 
</source>
 +
 
===SpringUtil===
 
===SpringUtil===
 
You can also access Spring beans on a ZUML page without <code>org.zkoss.zkplus.spring.DelegatingVariableResolver</code> by using a utility class <code>org.zkoss.zkplus.spring.SpringUtil</code> method <javadoc method="getBean()">org.zkoss.zkplus.spring.SpringUtil</javadoc>. Here is an example code to demostrate this
 
You can also access Spring beans on a ZUML page without <code>org.zkoss.zkplus.spring.DelegatingVariableResolver</code> by using a utility class <code>org.zkoss.zkplus.spring.SpringUtil</code> method <javadoc method="getBean()">org.zkoss.zkplus.spring.SpringUtil</javadoc>. Here is an example code to demostrate this

Revision as of 08:01, 25 February 2011

Using Spring Variable Resolver



Purpose

Access Spring managed bean within ZK framework

DelegatingVariableResolver

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

Lets define a simple bean first

public class SimpleBean {

        private String message;
        
        public SimpleBean() {
        }
        public SimpleBean(String msg) {
                this.message = msg;
        }
        public String getMessage() {
                return message;
        }
        public void setMessage(String message) {
                this.message = message;
        }
}

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"></constructor-arg>
        </bean>

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

Acess Spring beans in ZSCRIPT

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

<?xml version="1.0" encoding="UTF-8"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<zk xmlns="http://www.zkoss.org/2005/zul">
<zscript>
        String msg = simpleBean.message;
</zscript>
<window title="Bean in ZScript" width="640px" border="normal" >
 <vbox>
        <hbox>
        <label value="${msg}"></label>
    </hbox>
 </vbox> 
</window>
</zk>

Access Spring beans in EL expressions

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

<?xml version="1.0" encoding="UTF-8"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<zk xmlns="http://www.zkoss.org/2005/zul">
<window title="Bean in EL Expression" width="640px" border="normal" >
 <vbox>
        <hbox>
        <label value="${simpleBean.message}"></label>
    </hbox>
 </vbox> 
</window>
</zk>

Access Spring beans in ZK Databinding annotations

Coupling ZK AnnotateDataBinderInit with DelegatingVariableResolver Spring beans can also be used in ZK Databinding expressions as shown below. Lets first define a Person bean class with two fields for holding first name and last name String variables with corresponding setters/getters

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;
        }
}

and declare this bean in applicationContext.xml Spring configuration file as shown below

<bean id="person" class="org.zkoss.zkspringessentials.beans.Person">
                <property name="firstName" value="John"></property>
                <property name="lastName" value="Woo"></property>
        </bean>

Now we can populate ZK grid component using ZK databinding mechanism that binds Person bean first name and last name properies with corresponding textboxes

<?xml version="1.0" encoding="UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<zk xmlns="http://www.zkoss.org/2005/zul">
<window title="Bean in ZK databinding annotations" width="640px" border="normal" >

 <grid width="400px">
        <rows>
            <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>
</zk>

SpringUtil

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

<?xml version="1.0" encoding="UTF-8"?>
<zk xmlns="http://www.zkoss.org/2005/zul">
<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" >
 <vbox>
        <hbox>
        <label value="${msg}"></label>
    </hbox>
 </vbox> 
</window>
</zk>

Version History

Last Update : 2011/02/25


Version Date Content
     



Last Update : 2011/02/25

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