Spring与DWR集成的两种配置方式
关于Ajax的框架,以前用的是Buffalo,但是DWR比较热,于是配置一下DWR玩一玩。现在的形势是:无论弄个什么框架,如果不和Spring集成一下,都不好意思跟别人打招呼。
在这里使用的Spring和DWR都是最新的版本:
Spring版本: 2.5.5
DWR版本:2.0.5
首先来一个测试页面,这是一个静态页面,在这个页面中调用java.util.Date的toString()方法。使用java.util.Date比较简单,这样除了用到了Spring和DWR的类库外,不用写任何Java代码既可以达到演示的目的。
<head>
<title>www.simplelife.cn</title>
<script type=’text/javascript’ src=’dwr/interface/javaDate.js’></script>
<script type=’text/javascript’ src=’dwr/engine.js’></script>
<script type=’text/javascript’ src=’dwr/util.js’></script>
</head>
<body>
<input type=’button’ on-click=’javaDate.toString(reply);’ value=’show time’/>
<script type=’text/javascript’>
var reply = function(data)
{
if (data != null && typeof data == ’object’)
alert(dwr.util.toDescriptiveString(data, 2));
else
dwr.util.setValue(’d0′, dwr.util.toDescriptiveString(data, 1));
}
</script>
<br>
<span id=’d0′></span>
</body>
</html>
接下来有两种配置Spring与DWR集成的方式:
第一种配置方式
在这种方式中,不需要dwr.xml文件,将这部分配置与Spring的配置写在同一个文件中。DWR使用的servlet为:org.directwebremoting.spring.DwrSpringServlet,这样,只需要两个配置文件就可以了,分别是:
web.xml:
<!DOCTYPE web-app PUBLIC
“-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
“http://java.sun.com/dtd/web-app_2_3.dtd”>
<web-app id=”simplelife.cn”>
<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>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<description>Direct Web Remoter Servlet</description>
<servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
<!– This should NEVER be present in live –>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<!– Remove this unless you want to use active function reverse() {
[native code]
} ajax –>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<!– By default DWR creates application scope objects when they are first
used. This creates them when the app-server is started –>
<init-param>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<!– This enables full streaming mode. It’s probably better to leave this
out if you are running across the internet –>
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>-1</param-value>
</init-param>
<!–
For more information on these parameters, see:
- http://getahead.org/dwr/server/servlet
- http://getahead.org/dwr/function reverse() {
[native code]
}-ajax/configuration
–>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
另外一个文件是applicationContext.xml文件:
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:p=”http://www.springframework.org/schema/p”
xmlns:dwr=”http://www.directwebremoting.org/schema/spring-dwr”
xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd”>
<bean id=”dwrJavaDate” class=”java.util.Date”>
<dwr:remote javascript=”javaDate”>
</dwr:remote>
</bean>
<dwr:configuration></dwr:configuration>
<dwr:controller id=”dwrController” debug=”true“ />
</beans>
注意配置文件中的黑体部分。
第二种配置方式
使用第一种方式的Spring配置文件看起来比较乱,不那么干净,在这种方式中,将Spring的配置与DWR的配置完全分开,看起来干净利落。DWR使用的servlet为:org.directwebremoting.servlet.DwrServlet ,这样就需要三个配置文件了:
web.xml文件:
<!DOCTYPE web-app PUBLIC
“-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
“http://java.sun.com/dtd/web-app_2_3.dtd”>
<web-app id=”simplelife.cn”>
<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>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<description>Direct Web Remoter Servlet</description>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<!– This should NEVER be present in live –>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<!– Remove this unless you want to use active function reverse() {
[native code]
} ajax –>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<!– By default DWR creates application scope objects when they are first
used. This creates them when the app-server is started –>
<init-param>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<!– This enables full streaming mode. It’s probably better to leave this
out if you are running across the internet –>
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>-1</param-value>
</init-param>
<!–
For more information on these parameters, see:
- http://getahead.org/dwr/server/servlet
- http://getahead.org/dwr/function reverse() {
[native code]
}-ajax/configuration
–>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml文件,这是一个纯粹的Spring配置文件:
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:p=”http://www.springframework.org/schema/p”
xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd”>
<bean id=”dwrJavaDate” class=”java.util.Date”>
</bean>
</beans>
最后一个文件是dwr.xml,在这个文件中,使用spring容器中的对象:
<!DOCTYPE dwr PUBLIC ”-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN”
“http://getahead.org/dwr/dwr20.dtd”>
<dwr>
<allow>
<create creator=”spring” javascript=”javaDate”>
<param name=”beanName” value=”dwrJavaDate”/>
</create>
</allow>
</dwr>
以上两种方式都可以完成Spring与DWR的集成,我比较喜欢第二种,“上帝的归上帝,恺撒的归恺撒”。
如果再需要配置MVC等其他功能,直接在web.xml中声明另外的Servlet即可,就合DWR没什么关系了。
附录:目录结构
-rw-r–r– 1 javor javor 663 2008-10-14 16:58 index.html
drwxr-xr-x 3 javor javor 4096 2008-10-14 17:52 WEB-INF
javor@desktop:/data/servers/apache-tomcat-5.5.25/webapps/simplelife$ ls -l WEB-INF/lib
total 12076
-rw-r–r– 1 javor javor 188671 2008-10-14 16:36 commons-beanutils.jar
-rw-r–r– 1 javor javor 571259 2008-10-14 16:36 commons-collections.jar
-rw-r–r– 1 javor javor 121757 2008-10-14 16:36 commons-dbcp.jar
-rw-r–r– 1 javor javor 38015 2008-10-14 16:36 commons-logging-1.0.4.jar
-rw-r–r– 1 javor javor 60841 2008-10-14 16:36 commons-logging.jar
-rw-r–r– 1 javor javor 62103 2008-10-14 16:36 commons-pool.jar
-rw-r–r– 1 javor javor 84462 2008-10-14 16:36 commons-validator-1.1.4.jar
-rw-r–r– 1 javor javor 84462 2008-10-14 16:36 commons-validator.jar
-rw-r–r– 1 javor javor 502402 2008-10-14 16:36 dwr.jar
-rw-r–r– 1 javor javor 65261 2008-10-14 16:36 jakarta-oro-2.0.8.jar
-rw-r–r– 1 javor javor 358085 2008-10-14 16:36 log4j-1.2.12.jar
-rw-r–r– 1 javor javor 2949316 2008-10-14 16:36 spring.jar
-rw-r–r– 1 javor javor 404466 2008-10-14 16:36 spring-webmvc.jar
-rw-r–r– 1 javor javor 393259 2008-10-14 16:36 standard.jar
[END]