dwr的初步配置和使用

前端之家收集整理的这篇文章主要介绍了dwr的初步配置和使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 

最近项目用到了一个叫dwr的框架,鉴于我刚看js也没几天,很多东西不明白,就先按照网上的资料配置了一个简单的使用dwr框架的web应用。

eclipse:Juno Service Release 2

tomcat: version-6.x

jdk: 6.x

先用eclipse建立一个动态web工程。

修改web.xml为


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  
    	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>dwr-invoker</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>login.jsp</welcome-file>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
		<welcome-file>test.html</welcome-file>
	</welcome-file-list>

</web-app>

然后建三个类

package com.charvis.model;

public class User {
	//登陆ID,主键唯一
	private String id;
	//姓名
	private String name;
	//口令
	private String password;
	//电子邮件
	private String email;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}


}

package com.charvis.dao;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.charvis.model.User;

public class UserDAO {
	private static Log log = LogFactory.getLog(UserDAO.class);
	//存放保存的数据
    private static Map dataMap = new HashMap();

    /*//持久用户
    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 boolean save(User user) {
      if (dataMap.containsKey(user.getId()))
        return false;
      log.info("下面开始保存用户");
      log.info("id:"+user.getId());
      log.info("password:"+user.getPassword());
      log.info("name:"+user.getName());
      log.info("email:"+user.getEmail());
      dataMap.put(user.getId(),user);
      log.info("用户保存结束");
      return true;
    }
    //查找用户
    public User find(String id) {
      return (User)dataMap.get(id);
    }

}

package com.charvis.dwr;

import com.charvis.dao.UserDAO;
import com.charvis.model.User;

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);
    }

}

新建一个test.html文件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>DWR测试</TITLE>
<Meta http-equiv=Content-Type content="text/html; charset=UTF-8">
<script type='text/javascript' src="dwr/engine.js"></script>
<script type='text/javascript' src="dwr/util.js"></script>
<script type='text/javascript' src="dwr/interface/DWRUserAccess.js"></script>
</HEAD>
<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>
<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>
</HTML>




然后再web-inf文件夹下面新建dwr.xml文件

<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
    "http://directwebremoting.org/schema/dwr30.dtd">

<dwr>
	<allow>
		<create creator="new" javascript="DWRUserAccess">
			<param name="class" value="com.charvis.dwr.DWRUserAccess" />
		</create>
		<convert converter="bean" match="com.charvis.model.User" />
	</allow>
</dwr>


研究一下可以发现,这个配置文件就是映射js和java类的。js中这个DWRUserAccess对象,应该就会被映射成com.charvis.dwr.DWRUserAcces这个类的对象。回调函数的参数data,就是com.charvis.model.User这个的对象。

试运行一下,在用户名那个地方输入一个。会显示注册成功。这儿我试了一下commons-logging,当然还可以用log4j了。

原文链接:https://www.f2er.com/ajax/166082.html

猜你在找的Ajax相关文章