Spring与dwr整合 有两种方式
1、 在spring的配置文件中加入dwr的接口暴露
2、 用默认的方式
在web.xml中加入dwr的配置
Spring整合dwr花费了我整整两天的时间去尝试 ,去纠错到第二天晚上也就是现在
才调通程序
现在用第二种方式去整合spirng(mvc)和dwr
1、 先从web.xml开始
在web.xml里配置Listener
xml 代码如下:
<listener >
< listener-class > org.springframework.web.context.ContextLoaderListener listener-class >
< / listener >
如果在web.xml里给该Listener指定要加载的xml,如:
xml代码如下:
<!-- spring config -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
则会去加载相应的xml,而不会去加载/WEB-INF/下的applicationContext.xml。
但是,如果没有指定的话,默认会去/WEB-INF/下加载applicationContext.xml。
<!-- 首先 classpath是指 WEB-INF文件夹下的classes目录
存放class文件 对应的是项目开发时的src目录编译文件 总结:这是一个定位资源的入口
-->
Spring和dwr整合的时候必须要设置contextConfigLocation
和listener两个组件否则会报错,或者访问不到指定的类
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 在web.xml中通过contextConfigLocation配置spring,contextConfigLocation参数定义了要装入的 Spring 配置文件。如果要装载多个spring配置文件中间则用逗号(,)间隔 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml,/WEB-INF/ beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
<!-- spring前端控制器 -->
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 首先 classpath是指 WEB-INF文件夹下的classes目录
存放class文件 对应的是项目开发时的src目录编译文件 总结:这是一个定位资源的入口
-->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 处理中文乱码问题 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.directwebremoting.servlet.DwrListener</listener-class>
</listener>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!-- 每次请求开始时将request放入spring容器,请求结束时自动移除 -->
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
根据上面的配置信息。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml,/WEB-INF/ beans.xml</param-value>
</context-param>
需要把applicationContext.xml文件放在src下,因为在加载的时候myeclipse会自动放到WEB-INF的classes路径下面
把beans.xml文件放在项目的WEB-INF路径下
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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 开启注解扫描 spring-->
<!--<context:component-scan base-package="org.konghao.service"/>
--><!-- 开启RequestMapping映射 (spring mvc)-->
<mvc:annotation-driven/>
<!-- 视图处理器 -->
<bean id="jspView"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"
value="/WEB-INF/"/>
<property name="suffix"
value=".jsp"/>
</bean>
<!-- 现配先执行 ,后配后执行 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Throwable">main/error</prop>
</props>
</property>
</bean>
</beans>
Beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<bean id="helloService" class="org.konghao.service.HelloService"/>
</beans>
Dwr.xml的配置文件配置如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<create creator="spring" javascript="hello">
<param name="beanName" value="helloService"/>
<include method="say"/>
</create>
</allow>
</dwr>
测试页面的代码如下
<html>
<head>
<script type="text/javascript" src="<%=request.getContextPath() %>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/dwr/util.js"> </script>
<script type="text/javascript" src="<%=request.getContextPath() %>/dwr/interface/hello.js"></script>
<script type="text/javascript">
hello.say("zhang",function(data){
alert(data);
});
</script>
</head>
<body>
</body>
</html>