`

dwr学习 之 一、dwr+spring的简单集成

 
阅读更多

dwr学习 之 一、dwr+spring的简单集成
转自:http://limingnihao.iteye.com/blog/1074187



1. 环境搭建
我采用的环境为SpringMVC + myBatis + mySql + maven;

关于使用Eclipse构建Maven的SpringMVC项目,请参考: http://limingnihao.iteye.com/blog/830409.

关于mybatis,请参考:http://limingnihao.iteye.com/blog/781671



1.1 web.xml
         在配置好SpringMVC的环境之下,需要在web.xml中给Spring的dispatcher在添加一个dwr的servlet-mapping。

Xml代码  
1.<!-- spring --> 
2.<servlet> 
3.    <servlet-name>springDispatcher</servlet-name> 
4.    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
5.    <init-param> 
6.        <param-name>contextConfigLocation</param-name> 
7.        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> 
8.    </init-param> 
9.    <load-on-startup>1</load-on-startup> 
10.</servlet> 
11.<servlet-mapping> 
12.    <servlet-name>springDispatcher</servlet-name> 
13.    <url-pattern>*.do</url-pattern> 
14.</servlet-mapping> 
15. 
16.<!-- dwr设置 --> 
17.<servlet-mapping> 
18.    <servlet-name>springDispatcher</servlet-name> 
19.    <url-pattern>/dwr/*</url-pattern> 
20.</servlet-mapping> 


1.2 Spring配置文件
         添加dwr的namespace、dwr的controller、注解扫描等标签。给出配置文件全部内容如下:

Xml代码  
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<beans xmlns="http://www.springframework.org/schema/beans"  
3.    xmlns:aop="http://www.springframework.org/schema/aop"  
4.    xmlns:context="http://www.springframework.org/schema/context" 
5.    xmlns:mvc="http://www.springframework.org/schema/mvc"  
6.    xmlns:tx="http://www.springframework.org/schema/tx"  
7.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
8.    xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" 
9.    xsi:schemaLocation="http://www.springframework.org/schema/aop  
10.        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
11.        http://www.springframework.org/schema/beans  
12.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
13.        http://www.springframework.org/schema/context  
14.        http://www.springframework.org/schema/context/spring-context-3.0.xsd  
15.        http://www.springframework.org/schema/mvc  
16.        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
17.        http://www.springframework.org/schema/tx  
18.        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
19.        http://www.directwebremoting.org/schema/spring-dwr     
20.        http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"> 
21. 
22. 
23.    <aop:aspectj-autoproxy /> 
24.    <mvc:annotation-driven /> 
25.    <context:component-scan base-package="liming.student.manager" /> 
26. 
27.    <!-- 导入属性配置文件 --> 
28.    <context:property-placeholder location="classpath:mysql.properties" /> 
29.     
30.    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
31.        <property name="prefix" value="/WEB-INF/views/" /> 
32.        <property name="suffix" value=".jsp" /> 
33.    </bean> 
34. 
35.    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
36.        <property name="driverClassName" value="${jdbc.driverClassName}" /> 
37.        <property name="url" value="${jdbc.url}" /> 
38.        <property name="username" value="${username}" /> 
39.        <property name="password" value="${password}" /> 
40.    </bean> 
41. 
42.    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
43.        <property name="dataSource" ref="dataSource" /> 
44.    </bean> 
45. 
46.    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
47.        <property name="configLocation" value="classpath:mybatis-config.xml" /> 
48.        <property name="dataSource" ref="dataSource" /> 
49.    </bean> 
50. 
51.    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
52.        <property name="annotationClass" value="org.springframework.stereotype.Repository" /> 
53.        <property name="basePackage" value="liming.student.manager" /> 
54.        <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
55.    </bean> 
56. 
57.    <!-- DWR配置 --> 
58.    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
59.        <property name="order" value="1" /> 
60.    </bean> 
61.    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> 
62.        <property name="order" value="2" /> 
63.    </bean> 
64.    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 
65.    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> 
66.    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
67.        <property name="order" value="3" /> 
68.        <property value="true" name="alwaysUseFullPath"></property> 
69.        <property name="mappings"> 
70.            <props> 
71.                <prop key="/dwr/**">dwrController</prop> 
72.            </props> 
73.        </property> 
74.    </bean> 
75. 
76.    <dwr:controller id="dwrController" debug="true"> 
77.        <dwr:config-param name="allowScriptTagRemoting" value="true" /> 
78.        <dwr:config-param name="crossDomainSessionSecurity" value="false" /> 
79.    </dwr:controller> 
80.    <dwr:configuration></dwr:configuration> 
81.    <dwr:annotation-config /> 
82.    <dwr:annotation-scan scanRemoteProxy="true" scanDataTransferObject="true" base-package="liming.student.manager" /> 
83. 
84.</beans> 






2. 配置文件解说
2.1 controller
添加dwr的dwr:controller标签,debug为true时,可以访问/dwr/index.html的测试页面。还可以可以设置一些参数的值:

Xml代码  
1.<dwr:controller id="dwrController" debug="true"> 
2.    <dwr:config-param name="allowScriptTagRemoting" value="true" /> 
3.    <dwr:config-param name="crossDomainSessionSecurity" value="false" /> 
4.</dwr:controller> 
  
    controller常用配置:

参数名称
默认值
说明

jsonpEnabled
false
Set to true to enable DWR's JSONP remoting.

allowGetForSafariButMakeForgeryEasier
false
Set to true to make DWR work in Safari 1.x (where a bug drops the bodies from POST requests). POST requests are slightly harder to forge, so enabling this reduces security slightly.

crossDomainSessionSecurity
true
Set to false to enable requests from other domains. Note that enabling this can be a significant security risk. See the Wikipedia notes on CSRF for more. Do not set this to false without understanding the consequences.

allowScriptTagRemoting
true
Set to true to enable Script Tag remoting. Note that enabling this can be a significant security risk. See the Wikipedia notes on CSRF for more. Do not set this to false without understanding the consequences. There are some cases where you will need to enable Script Tag remoting, but want to leave crossDomainSessionSecurity in place - particularly when you have an http based web page, and an https based DWR service.

debug
false
Set to true to enable the debug/test pages.

scriptSessionTimeout
1800000 (30 mins)
How quickly do scriptSessions timeout?

maxCallCount
20
What is the maximum number of calls that can be done in a single batch. (Helps prevent DoS attacks).






2.2 url-mapping

         在Springmvc中,每个url都需要一个controller的映射器(RequestMapping),需要使用<dwr:url-mapping />标签进行声明,这样才可以访问/engine.js, /interface.js, /call/**, /interface/**路径。但是带来的不管是测试页(/dwe/index.html)将不能访问。解决办法是,声明一个SimpleUrlHandlerMapping的bean指定dwr的请求路径:

Xml代码  
1.<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
2.    <property name="order" value="3" /> 
3.    <property value="true" name="alwaysUseFullPath"></property> 
4.    <property name="mappings"> 
5.        <props> 
6.            <prop key="/dwr/**">dwrController</prop> 
7.        </props> 
8.    </property> 
9.</bean> 
  
2.3 configuration
<dwr:configuration/> 标签用于模仿在 dwr.xml 中可用的配置的行为。主要是声明实体类转换。例如:

Xml代码  
1.<dwr:configuration> 
2.    <dwr:convert type="bean" class="liming.student.manager.data.model.PlaceEntity"></dwr:convert> 
3.</dwr:configuration> 
  
2.4 remote
dwr:remote用于声明可以进行js调用的java bean:

Xml代码  
1.<bean class="liming.student.manager.web.dwr.DWRDemo"> 
2.    <dwr:remote javascript="DWRDemo"> 
3.        <dwr:include method="getPlaceBaseAllList"/> 
4.    </dwr:remote> 
5.</bean> 
  
2.5 annotation-config、annatation-scan
         当使用注解声明bean和实体类转换时,需要声明dwr:annotation-config、annotation-scan。

         @ RemoteProxy:注解定义为远程调用的类,可指定name,js调用的名称。

         @ RemoteMethod:可远程调用的方法。

         @DataTransferObject:实体类转换器。



annotation-config:启用DWR 扫描,在类路径中检测 @ RemoteProxy 与@ RemoteMethod注解的类。

       annotation-scan:定义一些注解扫描的参数:

                   base-package:指定的扫描的包;

                   regex:将扫描仪类路径中使用正则表达式;

                   scanRemoteProxy:DWR 是否扫描远程代理类?默认值为 true。

     scanDataTransferObject:DWR 是否扫描转换器?默认值为 true。

     scanGlobalFilter-默认值为 true。



Xml代码  
1.<dwr:annotation-config /> 
2.<dwr:annotation-scan scanRemoteProxy="true" scanDataTransferObject="true" base-package="liming.student.manager" /> 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics