Dwr第二种整合方式

与spring mvc整合

不要dwr.xml文件 全部在spring配置文件中完成  

Spring-core和spring-asm包有冲突 ,应该删除spring-asm这个包

                                                                                                                                            

讲解都在代码之中

 

Web.xml中不要dwr的配置 ,让spring-mvc接收全部的请求

<?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的配置文件

       还在下面装载相同的配置文件的话,有的时候就会报       "重复的名称找到的。查看日志的详细信息。Duplicate name found. See logs for details."  这个异常

       这是因为在初始加载spring文件的时候加载了一个文件,并且在后面加载spring-mvc文件的时候加载了相同的文件,扫描了两次相同的文件所以会报错     

     -->

    <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath:beans.xml</param-value>

    </context-param>

 

   

    <!-- spring前端控制器 -->

    <listener>

       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

   

   

<servlet>

    <servlet-name>springmvc</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!-- 为了避免与上面的冲突,如果把这个初始化参数去掉的话,它会自动扫描/WEB-INF/springmvc-servlet.xml这个文件 ,

    而如果这个spring-mvc配置文件不存在的话 ,初始化项目的时候会报文件找不到异常

    所以:web.xml下的contextConfigLocation参数的值只是spring的配置文件,而这个参数则设置spring-mvc的配置文件即可

    -->

    <init-param>

        <param-name>contextConfigLocation</param-name>

        <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>

    <servlet-mapping>

    <servlet-name>springmvc</servlet-name>

    <url-pattern>/dwr/*</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>

 

 

 

 

  <!-- 每次请求开始时将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>

 

 

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"

    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-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

       http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">

 

    <!-- 开启注解扫描 spring-->

    <!--<context:component-scan base-package="org.konghao.service"/>

    -->

   

<!-- 打开dwr的控制器 -->

    <dwr:controller id="dwrController" debug="true"/>

   

    <!-- 在spring中引入dwr的控制器 -->

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

       <property value="true" name="alwaysUseFullPath" />

       <property name="mappings">

           <props>

              <prop key="/dwr/**/*">dwrController</prop>

           </props>

       </property>

    </bean>   

    <!-- 如果没有用annotation的方法则在这里配置  ,已经把这个bean信息移动到beans.xml文件当中了 -->

    <!--<bean id="helloService" class="org.konghao.service.HelloService">

       <dwr:remote javascript="hello">

           <dwr:include method="load"/>

       </dwr:remote>

    </bean>

   

    --><!-- dwr的转换器 -->

    <dwr:configuration>

       <dwr:convert  type="bean" class="org.konghao.service.User"></dwr:convert>

    </dwr:configuration>

   

   

    <!-- 开启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"

 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.5.xsd

                    http://www.springframework.org/schema/context

                    http://www.springframework.org/schema/context/spring-context-2.5.xsd

                    http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">

    <bean id="helloService" class="org.konghao.service.HelloService">

       <dwr:remote javascript="hello">

           <dwr:include method="load"/>

       </dwr:remote>

    </bean>

</beans>

 

测试文件代码如下dwr01.jsp

<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.load(function(data){

           alert(data.name);

       });

    </script>

 

  </head>

  <body>

  </body>

</html>