日期:2014-05-16  浏览次数:20607 次

ajax的登录验证

1.login.jsp
<form id="form1" name="form1" action="login.action" method="post">
<center>

<table cellSpacing=10px >
<tr>
<td>取引CD</td>
<td><input id="cd" name="takeCd" type="text"
style="width: 150px" /></td>
</tr>
<tr>
<td>userId</td>
<td><input id="userId" name="id" maxlength="6" type="text"
style="width: 150px" /></td>
</tr>
<tr>
<td>passWord</td>
<td><input id="passWord" name="pass" type="password"
style="width: 150px" /></td>
</tr>

</table>

<br>

<input type="button" value="ログイン" name="press" onclick="check();" /></center>
<input type="hidden" name="flag" id="flag" value="" />
</form>

struts.xml

<action name="loginPass" class="LoginAction" method="checkPassWord">
   
    </action>

spring.xml里面配置javaBean

<bean name="LoginAction" class="jp.co.syspro.action.LoginAction">
<property name="kintaiService" ref="kintaiService" />
</bean>


然后在Action里面写验证代码:

public String checkPassWord() throws Exception{

String id=this.getRequest().getParameter("id");
String pwd=this.getRequest().getParameter("pwd");
String result="";
Map mapAjax= new HashMap();
mapAjax.put("id", id);
mapAjax.put("pwd", pwd);
TblEmpVO to=this.kintaiService.getEmpCd(mapAjax);
if(to!=null){
result="success";
}else{
result="false";
}
this.getResponse().setContentType("html/text charset=utf-8");
PrintWriter pw = this.getResponse().getWriter();
pw.write(result);
pw.flush();
pw.close();
return null;
}


然后再1.login.jap页面中 :
$.get(url,function(result){
var a=result;
var flg= document.getElementById("flag");
var f=document.getElementById("form1");
if(a=="success"){
     if(takeCd=="025450"){
    flg.value="0";
    f.submit();
    }else{
    flg.value="1";
        f.submit();
        }
}else if(a=="false"){
alert("输入的用户名,密码有错误");
}
});