一 前言
spring3.0.5和zk5.0.5的集成,与spring和struts的集成原理是一样的。就是由spring来管理mvc里的业务逻辑层。他的好处实在太多了,下面笔者列出几条供大家参考:
1.可以由spring来控制zul对应的类,进行singleton和prototype的设置;
2.性能上的提高,如果使用了singleton,同时类中,没有属性同步的问题,则性能会明显提高;
3.页面编写简单,通过 ${} 来访问spring里配置的bean;
二 准备工作
1.环境
jdk1.6.0.23
tomcat6.0.30
eclipse3.6.1_javaee
2.包
1)zk5.0.5 CE
breeze.jar
bsh.jar
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
fckez.jar
groovy.jar
jruby.jar
js.jar
jython.jar
zcommon.jar
zcommons-el.jar
zhtml.jar
zk.jar
zkplus.jar
zul.jar
zweb.jar
2)zkspring插件
zkspring-core.jar
3)srping3.0.5
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
4)log4j
log4j-1.2.16.jar
commons-logging-1.1.1.jar
三 配置
1.web.xml,此处什么都没做,就是zk和spring的独立配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>zk5.0.5_spring3.0.5</display-name>
<!-- log4j配置,必须要在最前面 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webApp.root</param-value>
</context-param>
<!-- spring配置 -->
<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>
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- zk5.0.5 -->
<listener>
<description>
Used to cleanup when a session is destroyed</description>
<display-name>ZK Session cleaner</display-name>
<listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
</listener>
<servlet>
<description>
The ZK loader for ZUML pages</description>
<servlet-name>zkLoader</servlet-name>
<servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
<init-param>
<param-name>update-uri</param-name>
<param-value>/zkau</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<description>
The asynchronous update engine for ZK</description>
<servlet-name>auEngine</servlet-name>
<servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>zkLoader</servlet-name>
<url-pattern>*.zul</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>zkLoader</servlet-name>
<url-pattern>*.zhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>auEngine</servlet-name>
<url-pattern>/zkau/*</url-pattern>
</servlet-mapping>
<!-- 系统属性 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>index.zul</welcome-file>
</welcome-file-list>
</web-app>
2.log4j.properties
log4j.rootLogger=DEBUG,appender1
# org.springframework包下面所有的日志输出的级别设为DEBUG
log4j.logger.org.springframework=INFO
# 控制台输出
log4j.appender.appender1=org.apache.log4j.ConsoleAppender
log4j.appender.appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.appender1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss:SSS}[%p]: %m%n
# 立即输出
log4j.appender.appender1.immediateFlush=true
3.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="myData" class="com.phl.MyDataImpl"/>
<bean id="myTest" class="com.phl.MyTest" scope="singleton"/>
</beans>
四 核心部分
index.zul的部分,<window apply="${myTest}">为集成后的方式,集成前,我们是这样设置的<window apply="com.phl.MyTest">
<?xml version="1.0" encoding="utf-8"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window apply="${myTest}">
<grid>
<rows>
<row forEach="${myData.elementsList}">
<label value="${each}" />
</row>
</rows>
</grid>
<separator visible="true" />
<listbox>
<listhead>
<listheader label="测试数据" sort="auto" />
</listhead>
<listitem forEach="${myData.elementsList}">
<listcell label="${each}" />
</listitem>
</listbox>
<button id="btnOK" label="测试"/>
</window>
五 zk的犀利用法
下面的用法,也可以获取spring关联的类,但他不会执行构建等初始化方法,用起来,只是跟普通的类一样。所以不推荐使用。
<zscript><![CDATA[
//通过spring来获取单例形式的关联java类
import org.zkoss.zkplus.spring.SpringUtil;
import com.phl.*;
MyTest2 mt = (MyTest2) SpringUtil.getBean("myTest2");
]]></zscript>
六 总结
1.log4j正确配置
2.spring3.0.5可以独立使用
3.zk5.0.5可以独立使用
4.导入包zkspring-core.jar,就可以在zul页面里,通过 ${} 访问bean,进而得到集成的效果
5.包都比较好找,我这里传上一份lib为空的项目,随后完整的项目,我会逐步上传,请大家关注
6.spring3.0.5+zk5.0.5+hibernate3.6(jpa2.0)的集成,详细看完我的2篇文章,大家能够实现现在最流行的zsh架构了吧^-^
spring3.0.5下载地址http://www.springsource.org/download
zk5.0.5和zkspring-core下载地址http://www.zkoss.org/
log4j和commons下载地址:http://www.apache.org/
补充
1.增加ZK的犀利用法
2.更新若干错别字(2011-2-16)
版权所有-转载请说明出处.
作者简介
昵称:澳洲鸟
姓名:朴海林
QQ:85977328
MSN:
[email protected]