首先定义运行环境:
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
org.directwebremoting.servlet.DwrServlet
debug
true
dwr
/dwr/*
配置dwr.xml文件
配置applicationContext-service.xml文件
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 " >
编写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 getAllUsers() throws Exception{
return userService.findAllUsers();
}
}
将dwr的js支持库加入页面
检测
运行tomcat,在地址栏输入localhost:8080/dwr 查看是否有你定义的controller,点击进去执行定义的方法execute。查看返回值是否正确(json类型的)