使用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>密 码:</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>