上一节使用了Action类中的validate方法进行校验,此种校验方式适合较为复杂的校验情况。而本节将介绍使用配置文件的校验方式!
在xwork.jar包的com.opensymphony.xwork2.validator.validators目录下有一个default.xml文件。这里面定义了许多struts2的预设的校验器。
1,RegisterAction-validation.xml。放在与action同一目录下,知名得义,不难理解
如果要给action中的方法abc校验,则文件名可以这样命名:RegisterAction-abc-validation.xml
<?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"> <!--此文件必须和RegisterAction.java在同一个目录下--> <!--对应的type定义都在xwork.jav包下的com.opensymphony.xwork2.validator.validators.default.xml文件 --> <validators> <field name="username"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>username should not be blank!</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">10</param> <message>username should be between ${minLength} and ${maxLength}</message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message>password should not be null</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">10</param> <message>password should between ${minLength} and ${maxLength}</message> </field-validator> </field> <field name="repassword"> <field-validator type="requiredstring"> <message>repassword should not be null</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">10</param> <message>repassword should be between 6 and 10</message> </field-validator> </field> <field name="age"> <field-validator type="int"> <param name="min">1</param> <param name="max">150</param> <message>age should be between 1 and 150</message> </field-validator> </field> <field name="birthday"> <field-validator type="required"> <message>birthday should not be blank!</message> </field-validator> <field-validator type="date"> <param name="min">1998-12-12</param> <param name="max">2009-12-12</param> <message>the birthday should be between ${min} and ${max}</message> </field-validator> </field> <field name="graduation"> <field-validator type="required"> <message>graduation should not be blank!</message> </field-validator> <field-validator type="date"> <param name="min">2005-12-12</param> <param name="max">2007-12-12</param> <message>the graduation should be between ${min} and ${max}</message> </field-validator> </field> </validators>
这些都是filedError,客户端需使用<s:fielderror/>来接收错误提示。
如果在action中存在validate方法,则两边都会执行,即会执行配置文件中的校验也会执行validate中的校验,建议不重写validate中的校验。
PS:如果存在子action继承父action的情况,那么校验的顺序会如何呢?如下情况
父类action
ParentAction{ public void test() }
其校验文件是
ParentAction-validation.xml ParentAction-test-validation.xml
子类action
ChildAction extends ParentAction{ public void test() }
其校验文件是
ParentAction-validation.xml ParentAction-test-validation.xml
则执行顺序是,先执行父类中的ParentAction-validation.xml,ParentAction-test-validation.xml
再执行子类中的ParentAction-validation.xml,ParentAction-test-validation.xml