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代码:

     <!-- spring -->
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- dwr设置 -->
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

1.2 Spring配置文件

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

Xml代码:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"> <aop:aspectj-autoproxy />
<mvc:annotation-driven />
<context:component-scan base-package="liming.student.manager" /> <!-- 导入属性配置文件 -->
<context:property-placeholder location="classpath:mysql.properties" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="annotationClass" value="org.springframework.stereotype.Repository" />
<property name="basePackage" value="liming.student.manager" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> <!-- DWR配置 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="order" value="2" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="order" value="3" />
<property value="true" name="alwaysUseFullPath"></property>
<property name="mappings">
<props>
<prop key="/dwr/**">dwrController</prop>
</props>
</property>
</bean> <dwr:controller id="dwrController" debug="true">
<dwr:config-param name="allowScriptTagRemoting" value="true" />
<dwr:config-param name="crossDomainSessionSecurity" value="false" />
</dwr:controller>
<dwr:configuration></dwr:configuration>
<dwr:annotation-config />
<dwr:annotation-scan scanRemoteProxy="true" scanDataTransferObject="true" base-package="liming.student.manager" /> </beans>

2. 配置文件解说

2.1 controller

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

Xml代码:

 <dwr:controller id="dwrController" debug="true">
<dwr:config-param name="allowScriptTagRemoting" value="true" />
<dwr:config-param name="crossDomainSessionSecurity" value="false" />
</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的请求路径:

dwr学习 之 一、dwr+spring的简单集成的更多相关文章

  1. 【DWR系列04】- DWR配置详解

    table { margin-left: 30px; width: 90%; border: 1px; border-collapse: collapse } img { border: 1px so ...

  2. Spring cache简单使用guava cache

    Spring cache简单使用 前言 spring有一套和各种缓存的集成方式.类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. [TOC ...

  3. 【DWR系列03】- DWR主要类详解

    img { border: 1px solid black } 一.简介 首先应该了解这个jar包主要的类,了解了类,就了解了DWR.DWR的在线javadoc:http://directwebrem ...

  4. 【DWR系列02】-DWR逆向Ajax即服务器推送

    .literal { background-color: #f2f2f2; border: 1px solid #cccccc; padding: 1px 3px 0; white-space: no ...

  5. 【DWR系列01】-DWR简介及入门例子

    .literal { background-color: #f2f2f2; border: 1px solid #cccccc; padding: 1px 3px 0; white-space: no ...

  6. 深入学习微框架:Spring Boot(转)

    转:http://www.infoq.com/cn/articles/microframeworks1-spring-boot/ 相关参考: https://spring.io/guides/gs/s ...

  7. Quartz学习——Spring和Quartz集成详解(三)

    Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度.下面就对Spring集成Quartz进行简单的介绍和示例讲解!和上一节 Quar ...

  8. spring 学习(四): spring 的 jdbcTemplate 操作

    spring 学习(四): spring 的 jdbcTemplate 操作 spring 针对 javaee 的每一层,都提供了相应的解决技术,jdbcTemplate 的主要操作在 dao 层. ...

  9. Spring Framework简单介绍

    Spring Framework        学习java编程不知不觉已经三年时间了,開始的时候,总是喜欢看着视频,然后按部就班的敲打着键盘,每当系统正常执行后.心里乐开了花.最開始的时候,所有的代 ...

随机推荐

  1. HBase技术简介

    一.HBase简介 HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. ...

  2. Pascal Hexagrammum Mysticum 的深度探索

        PASCAL . Hexagrammum Mysticum . (六角迷魂图) . 的深度探索 . 英中对比.英文蓝色,译文黑色,译者补充说明用紫红色 (已校完,但尚未定稿,想再整理并补充内容 ...

  3. 12.怎样自学Struts2发送邮件和验证补充[视频]

    12.怎样自学Struts2发送邮件和验证补充[视频] 之前写了一篇"打算做一个视频教程探讨怎样自学计算机相关的技术",优酷上传不了,仅仅好传到百度云上: http://pan.b ...

  4. HDU 3639 Hawk-and-Chicken

    Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. 服务器返回JSON,IE出现下载问题

    我向来的观点,IE就是个奇葩. 服务器返回json,chrome处理得好地地,但IE却奇葩地向你请求是否要保存这个JSON文件? 之所以出现这种弱智现象,是因为IE无法识别一个所谓的响应头部:appl ...

  6. xcode4中build Settings常见参数解析

    本文转载至 http://shiminghua234.blog.163.com/blog/static/263912422012411103526386/     1.Installation Dir ...

  7. pyspark 连 MongoDB复制集

    解决问题思路: 核心:0-理解pyspark的执行与java jar的关系: 1-看控制台,看日志: 2-jar缺不缺,版本号,放哪里. [root@hadoop1 mylocalRepository ...

  8. 解决ubuntu10.04不能上网

    1:命令行输入:lspci查看驱动,最后几行如果有ethernet controller:atheros communications ar8151 v1.0*的话,就说明驱动没有安装好, 2:下载地 ...

  9. HDU1565 方格取数(1) —— 状压DP or 插头DP(轮廓线更新) or 二分图点带权最大独立集(最小割最大流)

    题目链接:https://vjudge.net/problem/HDU-1565 方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory L ...

  10. poj 1274 The Perfect Stall 解题报告

    题目链接:http://poj.org/problem?id=1274 题目意思:有 n 头牛,m个stall,每头牛有它钟爱的一些stall,也就是几头牛有可能会钟爱同一个stall,问牛与 sta ...