1、Validator验证框架,2,ActionForm生命周期流程图,3,Why Struts Validation Framework,Issues with writing validate() method in ActionForm classes You have to write validate() method for each ActionForm class, which results in redundant code Changing validation logic requires recompiling Struts validation framework ad
2、dresses these issues Validation logic is configured using built-in validation rules as opposed to writing it in Java code,4,What Struts Validation Framework Is,Validator主要依赖两个jar包 Jakarta-oro.jar:提供一组处理文本的类,具有文本替换、过滤、和分割功能。 Commons-validator.jar:提供了一个简单、可扩展的验证框架,包含了通用的验证方法和验证规则。 Struts 1.1 includes
3、this by default Allows declarative validation for many fields Formats Dates, Numbers, Email, Credit Card, Postal Codes Lengths Minimum, maximum Ranges,5,What Struts Validation Framework Is,Validator采用基于两个xml文件的方式来配置验证规则,分别为validation.xml,validator-rules.xml。在struts应用中,需放到web-inf目录下。 1validator-rules
4、.xml 这个文件包含了一组验证规则,对所有struts应用都适用。一般情况不用修改这个文件,除非要修改或扩展默认规则。 2validation.xml文件 这个文件是针对于具体struts应用的,可以为应用中的ActionForm配置验证规则。而不用编码实现验证。,6,Built-in Validation Rules in validator-rules.xml,required minlength,maxlength byte, short, integer, long, float, double date, range, intRange, floatRange creditCard
5、, email 等等,7,Part of WEB-INF/validator-rules.xml, ,8,Attributes of Validator,name: logical name of the validation rule classname, method: class and method that contains the validation logic methodParams: comma-delimited list of parameters for the method msg: key from the resource bundle Validator fr
6、amework uses this to look up a message from Struts resource bundle depends: other validation rules that should be called first,9,How to Use Validation Framework,Configure Validator plugin Configure validation.xml file (and validator-rules.xml file) Extend Validator ActionForms or Dynamic Validator A
7、ctionForms org.apache.struts.validator.ValidatorForm Org.apache.struts.validator.DynaValidatorForm Set validate=”true” on Action Mappings,10,Configure Validator in WEB-INF/struts-config.xml,11,扩展Struts框架,Struts1.1框架提供了动态插入和加载组件的功能,这种组件被成为Struts插件。Struts插件实际上就是一个Java类,它在Struts应用启动时被初始化,在应用关闭时被销毁。任何作为
8、插件的Java类都应该实现org.apache.struts.action.PlugIn接口。 public interface PlugIn public void init(ActionServlet servlet, ApplicationConfig config) throws ServletException;public void destroy(); 一个Struts应用可以包含一个或多个插件。在Struts应用启动时,Struts框架调用每个插件类的init()方法进行初始化。在插件的初始化阶段,可以完成一些初始化操作,如建立数据库连接,或者和远程系统的连接等。 当应用被关闭
9、时,Struts框架就会调用每个插件类的destroy()方法,destroy()方法可以用来完成释放资源的人物,如关闭数据库连接或远程系统连接等。 除了创建插件类外,还需要在Struts配置文件中配置插件,Struts框架在启动时将根据相关的配置信息来初始化插件。与插件对应的配置元素为。,12,Built-in Error Messages in validator-rules.xml,# Struts Validator Error Messageserrors.required=0 is required.errors.minlength=0 can not be less than 1
10、 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 i
11、s 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.,13,WEB-INF/validation.xml: validatorLoginForm,minlength 4 maxlength8,14,Corresponds to a property in an ActionFormAttributes property: name of
12、 a property (in an ActionForm) that is to be validated depends: comma-delimited list of validation rules to apply against this field All validation rules have to succeed,15,: ,Allows you to specify an alternate message for a field elementname: specifies rule with which the msg is used, should be one
13、 of the rules in validation-rules.xml key: specifies key from the resource bundle that should be added to the ActionError if validation fails resource: if set to false, the value of key is taken as literal string,16,: , Are used to pass additional values to the messageis the 1st replacement and is t
14、he 2nd replacement, and so on,17,: ,Set parameters that a field element may need to pass to one of its validation rules such as minimum and miximum values in a range validation Referenced by elements using $var:var-name syntax,18,ActionForm and Validator,You cannot use ActionForm class with the Vali
15、dator Instead you need to use special subclasses of ActionForm class Validator supports two types special ActionForms Standard ActionForms ValidatorForm Dynamic ActionForms DynaValidatorForm,19,Revised WEB-INF/struts-config.xml,20,Reversied,errors.required=0 is required. errors.minlength=0 can not b
16、e 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 do
17、uble.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.,Note: all messages are copied from the comments part of WEB-INF/validator-rules.xml,WEB-INF/classes/strutsweb/LoginReso
18、urces.properties,21,WEB-INF/classes/strutsweb /ValidatorLoginForm,package strutsweb;import org.apache.struts.validator.*; import org.apache.struts.action.*; import javax.servlet.http.*;public class ValidatorLoginForm extends ValidatorForm private String name;private String password;public String getName() return name;public void setName(String name) this.name = name;public String getPassword() return password;public void setPassword(String password) this.password = password;,