---
title: "DWR2.0+Spring2.0 : DWR2.0 与 Spring2.0的整合"
author: "步行者"
date: "2009-05-10T16:46:12Z"
tags: []
slug: "dwr2-0-spring2-0-dwr2-0-spring2-0"
draft: false
lastmod: "2009-06-19T21:27:24Z"
id: 382960
url: "https://yangjunfeng.iteye.com/blog/382960"
---

**DWR2.0+Spring2.0 : DWR2.0 与 Spring2.0的整合**

   本文重点介绍DWR2.0与Spring2.0的整合，在阅读本文之前，假设读者已经了解了 DWR 与 spring的基本原理。

   1，web.xml 中 对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>	
<filter>
	<filter-name>requestContextFilter</filter-name>
	<filter-class>
		org.springframework.web.filter.RequestContextFilter
	</filter-class>
</filter>
<filter-mapping>
	<filter-name>requestContextFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
```

 

 

2，web.xml 中 对DWR的配置如下：

```
<servlet>
	<servlet-name>dwr-invoker</servlet-name>
	<servlet-class>
		org.directwebremoting.spring.DwrSpringServlet
	</servlet-class>
	<init-param>
		<param-name>debug</param-name>
		<param-value>true</param-value>
	</init-param>
	<init-param>
		<param-name>crossDomainSessionSecurity</param-name>
		<param-value>true</param-value>
	</init-param>
	<init-param>
		<param-name>allowScriptTagRemoting</param-name>
		<param-value>true</param-value>
	</init-param>
</servlet>

<servlet-mapping>
	<servlet-name>dwr-invoker</servlet-name>
	<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
```

 

3，Spring配置文件中加入DWR支持

 

```
<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
     	http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    	 http://www.springframework.org/schema/aop 
    	 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
     	http://www.directwebremoting.org/schema/spring-dwr
    	 http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">
```

 

4，声明&lt;dwr:configuration&gt;元素

  注意：必须在Spring配置文件中声明&lt;dwr:configuration&gt;元素(作为&lt;beans&gt;的子元素)。

 

```
<dwr:configuration>
     ......
</dwr:configuration>
```

 

   也可以把这个元素置空。

 

```
<dwr:configuration/>
```

 

5,creator 和 converter 的 编写

   (1)  可以直接将creator和converter写到&lt;dwr:configuration&gt;元素里,如下所示：

```
<dwr:configuration>
	 <dwr:create javascript="JHello" type="spring" scope = "session">
	    	<dwr:param name="beanName" value = "hello"/>
		<dwr:include method = "Hello"/>
	 </dwr:create>
	<dwr:convert type="bean"
		class="org.test.People">
		<dwr:include method="name" />
		<dwr:include method="age" />
	</dwr:convert>
</dwr:configuration>
 <bean id="hello" class="org.test.Hello"> </bean>
```

 

  (2)将creator和converter写到&lt;bean&gt;元素里,下面的代码与(1)是等价的，但与Spring结合得更加优美

```
 <bean id="hello" class="org.test.Hello"
  	 scope="session">
  	<dwr:remote javascript="JHello">
   		<dwr:include method="hello" />
		<dwr:convert type="bean"
		                       class="org.test.People">
		              <dwr:include method="name" />
		              <dwr:include method="age" />
                                </dwr:convert>
 	</dwr:remote>
 </bean>
```

 

 spring与dwr整合的的官方资料：[http://directwebremoting.org/dwr/server/spring](http://directwebremoting.org/dwr/server/spring)