(转)spring与DWR集成的两种步骤
1。第一种:
在这种方式中,不需要dwr.xml文件,将这部分配置与Spring的配置写在同一个文件中。DWR使用的servlet为:org.directwebremoting.spring.DwrSpringServlet,这样,只需要两个配置文件就可以了,分别是:
web.xml
<?xml version="1.0" encoding="utf-8"?>
<!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>
2)在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: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>
2.第二种
第二种配置方式
使用第一种方式的Spring配置文件看起来比较乱,不那么干净,在这种方式中,将Spring的配置与DWR的配置完全分开,看起来干净利落。DWR使用的servlet为:org.directwebremoting.servlet.DwrServlet ,这样就需要三个配置文件了:
web.xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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配置文件:
<?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: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容器中的对象:
<?xml version="1.0" encoding="UTF-8"?>
<!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>