Spring"
m (remove empty version history (via JWB)) |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{ZKDevelopersReferencePageHeader}} | {{ZKDevelopersReferencePageHeader}} | ||
+ | |||
+ | {{Deprecated|url=http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/Integration/Middleware_Layer/Spring}} | ||
=Overview= | =Overview= | ||
Line 27: | Line 29: | ||
**org.springframework.web.servlet-3.0.2.RELEASE.jar | **org.springframework.web.servlet-3.0.2.RELEASE.jar | ||
− | Put these jar files into your < | + | Put these jar files into your <code>$myApp/WEB-INF/lib/</code>. Here <code>$myApp</code> represents the name of your web application. |
== Configure web.xml == | == Configure web.xml == | ||
− | In your web.xml, you have to define < | + | In your web.xml, you have to define <code>org.springframework.web.context.ContextLoaderListener</code>, and to specify the location of the configuration file to load bean definitions. |
− | <source lang="xml" | + | <source lang="xml" highlight="3"> |
<context-param> | <context-param> | ||
<param-name>contextConfigLocation</param-name> | <param-name>contextConfigLocation</param-name> | ||
Line 44: | Line 46: | ||
== Create Spring Configuration File == | == Create Spring Configuration File == | ||
− | Define bean definitions in < | + | Define bean definitions in <code>applicationContext.xml</code> file, and put it into your <code>WEB-INF</code> directory. |
− | <source lang="xml" | + | <source lang="xml" highlight="5"> |
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> | <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> | ||
Line 58: | Line 60: | ||
Then you have to define a DataSource interface and its implementation: | Then you have to define a DataSource interface and its implementation: | ||
− | < | + | <code>''DataSource.java''</code> |
<source lang="java" > | <source lang="java" > | ||
Line 69: | Line 71: | ||
</source> | </source> | ||
− | < | + | <code>''DataSourceImpl.java''</code> |
<source lang="java" > | <source lang="java" > | ||
Line 91: | Line 93: | ||
= Accessing Spring Bean in the ZUML page = | = Accessing Spring Bean in the ZUML page = | ||
− | 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> with <javadoc>org.zkoss.zkplus.spring.DelegatingVariableResolver</javadoc> 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" highlight="5"> |
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?> | <?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?> | ||
<window> | <window> | ||
Line 109: | Line 111: | ||
</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. |
===Use with Composer=== | ===Use with Composer=== | ||
<javadoc>org.zkoss.zk.ui.util.GenericAutowireComposer</javadoc> will wire the variables defined in the variable resolvers. Thus, we could declare <javadoc>org.zkoss.zkplus.spring.DelegatingVariableResolver</javadoc> in the ZUML document, and then declare the Spring-managed bean as a control directly in a composer. For example, | <javadoc>org.zkoss.zk.ui.util.GenericAutowireComposer</javadoc> will wire the variables defined in the variable resolvers. Thus, we could declare <javadoc>org.zkoss.zkplus.spring.DelegatingVariableResolver</javadoc> in the ZUML document, and then declare the Spring-managed bean as a control directly in a composer. For example, | ||
− | <source lang="xml" | + | <source lang="xml" highlight="1"> |
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?> | <?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?> | ||
<window apply="foo.MyComposer"> | <window apply="foo.MyComposer"> | ||
Line 126: | Line 128: | ||
Then, if a data member's name matches a Spring-managed bean, it will be wired automatically too. For example, | Then, if a data member's name matches a Spring-managed bean, it will be wired automatically too. For example, | ||
− | <source lang="java" | + | <source lang="java" highlight="2"> |
public class PasswordSetter extends GenericFowardComposer { | public class PasswordSetter extends GenericFowardComposer { | ||
private User user; //wired automatically if user is a spring-managed bean | private User user; //wired automatically if user is a spring-managed bean | ||
Line 140: | Line 142: | ||
== Using SpringUtil == | == Using SpringUtil == | ||
− | < | + | <code>org.zkoss.zkplus.spring.SpringUtil</code> is a utility class which allows you to get Spring-managed beans in Java code with ease. |
− | <source lang="xml" | + | <source lang="xml" highlight="6"> |
<window> | <window> | ||
<zscript><![CDATA[ | <zscript><![CDATA[ | ||
Line 162: | Line 164: | ||
</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. |
+ | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{{ZKDevelopersReferencePageFooter}} | {{ZKDevelopersReferencePageFooter}} |
Latest revision as of 04:35, 5 February 2024
This article is out of date, please refer to http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/Integration/Middleware_Layer/Spring for more up to date information.
Overview
Spring is a platform for building Java application, and it includes many easy-to-use solutions for building web-based application.
Here we discuss how to use Spring with ZK, especially the use of DelegatingVariableResolver. It provides the basic support of Spring which allows a ZUML document to access variables defined in Spring. For more comprehensive support, such as Spring scopes, annotations and security, please refer to another product: ZK Spring.
Installing Spring
First you have to install Spring to your Web application. If you are familiar with Spring, you could skip this section. In this section we use Spring core 3.0.2 and Spring Security 3.0.2.
Copy Spring binaries into your Web library
Before using Spring, you have to download it, and put the jar file into the directory of your web application.
- Download Spring Core framework 3.0.2 release binaries download
- org.springframework.aop-3.0.2.RELEASE.jar
- org.springframework.asm-3.0.2.RELEASE.jar
- org.springframework.beans-3.0.2.RELEASE.jar
- org.springframework.context-3.0.2.RELEASE.jar
- org.springframework.context.support-3.0.2.RELEASE.jar
- org.springframework.core-3.0.2.RELEASE.jar
- org.springframework.expression-3.0.2.RELEASE.jar
- org.springframework.transaction-3.0.2.RELEASE.jar
- org.springframework.web-3.0.2.RELEASE.jar
- org.springframework.web.servlet-3.0.2.RELEASE.jar
Put these jar files into your $myApp/WEB-INF/lib/
. Here $myApp
represents the name of your web application.
Configure web.xml
In your web.xml, you have to define org.springframework.web.context.ContextLoaderListener
, and to specify the location of the configuration file to load bean definitions.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Create Spring Configuration File
Define bean definitions in applicationContext.xml
file, and put it into your WEB-INF
directory.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="DataSource" class="test.DataSourceImpl"/>
</beans>
Creating Spring Bean Class
Then you have to define a DataSource interface and its implementation:
DataSource.java
package test;
public interface DataSource
{
java.util.List getElementsList();
}
DataSourceImpl.java
package test;
import java.util.*;
public class DataSourceImpl implements DataSource
{
public List getElementsList()
{
List list = new ArrayList();
list.add("Tom");
list.add("Henri");
list.add("Jim");
return list;
}
}
Accessing Spring Bean in the ZUML page
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
with 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.
Use with Composer
GenericAutowireComposer will wire the variables defined in the variable resolvers. Thus, we could declare DelegatingVariableResolver in the ZUML document, and then declare the Spring-managed bean as a control directly in a composer. For example,
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window apply="foo.MyComposer">
...
<textbox id="password"/>
...
<button id="submit" label="Change"/>
</window>
Then, if a data member's name matches a Spring-managed bean, it will be wired automatically too. For example,
public class PasswordSetter extends GenericFowardComposer {
private User user; //wired automatically if user is a spring-managed bean
private Textbox password; //wired automatically if there is a textbox named password
public void onClick$submit() {
user.setPassword(password.getValue());
}
}
For more information, please refer to MVC: Controller.
Using SpringUtil
org.zkoss.zkplus.spring.SpringUtil
is a utility class which allows you to get Spring-managed beans in Java code 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.