DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站.它可以允许在里的代码使用运行在上的JAVA函数,就像它就在浏览器里一样。
以下模拟一个简单的dwr入门案例,重点理解dwr是如何跟java后台服务器打交道的
模拟效果如下
该功能说明了dwr是怎么跟后台服务器打交道的
模拟从服务器加载下拉列表数据
模拟保存功能
模拟查询功能
接下来为dwr+spring集成步骤:
1、新建一个web工程,导入dwr+spring所需jar,如下图
目录结构图
修改web.xml
contextConfigLocation classpath:resource/app*.xml org.springframework.web.context.ContextLoaderListener dwrServlet org.directwebremoting.servlet.DwrServlet config /WEB-INF/classes/config/dwr.xml debug true dwrServlet /dwr/*
新建实体类Dept
package entity; publicclass Dept { private Long id; private String name; public Dept() { } public Dept(Long id, String name) { this.id = id; this.name = name; } public Long getId() { return id; } publicvoid setId(Long id) { this.id = id; } public String getName() { return name; } publicvoid setName(String name) { this.name = name; } }
新建业务逻辑类
DeptServices类
package services; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import entity.Dept; @SuppressWarnings("unchecked") publicclass DeptServices { public List findDept() { thrownew RuntimeException("查找失败!"); } publicvoid deleteDept(Long id) { System.out.println("Delete dept "+ id); } public List getDeptsForPo() { List depts =new ArrayList(); depts.add(new Dept(1l, "教质部")); depts.add(new Dept(2l, "学术部")); depts.add(new Dept(3l, "就业部")); depts.add(new Dept(4l, "咨询部")); return depts; } publicvoid saveDept(Listdepts) { // System.out.println(dept.getId() + ":" + dept.getName()); System.out.println(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 (InterruptedException e) { e.printStackTrace(); } return depts; } }
HelloServices类
package services; /** * @author dlwu * */ publicclass HelloServices { public String sayHello(String name){ System.out.println("Hello now!"); return"Hello "+ name +"!"; } }
LoginService类
package services; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; publicclass LoginService { publicvoid checkUserLogin(String userid, String pwd){ WebContext ctx = WebContextFactory.get(); ctx.getSession().setAttribute("userid", userid); } public String getLoginUser(){ WebContext ctx = WebContextFactory.get(); return (String)ctx.getSession().getAttribute("userid"); } }
配置applicationContext.xml
配置dwr.xml,dwr.xml是前台js跟java服务器沟通的桥梁
页面
hello.jsp页面
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>My JSP 'hello.jsp' starting page
dept.jsp页面
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>My JSP 'hello.jsp' starting page
ID: Name: