1.首先 我们写个一Action继承ActionSupport
package action;
import com.opensymphony.xwork2.ActionSupport;
import bean.User;
public class TempAction extends ActionSupport{
private User user;
private String repwd;
public String execute(){
return "success";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getRepwd() {
return repwd;
}
public void setRepwd(String repwd) {
this.repwd = repwd;
}
}
2.我们配置一个xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2dtd">
<validators>
<field name="user.name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message key="name.null"/>
<!--<message>用户名不能为空</message>
--></field-validator>
<field-validator type="stringlength">
<param name="maxLength">10</param>
<param name="minLength">6</param>
<message key="name.length"/>
<!--<message>用户名长度须在${minLength}和${maxLength}中间</message>
--></field-validator>
</field>
<field name="user.password">
<field-validator type="requiredstring">
<message key="password.null"/>
<!--<message>密码不能为空</message>
--></field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<message key="password.length"/>
<!--<message>密码长度须大于${minLength}</message>
--></field-validator>
</field>
<field name="repwd">
<field-validator type="requiredstring">
<message key="repwd.null"/>
<!--<message>确认密码不能为空</message>
--></field-validator>
<field-validator type="fieldexpression">
<param name="expression">user.password==repwd</param>
<message key="repwd.s"/>
<!--<message>密码和确认密码必须相同</message>
--></field-validator>
</field>
<field name="user.telephone">
<field-validator type="requiredstring">
<message key="telephone.null"/>
<!--<message>电话号码不能为空</message>
--></field-validator>
<field-validator type="regex">
<param name="regex">^(\d{3,4}-){0,1}\d{7,8}$</param>
<message key="telephone.re"/>
<!--<message>电话号码格式不正确</message>
--></field-validator>
</field>
<field name="user.username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message key="username.null"/>
<!--<message>用户姓名不能为空</message>
--></field-validator>
<field-validator type="stringlength">
<param name="maxLength">4</param>
<param name="minLength">2</param>
<message key="username.length"/>
<!--<message>用户姓名长度须在${minLength}和${maxLength}中间</message>
--></field-validator>
</field>
</validators>