dwr学习 之 一、dwr+spring的简单集成
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的简单集成的更多相关文章
- 【DWR系列04】- DWR配置详解
table { margin-left: 30px; width: 90%; border: 1px; border-collapse: collapse } img { border: 1px so ...
- Spring cache简单使用guava cache
Spring cache简单使用 前言 spring有一套和各种缓存的集成方式.类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. [TOC ...
- 【DWR系列03】- DWR主要类详解
img { border: 1px solid black } 一.简介 首先应该了解这个jar包主要的类,了解了类,就了解了DWR.DWR的在线javadoc:http://directwebrem ...
- 【DWR系列02】-DWR逆向Ajax即服务器推送
.literal { background-color: #f2f2f2; border: 1px solid #cccccc; padding: 1px 3px 0; white-space: no ...
- 【DWR系列01】-DWR简介及入门例子
.literal { background-color: #f2f2f2; border: 1px solid #cccccc; padding: 1px 3px 0; white-space: no ...
- 深入学习微框架:Spring Boot(转)
转:http://www.infoq.com/cn/articles/microframeworks1-spring-boot/ 相关参考: https://spring.io/guides/gs/s ...
- Quartz学习——Spring和Quartz集成详解(三)
Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度.下面就对Spring集成Quartz进行简单的介绍和示例讲解!和上一节 Quar ...
- spring 学习(四): spring 的 jdbcTemplate 操作
spring 学习(四): spring 的 jdbcTemplate 操作 spring 针对 javaee 的每一层,都提供了相应的解决技术,jdbcTemplate 的主要操作在 dao 层. ...
- Spring Framework简单介绍
Spring Framework 学习java编程不知不觉已经三年时间了,開始的时候,总是喜欢看着视频,然后按部就班的敲打着键盘,每当系统正常执行后.心里乐开了花.最開始的时候,所有的代 ...
随机推荐
- 下载Google官方/CM Android源码自己主动又一次開始的Shell脚本
国内因为某种原因,下载CM或Google官方的Android源码总easy中断.总看着机器.一中断就又一次运行repo sync还太麻烦,所以我特意编写了一段shell脚本(download.sh). ...
- 基于DM642 RAW采集格式的视频驱动开发及应用
摘 要:为解决C64X系列数字信号处理器(DSP)视频驱动不能应用于原始数据格式(RAW)采集格式的问题,设计了DM642和电耦合元件(CCD)高清传感器的数据传输接口,并分析.修改用于标准格式的视频 ...
- 三种方法打印 main函数的返回地址的值(old EIP)(用途,你懂得!)
这里能够简单的改动随意函数的返回地址.能够做到自己定义EIP的指向,就可以运行当前进程空间的随意指令,这里仅仅是让大家更清楚栈帧结构,没有涉及跨进程的inline HOOK 等,后面会陆续讲下读取随意 ...
- STM32唯一身份识别ID(器件电子签名)的读取以及芯片Flash大小读取
每个STM32有一个独立的ID,这个ID可以用来: 产品唯一的身份标识的作用: ● 用来作为序列号(例如USB字符序列号或者其他的终端应用): ● 用来作为密码,在编写闪存时,将此唯一 ...
- Erlang Garbage Collector
Erlang Garbage Collector | Erlang Solution blog https://www.erlang-solutions.com/blog/erlang-garbage ...
- QT实现FTP服务器(一)
QListenSocket类的实现: #include "QListenSocket.h" #include <QTcpSocket> #include <QDe ...
- hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1584 : Bounce 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 For Argo, it is very interesting watching a cir ...
- vue中如何实现后台管理系统的权限控制
vuejs单页应用的权限管理实践 一.前言 在广告机项目中,角色的权限管理是卡了挺久的一个难点.首先我们确定的权限控制分为两大部分,其中根据粒的大小分的更细: 接口访问的权限控制 页面的权限控制 菜单 ...
- ap和路由器有什么区别 ap和路由器的区别介绍【图文】
现在能够摆脱网线限制能够自由方便上网的WiFi和无线网络也来越流行,很多酒店.饭店.宾馆.办公楼等地方都会提供无线网络.而能够提供无线网络的设备有很多,现在我们介绍的是无线ap和无线路由器.那么,ap ...
- PHP——巧用PHP函数array_merge()合并数组
前言 返回联系人列表,包含所有的字母,之前返回的是存在这个联系人才会返回对应的大写字母,后面更改了要求要返回所有的字母从A-Z.PHP内置的一些函数活用起来,真的很省劲! 步骤 一般这时候大家可能直接 ...