1.register.jsp
<body>
<h1><s:text name="register.h1"></s:text></h1>
<div>
<s:fielderror/>
</div>
<s:form action="register.action" theme="simple">
<div>
<s:text name="register.name"></s:text><s:textfield name="name"/>
</div>
<div>
<s:text name="register.pwd"></s:text><s:password name="pwd"/>
</div>
<div>
<s:text name="registger.repwd"></s:text><s:password name="repwd"/>
</div>
<div><s:submit key="register.submit"/></div>
</s:form>
</body>
2.struts.xml使用标签,引用资源文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="msg"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="register" class="com.yh.struts.action.UserAction" method="regist">
<result>success.jsp</result>
<result name="input">register.jsp</result>
</action>
</package>
</struts>
3.msg.properties 以及msg_en.properties 资源文件 xwork-core-2.3.12.jar---------------- xwork-messages.properties
xwork.default.invalid.fieldvalue=\ {0}\u5FC5\u987B\u4E3A\u6570\u5B57
login.name=\u7528\u6237\u540D\uFF1A
login.pass=\u5BC6\u7801\uFF1A
login.title=\u7528\u6237\u767B\u9646
login.name.span=\u7528\u6237\u540D\u53EA\u80FD\u4E3A\u6570\u5B57\uFF0C\u4E14\u57286\u523012\u4F4D\u4E4B\u95F4
login.pass.span=\u5BC6\u7801\u4E3A\u5B57\u6BCD\u6570\u5B57\uFF0C6\u523012\u4F4D\u4E4B\u95F4
login.submit=\u63D0\u4EA4
4.action使用默认的自动验证set,validateXx(),validate(),验证,框架验证的自动引用,验证通过返回success
public class UserAction extends ActionSupport {
private String name;
private String pwd;
private String repwd;
public String getRepwd() {
return repwd;
}
public void setRepwd(String repwd) {
this.repwd = repwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
/**
* 对指定方法验证
*/
public void validateRegist() {
// TODO Auto-generated method stub
// if (name.length()<6||name.length()>15) {
// this.addFieldError("name", "用户名长度必须在6-15之间!");
// }
// if (pwd.length()<6) {
// this.addFieldError("pwd", "密码长度必须大于6位!");
// }
// if(!pwd.equals(repwd)){
// this.addFieldError("repwd", "两次密码不一致!");
// }
// if(repwd.isEmpty()){
// this.addFieldError("repwd", "确认密码不能为空!");
// }
}
/**
* 通用验证
*/
public void validate(){
if (name.isEmpty()) {
this.addFieldError("name", this.getText("namenotnull"));
}
if (pwd.isEmpty()) {
this.addFieldError("pwd", "密码为空!");
}
}
public String regist() {
return Action.SUCCESS;
}
}
5.UserAction-validation.xml自动的框架验证
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<!--
验证字段用哪个校验器 <field name="name"> <field-validator type="requiredstring">
<message>验证框架:用户名不能为空!</message> </field-validator> </field>
-->
<!-- 用哪个校验器验证字段 -->
<validator type="requiredstring">
<param name="fieldName">name</param>
<param name="trim">true</param>
<message key="namenotnull"></message>
</validator>
<validator type="stringlength">
<param name="fieldName">name</param>
<param name="minLength">6</param>
<param name="maxLength">15</param>
<message>验证框架:用户名长度必须在${minLength}和${maxLength}之间!</message>
</validator>
<validator type="regex">
<param name="fieldName">name</param>
<param name="expression">^[a-zA-Z]\w*$</param>
<message>用户名必须以字母开头!</message>
</validator>
<validator type="requiredstring">
<param name="fieldName">pwd</param>
<message>密码不能为空!</message>
</validator>
<validator type="stringlength">
<param name="fieldName">pwd</param>
<param name="minLength">6</param>
<message>密码长度必须大于等于${minLength}</message>
</validator>
<validator type="fieldexpression">
<param name="fieldName">repwd</param>
<param name="expression">pwd==repwd</param>
<message>两次密码不一致!</message>
</validator>
</validators>
6.