dwr的使用

使用dwr进行异步的请求操作

使用dwr可以通过调用javascript的方法来调用java的相应方法,从而达到异步请求处理的结果。

1、导入相应的jar包

dwr.jar

2、编写处理Ajax请求的类

package cn.hpu.dwr;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import cn.hpu.po.RoleRelation;
import cn.hpu.po.User;
import cn.hpu.service.UserService;

public class LoginAjax {

	private UserService userService;
	
	/**用户登录
	 * @author qinrui
	 * @param java.util.Map map
	 * @param session
	 * @return java.lang.String
	 */
	public String login(Map map ,HttpSession session) {
		
		String name = map.get("name").toString();
		String password = map.get("password").toString();
		String code = map.get("code").toString();
		
		// 数据校验
		if(name != null && !"".equals(name.trim()) && password != null && !"".equals(password.trim())) {
			
			if(code != null && !"".equals(code.trim())) {
				
				String randCode = session.getAttribute("rand").toString();
				
				if(randCode.equals(code)) {
					
					User loginUser = userService.loadUserByRegName(name);
					
					if(loginUser != null) {
						
						if(loginUser.getPassword().equals(password)) {
							// 登录记录
							session.setAttribute("user", loginUser);
							
							List<RoleRelation> roleRelations = loginUser.getRoleInfo().getRoleRelations();
							
							session.setAttribute("roleRelations", roleRelations);
							
							return "true";
						} else {
							return "密码错误!";
						}
						
					} else {
						return "用户名不存在!";
					}
				} else {
					return "验证码不正确!";
				}
				
			} else {
				return "验证码为空!";
			}
			
		} else {
			return "用户名或密码为空!";
		}
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}

3、配置beans.xml

<!-- 配置用户登录的异步类 -->
<bean id="loginAjax" class="cn.hpu.dwr.LoginAjax">
	<property name="userService" ref="userService" />
</bean>

 

4、在WEB-INF下面添加dwr.xml文件并进行配置(此处使用Spring来进行管理创建)

<?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>
		<!-- 不使用Spring进行管理 -->
		<!--<create creator="new" javascript="LoginAjax">
			<param name="class" value="cn.hpu.dwr.LoginAjax" />
		</create>
		<convert converter="bean" match="cn.hpu.po.DDCategory"></convert>-->
		
		<!-- 用户登录的异步类 -->
		<create creator="spring" javascript="loginAjax">
			<param name="beanName" value="loginAjax" />
			<include method="login" />
		</create>
	</allow>
		
</dwr>

 

5、引入相应的javascript文件

<!-- 使用dwr所必须引入的js文件 -->
<script type="text/javascript" src="dwr/engine.js" ></script>
<!-- 使用dwr获取页面(表单等)值引入的工具js文件 -->
<script type="text/javascript" src="dwr/util.js" ></script>
<!-- 与java文件相对应的js文件,此处的js文件名必须与dwr.xml配置里面的javascript的值相同 -->
<script type="text/javascript" src="dwr/interface/complainAjax.js" ></script>

 6、使用complainAjax来调用java里面的方法

<script type="text/javascript" >
function login() {
	var loginMap = dwr.util.getValues("loginForm");
	loginAjax.login(loginMap , loginCallback);
}

function loginCallback(data) {
	if(data == 'true') {
		window.location.href = 'index';
	} else {
		alert(data);
	}
}
</script>

 

<div id="loginField" style="background: url('img/login.png');">
	<form id="loginForm" method="post">
		<table>
			<tr>
				<td>用户名:</td>
				<td><input type="text" name="name" class="inputStyle" /></td>
			</tr>
			<tr>
				<td>密&nbsp;&nbsp;码:</td>
				<td><input type="password" name="password" class="inputStyle" /></td>
			</tr>
			<tr>
				<td>验证码:</td>
				<td><input type="text" name="code" class="codeInputStyle" /><img src="image" class="imageStyle" /></td>
			</tr>
			<tr>
				<td colspan="2" class="buttonRow"><input type="button" οnclick="login()" value="登录" />
				<input type="reset" value="重置" /></td>
			</tr>
		</table>
	</form>
</div>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值