1 login.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 4.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="./demo/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="./demo/formValidator-4.1.1.js" type="text/javascript" charset="UTF-8"></script>
<script src="./demo/formValidatorRegex.js" type="text/javascript" charset="UTF-8"></script>
<script type="text/javascript">
$(document).ready(function() {
$.formValidator.initConfig({
formID : "form1",
theme : "ArrowSolidBox",
submitOnce : true,
onError : function(msg, obj, errorlist) {
$("#errorlist").empty();
$.map(errorlist, function(msg) {
$("#errorlist").append("<li>" + msg + "</li>")
});
alert(msg);
},
ajaxPrompt : ‘有数据正在异步验证,请稍等...‘
});
$("#us").formValidator({
onShowText : "请输入用户名",
onShow : "请输入用户名,只有输入\"111111\"才是对的",
onFocus : "用户名至少5个字符,最多10个字符",
onCorrect : "该用户名可以注册"
}).inputValidator({
min : 5,
max : 10,
onError : "你输入的用户名非法,请确认"
}).ajaxValidator({
dataType : "json",
async : true,
url : "/JqueryFormValidateDemo/user/login.action",
data : {
"username" : function() {
return $("#us").val();
}
},
success : function(data) {
if (data == "yes")
return true;
return data;
},
// buttons : $("#button"),
error : function(jqXHR, textStatus, errorThrown) {
alert("服务器没有返回数据,可能服务器忙,请重试" + errorThrown);
},
onError : "该用户名不可用,请更换用户名",
onWait : "正在对用户名进行合法性校验,请稍候..."
});
$("#password1").formValidator({
onShow : "请输入密码",
onFocus : "至少1个长度",
onCorrect : "密码合法"
}).inputValidator({
min : 1,
empty : {
leftEmpty : false,
rightEmpty : false,
emptyError : "密码两边不能有空符号"
},
onError : "密码不能为空,请确认"
});
});
</script>
</head>
<body>
<form action="" method="post" name="form1" id="form1">
<input type="submit" name="button" id="button" value="提交" /> <br />
<table border="0px" style="font-size:12px" width="730px">
<tr>
<td align="right" width="135px">用户名:</td>
<td width="265px"><input type="text" id="us" name="us"
style="width:120px" value="maodong" />
</td>
<td width="330px"><div id="usTip" style="width:280px"></div>
</td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input type="password" id="password1" name="password1"
style="width:120px" />
</td>
<td><div id="password1Tip" style="width:280px"></div>
</td>
</tr>
<tr>
<td colspan="3"><div id="msTip" style="width:280px"></div>
</td>
</tr>
</table>
</form>
</body>
</html>
2 struts action
package action;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import net.sf.json.JSONArray;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Action;
public class LoginAction extends ActionSupport{
private String username;
private String message;
public String execute(){
if(username.equals("111111")){
this.setMessage("yes");
return Action.SUCCESS;
}
this.setMessage("error");
return Action.SUCCESS;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
3. struts。xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="user" namespace="/user" extends="json-default">
<action name="login" class="action.LoginAction" >
<result type="json">
<param name="root">message</param>
</result>
</action>
</package>
</struts>