前端之家收集整理的这篇文章主要介绍了
用XML方式校验,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<?xmlversion="1.0"encoding="UTF-8"?>
<!--XML文件命名InputValidate-validation.xml(class名-validation.xml)-->
<!DOCTYPEvalidatorsPUBLIC
"-//ApacheStruts//XWorkValidator1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<fieldname="ID">
<field-validatortype="requiredstring">
<paramname="trim">true</param>
<message>ID不能为空</message>
</field-validator>
</field>
<fieldname="password">
<field-validatortype="requiredstring">
<message>password不能为空</message>
</field-validator>
<!--用正则表达式校验-->
<field-validatortype="regex">
<paramname="expression"><![CDATA[^1[358]\d{9}$]]></param>
<message>password格式不正确</message>
</field-validator>
</field>
</validators>
packagecom.cb;
importcom.opensymphony.xwork2.ActionContext;
importcom.opensymphony.xwork2.ActionSupport;
publicclassInputValidateextendsActionSupport{
privateStringID;
privateStringpassword;
publicvoidsetID(StringiD){
ID=iD;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
publicStringsave(){
ActionContext.getContext().put("Msg","保存成功");
return"success";
}
publicStringgetID(){
returnID;
}
publicStringgetPassword(){
returnpassword;
}
}
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<%@tagliburi="/struts-tags"prefix="s"%><!--使用Struts2标签-->
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<title>Input校验</title>
</head>
<body>
<s:fielderror></s:fielderror><!--显示Struts2校验结果-->
<formaction="InputValidate_save.action"method="post">
账号:<inputtype="text"name="ID"><br><br>
密码:<inputtype="password"name="password"><br><br>
<inputtype="submit"value="登陆">
</form>
</body>
</html>