首先定义运行环境:
- jdk:1.8
- tomcat:8.5.11
- spring:4.3.9
- mybatis:3.4.4
- mybatis-spring:1.3.0
- dwr:3.0
注意此处的问题!!
若要使用dwr与spring整合的形式(dwr.xml文件废除,整合至spring的application配置文件中)参考:
参考博客,需要将spring的版本降至spring3.x.x,因为dwr组件并不支持spring4.x.x以上的版本。错误信息如下:
Java
.lang.NoSuchMethodError: org.springframework.util.ClassUtils.forName(Ljava/lang/String;)Ljava/lang/Class;
配置web.xml文件
<!-- 配置dwr -->
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
配置dwr.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<!-- 通用dwr配置 -->
<dwr>
<allow>
<!--
Spring中获取java对象
javascript属性表示为jsp页面引用的脚本名
creator属性是最重要的配置,设置为spring为通过spring创建对象实例,设置为new表示通过new创建对象实例
-->
<create javascript="dwrController" creator="spring">
<!--
name设置为beanName,value属性就设为spring中对应bean的id
name若设置为class,value属性就设为controller的路径
-->
<param name="beanName" value="dwrService"></param>
</create>
<!-- 自定义类转换器 -->
<convert converter="bean" match="com.alex.ssm.po.UsersPlus">
<param name="include" value="userid,useraddress,username,userphone,useryear,usersex,userrank"/>
</convert>
</allow>
</dwr>
配置applicationContext-service.xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd " >
<!-- 定义用户service -->
<bean id="userService" class="com.alex.ssm.service.impl.UserServiceImpl"/>
<!-- 定义dwrService -->
<bean id="dwrService" class="com.alex.ssm.controller.DwrController" />
</beans>
编写dwrController
package com.alex.ssm.controller;
import com.alex.ssm.po.UsersPlus;
import com.alex.ssm.service.impl.UserService;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.List;
/**
* Created by Alex on 2017/7/3.
* dwr的controller类
*/
@Controller
//需要标识为RemoteProxy才能被dwr调用
@RemoteProxy
public class DwrController {
@Autowired
private UserService userService;
//查找所有用户
@RemoteMethod
public List<UsersPlus> getAllUsers() throws Exception{
return userService.findAllUsers();
}
}
将dwr的js支持库加入页面
<%--DWR服务JS--%>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/interface/dwrController.js'></script>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/engine.js'></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/dwr/util.js"></script>
检测
运行tomcat,在地址栏输入localhost:8080/dwr 查看是否有你定义的controller,点击进去执行定义的方法execute。查看返回值是否正确(json类型的)