登录页面
<form action="" method="post" id="frm">
<table border="1" align="center">
<caption>
<h1>部门表登录</h1>
</caption>
<tr>
<td>dname</td>
<td><input name="dept.dname" />
</td>
</tr>
<tr>
<td>loc</td>
<td><input name="dept.loc" />
</td>
</tr>
<tr align="center">
<!-- 必须改成普通按钮! -->
<td colspan="2"><input type="button" value="登录" id="button" />
<input type="reset" value="重置" />
</td>
</tr>
</form>
脚本页面
$(function() {
// 单击事件
$("#button").click(function() {
// jQuery.post(url, [data], [callback], [type])
$.post("login.action", $("#frm").serialize(), function(data) {
if(data==1){
document.location.href="index.jsp"; //重定向
}else{
$("body").prepend("<div style='color:red;text-align:center'>用户名或密码错误!</div>");
}
}, "json");
});
});
Action控制器
public String ajax_login() throws Exception {
// 调用方法
dept = biz.findIsLogin(dept);
// 判断
if (dept != null) {
session.put("depts", dept);
result = "1"; // 登录成功
} else {
result = "0"; // 登录失败
}
return SUCCESS;
}
Map<String, Object> session;
@Override
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session = session;
}
显示结果: