springmvc和DWR的整合

12 篇文章 0 订阅
2 篇文章 0 订阅

首先定义运行环境:

  1. jdk:1.8
  2. tomcat:8.5.11
  3. spring:4.3.9
  4. mybatis:3.4.4
  5. mybatis-spring:1.3.0
  6. 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类型的)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值