一、验证有很多种:
1.最开始接触的js验证客户端验证
2.服务器端验证
2.1.在FormBean中验证(本例中采用的便是FormBean中使用Validator验证格式)
2.2.Action业务逻辑层验证(比如用户名在数据库中是否存在等等)
3.采用Ajax在异步的情况下进行客户端和服务器同时的验证
[color=green]
二、步骤:
1.建立web项目struts_validator添加struts(1.2)支持
2.右击项目-->new-->other-->myeclipse-->web-struts-->struts1.2-->Strurs1.2 Form ,Action & JSP;supclass:ValidatorForm;再添加一些属性,跳转些...
3.打开validator-rules.xml将一下代码添加到struts-config.xml中
<plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"/> </plug-in>
4.打开validator-rules.xml将一下代码添加到ApplicationResources.properties中,当然你可以根据需要修改资源文件
# Struts Validator Error Messages errors.required={0} is required. errors.minlength={0} can not be less than {1} characters. errors.maxlength={0} can not be greater than {1} characters. errors.invalid={0} is invalid. errors.byte={0} must be a byte. errors.short={0} must be a short. errors.integer={0} must be an integer. errors.long={0} must be a long. errors.float={0} must be a float. errors.double={0} must be a double. errors.date={0} is not a date. errors.range={0} is not in the range {1} through {2}. errors.creditcard={0} is an invalid credit card number. errors.email={0} is an invalid e-mail address.
5.编写public class RegistForm extends ValidatorForm的validate方法
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { //必须要继承自父类的 ActionErrors errors = super.validate(mapping, request); // Only need crossfield validations here if (!password.equals(password2)) { errors.add("password2", new ActionMessage("error.password.match")); } return errors; }
6.在WEB-INF目录下编写validation.xml文件
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd"> <form-validation> <formset> <!-- 如果FormBean继承的是ValidatorForm那么<form name="registForm">的name 属性值和<action/>中name的属性值是一样的 如果FormBean继承的是ValidatorActionForm那么<form name="registForm">的name 属性值和<action/>中path的属性值是一样的 --> <form name="registForm"> <field property="username" depends="required,minlength"> <!-- 如果arg不指定name,那么depends的将共享 resource="false"那么在<html:errors property="username" /> 中显示的将是我们key定义的数据 resource="true"(默认的)那么将使用资源文件中的key-value配置 具体的属性信息可以参看http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd --> <arg0 key="username" resource="false" /> <arg1 name="minlength" key="${var:minlength}" resource="false" /> <var> <var-name>minlength</var-name> <var-value>6</var-value> </var> </field> <field property="password" depends="required,minlength"> <arg0 key="password" resource="false" /> <arg1 name="minlength" key="${var:minlength}" resource="false" /> <var> <var-name>minlength</var-name> <var-value>6</var-value> </var> </field> <field property="password2" depends="required"></field> <field property="email" depends="email"> <arg0 key="customer.email" resource="false" /> <arg1 name="email" key="[email protected]" resource="false" /> </field> </form> </formset> </form-validation>
7.部署运行:http://localhost:8080/struts_validator_3/regist.jsp
8.查看附件
[/color]
.