项目目录:
Dept.java
package com.ideatc.dwr.domain;
/**
* @ClassName: Dept
* @Description: 部门实体类
* @Company: IDEATC CO.,LTD
* @author dzy
* @date 2015-7-27 上午9:15:12
* @Version v1.0
*/
public class Dept {
private Long id;
private String name;
public Dept() {
super();
}
public Dept(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
DeptService.java
<span style="font-family: Arial, Helvetica, sans-serif;">package com.ideatc.dwr.service;</span>
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ideatc.dwr.domain.Dept;
/**
* @ClassName: DeptService
* @Description: 业务逻辑类
* @Company: IDEATC CO.,LTD
* @author dzy
* @date 2015-7-27 上午9:17:02
* @Version v1.0
*/
@SuppressWarnings("all")
public class DeptService {
public List findDept(){
throw new RuntimeException("查找失败!");
}
public void deleteDept(Long id){
System.out.println("Delete Dept " + id);
}
public List getDeptsForPo(){
List depts = new ArrayList();
depts.add(new Dept((long) 11,"教质部"));
depts.add(new Dept((long) 21,"学术部"));
depts.add(new Dept((long) 31,"就业部"));
depts.add(new Dept((long) 41,"咨询部"));
return depts;
}
public void saveDept(List<Dept> depts){
System.out.println("Save Dept " + depts);
}
public List getDepts(){
List depts = new ArrayList();
Map map = new HashMap();
map.put("id","01");
map.put("name", "教质部");
depts.add(map);
map = new HashMap();
map.put("id","02");
map.put("name", "学术部");
depts.add(map);
map = new HashMap();
map.put("id","03");
map.put("name", "就业部");
depts.add(map);
map = new HashMap();
map.put("id","04");
map.put("name", "咨询部");
depts.add(map);
try{
Thread.sleep(1000);
}catch(Exception e){
throw new RuntimeException(e);
}
return depts;
}
}
HelloService.java
package com.ideatc.dwr.service;
/**
* @ClassName: HelloServices
* @Description: TODO
* @Company: IDEATC CO.,LTD
* @author dzy
* @date 2015-7-27 上午9:27:12
* @Version v1.0
*/
public class HelloService {
public String sayHello(String name){
System.out.println("Hello now !");
return "Hello " + name + "!";
}
}
LoginService.java
package com.ideatc.dwr.service;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
/**
* @ClassName: LoginService
* @Description: TODO
* @Company: IDEATC CO.,LTD
* @author dzy
* @date 2015-7-27 上午9:28:21
* @Version v1.0
*/
public class LoginService {
public void checkUserLogin(String userid,String pwd){
WebContext webContext = WebContextFactory.get();
webContext.getSession().setAttribute("userid", userid);
}
public String getLoginUser(){
WebContext webContext = WebContextFactory.get();
return (String) webContext.getSession().getAttribute("userid");
}
}
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>
<!-- 建立JS对象,将目标对象的方法转换成JS对象的方法 -->
<create javascript="helloSrv" creator="new">
<param name="class" value="com.ideatc.dwr.service.HelloService"></param>
</create>
<!-- 从Spring中获取Java对象 -->
<create javascript="deptSrv" creator="spring">
<!-- value的值是spring容器中的bean的id -->
<param name="beanName" value="deptService"></param>
<!-- 禁止执行 -->
<exclude method="deleteDept"/>
</create>
<create javascript="loginSrv" creator="spring">
<param name="beanName" value="loginSrv"></param>
</create>
<!-- 指定针对于特定对象的转换器 -->
<convert match="com.ideatc.dwr.domain.*" converter="bean"></convert>
<convert match="java.lang.Throwable" converter="bean">
<param name="include" value="message"></param>
</convert>
</allow>
</dwr>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 配置系统基础环境 -->
<bean id="deptService" class="com.ideatc.dwr.service.DeptService"></bean>
<bean id="loginSrv" class="com.ideatc.dwr.service.LoginService"></bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<!-- spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/app*.xml</param-value>
</context-param>
<!-- spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置DWR前端控制器 -->
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<!-- 指定配置文件 -->
<init-param>
<param-name>config</param-name>
<!-- 加载dwr.xml文件,该文件位于src下的config文件夹中 -->
<param-value>/WEB-INF/classes/dwr/dwr.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
dept.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'dept.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/helloSrv.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
<script type="text/javascript">
function loadDept(){
//说明已经加载,不必重新加载
if($('depts').options.length > 0){
return ;
}
DWRUtil.addOptions('depts',[{id:'',name:'正在下载...'}],'id','name');
dwr.engine._execute("dwr",'deptSrv','getDepts',function(depts){
DWRUtil.removeAllOptions('depts');
DWRUtil.addOptions('depts',depts,'id','name');
});
}
function loadDept2(){
if($('depts2').options.length > 0){
return ;
}
DWRUtil.addOptions('depts2',[{id:'',name:'正在下载...'}],'id','name');
dwr.engine._execute("dwr",'deptSrv','getDeptsForPo',function(depts){
DWRUtil.removeAllOptions('depts2');
DWRUtil.addOptions('depts2',depts,'id','name');
});
}
function saveDept(){
//声明dept对象
var dept = {
id:$("deptid").value,
name:$("deptname").value
};
dwr.engine._execute("dwr",'deptSrv','saveDept',[dept],function(){
alert('保存成功!');
});
}
function find(){
dwr.engine._execute("dwr",'deptSrv','findDept',{
callback:function(results){
alert('查询成功!');
},
errorHandler:function(e){
alert("查询失败:" + e);
}
});
}
</script>
<body>
<select id="depts" οnclick="loadDept();"></select>
<select id="depts2" οnclick="loadDept2();"></select>
<hr/>
ID:<input id="deptid" type="text" size="8"/>
NAME:<input id="deptname" type="text" size="8"/>
<input value="保存部门" type="button" οnclick="saveDept();"/>
<input type="button" οnclick="find();" value="查找"/>
</body>
</html>
hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'hello.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<!-- 记得引入JS -->
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/interface/helloSrv.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/dwr/util.js"></script>
<script type="text/javascript">
function hello(){
//方法一
//返回处理后的结果信息
/* var fn = function(result){
$("msg").innerHTML = result;
}
helloService.sayHello($("name").value,fn); */
//方法二
helloSrv.sayHello($("name").value,function(result){
$("msg").innerHTML = result;
});
//方法三
//好处:不用导入上面的三个JS
//第一个参数:dwr访问路径,在web.xml中配置,如<url-pattern>/dwr/*</url-pattern>
//第二个参数:dwr与java服务器通信变量,在dwr.xml中声明
//第三个参数:服务器方法名
//第四个参数:页面请求参数,即服务器方法名的参数
//第五个参数:回调函数
// dwr.engine._execute("dwr","helloService","sayHello",$("name").value,fn);
}
</script>
<body>
<div id="msg"></div>
<input type="text" id="name" />
<input type="button" value="Hello" οnclick="hello();" />
</body>
</html>
完整项目:http://download.csdn.net/detail/yong472727322/8946317