dwr的用法

前端之家收集整理的这篇文章主要介绍了dwr的用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
dwr属于ajax的一个框架 所以说实现原理和ajax一样
dwr使用步骤:
1.导入dwr所用的jar包文件 dwr.jar和commons-logging.jar
2.在web.xml文件中配置dwr默认的信息 (即默认的servlet)
			<servlet>
				<servlet-name>dwr-invoker</servlet-name>
				<servlet-class>org.directwebremoting.servlet.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>

3.在web.xml文件的同一目录下面创建dwr.xml文件 并且在dwr.xml文件配置在javascript中需要调用的普通的java类(不是servlet)
4.检查一下dwr配置是否成功 检查方式:打开服务器 然后在浏览器中输入http://localhost/mydwr/dwr
5.在需要使用dwr的页面 导入三个js文件 然后就可以在该页面的javascript代码中直接通过名字调用到java类中的方法

dwr.xml文件
普通(非单例模式的类)的配置
		<create creator="new" javascript="dwrStudy">
			<param name="class" value="com.pk.mydwr.test.DwrStudy" />
		</create>


单例模式的类的配置
a).首先要导入单例模式需要的一些jar包
b).然后配置方式如下
			<create creator="script" javascript="chinaCitySingletonDao">
				  <param name="language" value="beanshell"/>
				    <param name="script">
				      import com.puckasoft.dao.ChinaCitySingletonDao;
				      return ChinaCitySingletonDao.getInstance();
				   </param>
			</create>


例子:已局部刷新邮箱是否注册为例

dwr.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
	<allow>
		<!-- DwrStudy dwrStudy = new DwrStudy();dwrStudy是用在javascript中的
			即在javascript中使用dwrStudy就相当于调用了DwrStudy类的实例
		 -->
		
<!--非单例配置
<create creator="new" javascript="userDao">
<param name="class" value="com.pk.mydwr.dao.UserDao" />
</create>
-->
<create creator="script" javascript="userDao"> <param name="language" value="beanshell"/> <param name="script"> import com.pk.mydwr.dao.UserDao; return UserDao.getInstance(); </param></create><convert match="com.pk.mydwr.po.User" converter="bean"></convert></allow></dwr>



firstdwr.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 'firstajax.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">
	-->
	<style type="text/css">
		input {
			width: 220px;
			height: 34px;
			font-size: 22px;
		}
	</style>
	<script type='text/javascript' src='/mydwr/dwr/interface/userDao.js'></script>
	<script type='text/javascript' src='/mydwr/dwr/engine.js'></script>
	<script type='text/javascript' src='/mydwr/dwr/util.js'></script>
	
	<script type="text/javascript">
		function checkEmail(obj){
			userDao.isOnly(obj,function(data){
				var message = "<img src='images/right.jpg'>";
				if(!data){
					message = "<img src='images/wrong.jpg'><b style='color:red'>该邮箱已经被占用<b>";
				}
				document.getElementById("emailError").innerHTML = message;
			});
		}
	</script>
  </head>
  
  <body>
  	<jsp:include page="top.jsp"></jsp:include>
  	<form action="" method="post">
		<table align="center">
			<tr>
				<td>
					邮箱:
				</td>
				<td>
					<input type="text" name="email" id="email" onblur="checkEmail(this.value)"/>
					<span id="emailError"></span>
				</td>	
			</tr>
			<tr>
				<td>
					昵称:
				</td>
				<td>
					<input type="text" name="nickname" id="nickname"/>
					<span id="nicknameError"></span>
				</td>	
			</tr>
			<tr>
				<td>
				</td>
				<td>
					<input type="submit" value="注册新用户"/>
				</td>	
			</tr>
		</table>
  	</form>
  </body>
</html>
UserDao.java
public class UserDao {
	private UserDao(){
		
	}
	
	private static UserDao dao = null;
	
	public static UserDao getInstance(){
		if(dao == null){
			dao = new UserDao();
		}
		return dao;
	}
	
	public boolean isOnly(String email){
		boolean flag = true;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select id from user where email=?";
		try {
			conn = DBConn.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1,email);
			rs = ps.executeQuery();
			while (rs.next()) {
				flag = false;
			}
		} catch (sqlException e) {
			e.printStackTrace();
		} finally {
			DBConn.closeAll(conn,ps,rs);
		}
		return flag;
	}

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

猜你在找的Ajax相关文章