1.action中validate()与validateXxx()

用于action执行前的校验,如果校验失败,跳到input视图,
前者校验整个Action;
后者校验该Action中的Xxx方法.
如果两者都有,则validateXxx方法会先调用。

2.struts2中输入校验的流程

a.类型转换器将用户请求字符串的参数转化为java类型,并将其设为Action的属性值。
b.如果转换过程中出现异常,异常将保存在ActionContext中,conversionError拦截器负责将其封装到fieldError中,否则直接到c,类型转换完毕。
c.通过反射调用validateXxx(),然后调用validate()。
d.如果上面3步没有出现fieldError,则调用action的处理请求方法,否则跳到input视图.

3.struts2校验框架

校验配置文件的DTD在xword-core-2.2.3.jar中,系统自带的校验器的xml配置文件是
xword-core-2.2.3.jar里的com.opensymphony.xword2.vilidator.validators.default.xml文件

校验配置文件名称必须为
ActionClassName-validation.xml:Action类名-validation.xml
ActionClassName-ActoinAliasName-validation.xml:Action类名-类别名-validation.xml
类别名为在struts.xml中action的name属性名。

如果要输出国际化信息,则使用<message key="资源文件键名">或<message>${getText("资源文件键名")</message>

以上是服务器校验,如果需要客户端校验,可以在提供了服务器校验的基础上,在<s:form>中添加validation="true"属性,这样,客户端会根据服务端校验框架自动生产客户端的javascript语句,但不是所有的系统自带校验器都支持生成客户端校验脚本。

注:如果配置的自带的校验器不支持生成客户端脚本,而客户端form又设置了validation="true",那么将会报错:


Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@5810af
The problematic instruction:
----------
==> list tag.getValidators("${tagName}") as validator [on line 46, column 9 in template/xhtml/form-close-validate.ftl]
 in include "/${parameters.templateDir}/xhtml/form-close-validate.ftl" [on line 25, column 1 in template/xhtml/form-close.ftl]
----------

Java backtrace for programmers:
----------
freemarker.template.TemplateModelException: Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@5810af
	at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130)
	at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
	at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
	at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
	at freemarker.core.Environment.visit(Environment.java:417)
	at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.MixedContent.accept(MixedContent.java:92)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.MixedContent.accept(MixedContent.java:92)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.Environment.include(Environment.java:1483)
	at freemarker.core.Include.accept(Include.java:169)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.MixedContent.accept(MixedContent.java:92)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.Environment.process(Environment.java:190)
	at freemarker.template.Template.process(Template.java:237)
	at org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:157)
	at org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:559)
	at org.apache.struts2.components.UIBean.end(UIBean.java:513)
	at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
	at org.apache.jsp.index_jsp._jspx_meth_s_005fform_005f0(index_jsp.java:226)
	at org.apache.jsp.index_jsp._jspService(index_jsp.java:103)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:384)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
	at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
	at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildValidatorKey(AnnotationActionValidatorManager.java:219)
	at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.getValidators(AnnotationActionValidatorManager.java:76)
	at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.getValidators(AnnotationActionValidatorManager.java:72)
	at org.apache.struts2.components.Form.getValidators(Form.java:265)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:840)
	at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:106)
	... 48 more
2011-8-18 0:17:17 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
	at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildValidatorKey(AnnotationActionValidatorManager.java:219)
	at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.getValidators(AnnotationActionValidatorManager.java:76)
	at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.getValidators(AnnotationActionValidatorManager.java:72)
	at org.apache.struts2.components.Form.getValidators(Form.java:265)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:840)
	at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:106)
	at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
	at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
	at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
	at freemarker.core.Environment.visit(Environment.java:417)
	at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.MixedContent.accept(MixedContent.java:92)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.MixedContent.accept(MixedContent.java:92)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.Environment.include(Environment.java:1483)
	at freemarker.core.Include.accept(Include.java:169)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.MixedContent.accept(MixedContent.java:92)
	at freemarker.core.Environment.visit(Environment.java:210)
	at freemarker.core.Environment.process(Environment.java:190)
	at freemarker.template.Template.process(Template.java:237)
	at org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:157)
	at org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:559)
	at org.apache.struts2.components.UIBean.end(UIBean.java:513)
	at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
	at org.apache.jsp.index_jsp._jspx_meth_s_005fform_005f0(index_jsp.java:226)
	at org.apache.jsp.index_jsp._jspService(index_jsp.java:103)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:384)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
	at java.lang.Thread.run(Thread.java:619)

4.struts2校验文件的搜索顺序

如果定义了Test-validation.xml和Test-login-validation.xml两个文件,那么,系统对于login请求,会先搜索Test-validation.xml文件,找到后并不停止搜索,而是继续找Test-login-validation.xml文件,如果两个都找到的话,那么对login请求的验证为这两个文件的总和,而不是其中的一个,如果Test继承BaseTest的话,那么系统会先搜索BaseTest类的两个校验文件,然后也不停止,继续找Test类的两个校验文件,然后将4个文件合并一起校验,如果其中有冲突,那么后找到的校验文件将覆盖先找到的校验文件。

5.struts2校验文件的两种书写风格

a.字段风格


Xml代码 
    

  
    
   
 
1. <field name="">
2.     <validator type="" short-circuit="">
3.        ...   
4.     </validator>  
5. </field>  
 
<field name="">
     <validator type="" short-circuit="">
        ...
     </validator>
 </field>

b.非字段风格

Xml代码 
    

  
    
   
 
1. <validator type="" short-circuit="">
2.     <param name="fieldName">value</param>  
3.     ...   
4. </validator>     
 
<validator type="" short-circuit="">
     <param name="fieldName">value</param>
     ...
 </validator>

其中short-circuit(短路)属性值为boolean,默认为false,即不支持短路

6.校验顺序玉短路原则

注:字段校验器指必须提供字段的校验器,而不是指字段校验风格,非字段同理。

校验顺序
a.所有非字校验器优先于字段校验器(所以,字段校验器永远不会影响非字段校验器)
b.所有字段与非字段风格按编写先后顺序执行

短路原则:
a.当非字段校验失败,则该字段的字段校验器不会获得校验机会,但不影响其他非字段校验
b.字段校验失败,则在此失败处代码以下的该字段的所有字段校验器不会获得校验机会

注:短路原则是为了不让一个字段同时出现多条验证失败的提示信息,如验证date不能为空且必须大于2000-01-01,那么如果输入的是空值且定义short-circuit="false"的话,页面将会显示这两种验证的message.

7.配置自定义的校验器

将default.xml文件复制到classes目录下,然后在里面添加自定义的校验器配置,因为如果定义了自己的校验器配置文件在classes目录下,那么系统将不再会加载default.xml文件里的内容。