1.校验规则和对应校验器类的声明文件
xwork-2.1.2.jar\com.opensymphony.xwork2.validator.validators.default.xml 的部分内容:
<validators>
<validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
</validators>
2.校验规则文件名
Action类名-Validation.xml 或 Action类名-Action别名-Validation.xml
(Action别名是struts.xml中action标签的name属性值)
2.1.6最少需7个jar:
struts2-core-2.1.6.jar;
xwork-2.1.2.jar;
ognl-2.6.11.jar;
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.1.jar
freemarker-2.3.13.jar
2.1.8最少需6个jar:
struts2-core-2.1.8.jar;
xwork-2.1.6.jar;
ognl-2.7.3.jar;
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
freemarker-2.3.15.jar
在上面两种配置下均没有 Action类名-Action方法名-Validation.xml 这种用法 准确说应该是这样命名的话验证框架不起作用
com.opensymphony.xwork2.validator.Validator接口的部分注释:
To define validation rules for an Action, create a file named ActionName-validation.xml in the same package as the Action. You may also create alias-specific validation rules which add to the default validation rules defined in ActionName-validation.xml by creating another file in the same directory named ActionName-aliasName-validation.xml. In both cases, ActionName is the name of the Action class, and aliasName is the name of the Action alias defined in the xwork.xml configuration for the Action.
在2.1.6下可能会出很多警告 无关痛痒,但非常多
2012-3-14 23:02:40 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [struts.actionMapping]
2012-3-14 23:02:40 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [org.apache.catalina.jsp_file]
2012-3-14 23:03:27 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [struts.actionMapping]
2012-3-14 23:03:27 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [struts.valueStack]
2012-3-14 23:03:28 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [org.apache.catalina.jsp_file]
2012-3-14 23:03:28 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [templateDir]
2012-3-14 23:03:28 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [theme]
2012-3-14 23:03:28 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [.freemarker.Request]
2012-3-14 23:03:28 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [.freemarker.RequestParameters]
3.查找顺序 从上到下
The framework will also search up the inheritance tree of the Action to find validation rules for directly implemented interfaces and parent classes of the Action. This is particularly powerful when combined with ModelDriven Actions and the VisitorFieldValidator. Here's an example of how validation rules are discovered. Given the following class structure:
The framework method will look for the following config files if Dog is to be validated:
- Animal
- Animal-aliasname
- AnimalImpl
- AnimalImpl-aliasname
- Quadraped
- Quadraped-aliasname
- QuadrapedImpl
- QuadrapedImpl-aliasname
- Dog
- Dog-aliasname
com.opensymphony.xwork2.validator.validators.ValidatorSupport 抽象类