<!-- 这个方法中的代码在该Action类中的所有Action方法被调用之前都会执行,所以它当中的内容通常为通用的验证
步骤:
1.action要继承ActionSupport
2.重写validate方法做通用验证
3.在配置文件的action中配置input视图
4.jsp页中用s:fielderror来显示错误信息-->
@Override
public void validate() {
if(null==user.getUsername()||user.getUsername().isEmpty()){//添加错误信息,如果不添加就没有任何意义了
this.addFieldError("user.username", "用户名不能为空");
}
if(null==user.getPassword()||user.getPassword().isEmpty()){
this.addFieldError("user.password", "密码不能为空");
}
if(user.getPassword().length()<6){
this.addFieldError("user.password", "密码长度过低");
}
}
<!-- 针对特定的Action方法进行校验 Xxx 为特定的方法名
步骤:
1.action要继承ActionSupport
2.重写validate方法做通用验证,也可以写validateXxx方法做特定方法的验证
3.在配置文件的action中配置input视图
4.jsp页中用s:fielderror来显示错误信息-->
public String regist() throws Exception {
biz.saveObject(user);
return SUCCESS;
}
//针对上面的 regist() 方法的单独验证
public void validateRegist(){
if(null==user.getUsername()||user.getUsername().isEmpty()){
this.addFieldError("user.username", "用户名不能为空");
}
if(null==user.getPassword()||user.getPassword().isEmpty()){
this.addFieldError("user.password", "密码不能为空");
}
if(user.getPassword().length()<6){
this.addFieldError("user.password", "密码长度过低");
}
}
<!--
步骤:
1.action要继承ActionSupport
2.在配置文件的action中配置input视图
3.jsp页中用s:fielderror来显示错误信息
4.编写xml配置文件 ,在配置文件中配置验证规则验证文件规则:
1.验证文件和要验证的Action在同一个包中
2.命名:
对整Action类生效的写法:ClassName-validation.xml
eg: UserAction-validation.xml
对某个Action方法生效的写法: ClassName-ActionName-validation.xmleg: UserAction-login-validation.xml (此处的login是在Struts.xml中Action 中的 name)
--><?xml version="1.0" encoding="UTF-8"?>
<!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="user.username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message key="required"/>
</field-validator>
<field-validator type="stringlength">
<param name="maxLength">20</param>
<param name="minLength">6</param>
<message>长度应该在${minLength}和${maxLength}之间</message>
</field-validator>
</field>
<field name="user.pass">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>密码不能为空</message>
</field-validator>
</field>
<field name="repass">
<field-validator type="fieldexpression">
<param name="expression">user.pass==repass</param>
<message>确认密码要和密码一致</message>
</field-validator>
</field>
</validators>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator Definition 1.0//EN"
"http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd">
<!-- START SNIPPET: validators-default -->
<validators>
<validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
<validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
<validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
<validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
<validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
<validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
<validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
<validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
<validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
<validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
<validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
<validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
<validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
<validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
<validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
<validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>
</validators>
<!-- END SNIPPET: validators-default -->