DWR(Direct Web Remoting)js调用java方法
DWR(Direct Web Remoting)是一个用于改善web页面与java类交互的远程服务器端Ajax开源框架。(JavaScript访问java类中的方法)
1. 配置web.xml
<!-- 配置dwr处理器 --> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class> org.directwebremoting.servlet.DwrServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
User.javapackage lynn; public class User { // 登陆ID,主键唯一 private String id; // 姓名 private String name; // 口令 private String password; // 电子邮件 private String email;//各属性的get和set方法... ...UserDAO.javapackage lynn; import java.util.HashMap;import java.util.Map; public class UserDAO { // 存放保存的数据 private static Map<String, User> dataMap = new HashMap<String, User>(); // 持久用户 public boolean save(User user) { if (dataMap.containsKey(user.getId())) return false; System.out.println("下面开始保存用户"); System.out.println("id:" + user.getId()); System.out.println("password:" + user.getPassword()); System.out.println("name:" + user.getName()); System.out.println("email:" + user.getEmail()); dataMap.put(user.getId(), user); System.out.println("用户保存结束"); return true; } // 查找用户 public User find(String id) { return (User) dataMap.get(id); }}
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd"> <dwr> <allow> <create creator="new" javascript="DWRUserAccess"> <param name="class" value="lynn.DWRUserAccess" /> </create> <convert converter="bean" match="lynn.User" /> </allow></dwr>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD> <TITLE>DWR测试</TITLE> <meta http-equiv=Content-Type content="text/html; charset=gb2312"> <script src="dwr/engine.js"></script> <script src="dwr/util.js"></script> <script src="dwr/interface/DWRUserAccess.js"></script> </HEAD> <SCRIPT LANGUAGE="JavaScript">function saveFun(data) { if (data) { alert("注册成功!"); } else { alert("登陆ID已经存在!"); }} function OnSave() { var userMap = {}; userMap.id = regForm.id.value; userMap.password = regForm.password.value; userMap.name = regForm.name.value; userMap.email = regForm.email.value; DWRUserAccess.save(userMap, saveFun);} function findFun(data) { if (data == null) { alert("无法找到用户:" + queryForm.id.value); return; } alert("找到用户,nid:" + data.id + ",npassword:" + data.password + ",nname:" + data.name + ",nemail:" + data.email);} function OnFind() { DWRUserAccess.find(queryForm.id.value, findFun);}</SCRIPT> <BODY> <B>用户注册</B> <br> ------------------------------------------------ <Br> <form name="regForm"> 登陆ID: <input type="text" name="id"> <br> 口 令: <input type="password" name="password"> <br> 姓 名: <input type="text" name="name"> <br> 电子邮件: <input type="text" name="email"> <br> <input type="button" name="submitBtn" value="提交" onclick="OnSave()"> <br> </form> <br> <br> <B>用户查询</B> <br> ------------------------------------------------ <Br> <form name="queryForm"> 登陆ID: <input type="text" name="id"> <br> <input type="button" name="submitBtn" value="提交" onclick="OnFind()"> <br> </form> <br> </BODY></HTML>
function saveFun(data) { if (data) { alert("注册成功!"); } else { alert("登陆ID已经存在!"); }} function OnSave() { var userMap = {}; userMap.id = regForm.id.value; userMap.password = regForm.password.value; userMap.name = regForm.name.value; userMap.email = regForm.email.value; DWRUserAccess.save(userMap, saveFun);}
<!--初始化WebApplicationContext--> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class></listener>