1.首先创建实体类 User
public class User
{
private String id;
private String name;
private String password;
private String email;
//继续写getters and setters
2.编写DAO
public class UserDAO
{
private static Map<String, User> dataMap = new HashMap<String, User>();
//储存user到datMap中
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;
}
//根据id查找user
public User find(String id)
{
return (User)dataMap.get(id);
}
//查看datamap的总数
public int totalNum()
{
System.out.println(dataMap.size());
return dataMap.size();
}
public Map<String,User> findAll()
{
// User u1 = new User();
// u1.setId("1");
// u1.setName("2");
// u1.setEmail("3");
// u1.setPassword("4");
// List<User> l = new ArrayList<User>();
// l.add(u1);
return dataMap;
}
}
3.用于JS调用的接口
public class DWRUserAccess
{
UserDAO userDAO = new UserDAO();
public boolean save(User user) {
return userDAO.save(user);
}
public User find(String id) {
return userDAO.find(id);
}
public String ss(){
System.out.println("aaaa");
return "songnan";
}
public int totalNum()
{
return userDAO.totalNum();
}
public Map<String,User> findAll()
{
// User u1 = new User();
// u1.setId("1");
// u1.setName("2");
// u1.setEmail("3");
// u1.setPassword("4");
// List<User> l = new ArrayList<User>();
// l.add(u1);
return userDAO.findAll();
}
}
4.开始写前台,首先引入dwr.jar,然后增加dwr.xml并添加如下内容
<?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>
<convert match="java.lang.StackTraceElement" converter="bean"></convert>
<!--创建javabean-->
<convert converter="bean" javascript="User" match="pojo.User">
<param name="include" value="id,name,password,email">
</param>
</convert>
<!--将DWRUserAccess实例化-->
<create creator="new" javascript="DWRUserAccess">
<param name="class" value="dwr.DWRUserAccess"/>
</create>
</allow>
</dwr>
5.修改web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<!-- 固定这么写 -->
<web-app id="dwr">
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
6.开始写页面
<!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>
<script src="jquery-1.6.3.js"></script>
</HEAD>
<BODY>
<B>用户注册</B>
<br>
------------------------------------------------
<Br>
<form name="regForm">登陆ID:<input type="text" name="id" id="id"><br>
口 令:<input type="password" name="password" id="password"><br>
姓 名:<input type="text" name="name" id="name"><br>
电子邮件:<input type="text" name="email" id="email"><br>
<input type="button" name="submitBtn" value="提交" οnclick="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="提交" οnclick="OnFind()"><br>
</form>
<br>
<input type="text" name="gg" id="gg">
<input type="button" name="aa" id="aa" οnclick="nice()">
<input type="button" name="aa" id="aa" οnclick="totalNum()">
<input type="button" name="aa" id="aa" οnclick="findAlll()">
</BODY>
</HTML>
<SCRIPT LANGUAGE="JavaScript">
function nice()
{
alert($("input#gg").val());
DWRUserAccess.ss();
}
function totalNum()
{
DWRUserAccess.totalNum();
}
function findAlll()
{
DWRUserAccess.findAll(callBackFortestMethod6);
}
function callBackFortestMethod6(data)
{
alert(data.getvalue("admin"));
//for(i = 0;i<data.length;i++)
//{
//alert(data[id].)
//}
}
function saveFun(data)
{
alert(data);
if (data)
{
alert("注册成功!");
}
else
{
alert("登陆ID已经存在!");
}
}
function OnSave()
{
var userMap = {};
userMap.id = $("input#id").val();
userMap.password = $("input#password").val();
userMap.name = $("input#name").val();
userMap.email = $("input#email").val();
DWRUserAccess.save(userMap, saveFun);
}
function findFun(data)
{
if (data == null)
{
alert("无法找到用户:"+$("input#id").val());
return;
}
alert("找到用户,nid:"+data.id+",npassword:"+data.password+",nname:"+data.name+",nemail:"+data.email);
}
function OnFind()
{
DWRUserAccess.find(queryForm.id.value, findFun);
}
</SCRIPT>