1.web.xml中加入dwr配置如下:
<!-- dwr 配置 -->
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>org.directwebremoting.spring.DwrSpringServlet</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>
2.由spring接管dwr,配置spring的xml文件如下:
spring的xml中开头namespace加上drw标签配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName">
spring中配置dwr,如下:
<!-- dwr 配置 -->
<!-- 注意这里新增加的dwr tag, 为使其生效,文件头中要声明namespace -->
<dwr:configuration>
<dwr:convert type="bean" class="com.myweb.modal.ibatis.Month" />
</dwr:configuration>
<!-- 扫描读取注解加载的类 -->
<dwr:annotation-scan scanRemoteProxy="true" base-package="com.myweb.service" />
<dwr:url-mapping />
<!-- 部署项目时, 请把debug设为false -->
<dwr:controller id="dwrController" debug="true" />
3.配置dwr的service:
package com.myweb.service;
import java.util.List;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.springframework.beans.factory.annotation.Autowired;
import com.myweb.modal.ibatis.Month;
@RemoteProxy(creator=SpringCreator.class)
public class TestDwrService {
@Autowired
private MonthService monthService;
@RemoteMethod
public List<Month> test(){
List<Month> months = monthService.findIMonths();
return months;
}
}
4.配置页面:
<!-- struts2不推荐使用JSTL,如果要使用el表达式,请注意加入isELIgnored="false" -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
<%@ include file="/common/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type='text/javascript' src='${ctxPath}/dwr/engine.js'></script>
<script type='text/javascript' src='${ctxPath}/dwr/util.js'></script>
<script type='text/javascript' src='${ctxPath}/dwr/interface/TestDwrService.js'></script>
</head>
<body>
<a href="javascript:testDwr();">测试dwr</a>
</body>
<script type="text/javascript">
function testDwr(){
TestDwrService.test(function(data){
var detail = '';
for(var i=0;i<data.length;i++){
detail = detail + data[i].name+"花费:"+data[i].total+"元\r\n"
}
alert(detail);
});
}
</script>
</html>