您的位置:首页 > 编程语言 > Java开发

Struts2系列:(19)Validation and Workflow Interceptor的区别

2016-06-30 01:23 579 查看
原文地址:http://www.coderanch.com/t/510822/Struts/Difference-Validation-Workflow-Interceptor-Struts

Validation does the validation. Workflow uses the results of validation (if any) to determine if we need to see the "input" result (by default) again.
validation拦截器用于进行验证,而workflow则利用了validation拦截器的结果,来决定是否返回"input"结果。

In other words, using just the "validation" interceptor isn't enough, unless you're handling any errors on your own (which you could certainly do).
换句话说,只使用validation拦截器是不够的,还需要workflow拦截器对validation拦截器的结果进行处理。

Apache Struts 2 Documentation
Validation
原地址:http://struts.apache.org/docs/validation.html

Validation also depends on both the validation and workflow interceptors (both are included in the default interceptor stack).
The validation interceptor does the validation itself and creates a list of field-specific errors. The workflow interceptor checks for the presence of validation errors: if any are found, it returns the "input" result (by default), taking the user back to the form which contained the validation errors.

If we're using the default settings and our action doesn't have an "input" result defined and there are validation (or, incidentally, type conversion) errors, we'll get an error message back telling us there's no "input" result defined for the action.
这里需要我们去配置name属性为"input"的<result>标签。

第二、注册Validators

Registering Validators

Validation rules are handled by validators, which must be registered with the ValidatorFactory (using the registerValidator method). The simplest way to do so is to add a file name validators.xml in the root of the classpath (/WEB-INF/classes) that declares all the validators you intend to use.在/WEB-INF/classes文件夹下添加validators.xml文件;在开发的时候,直接在src目录下添加validators.xml即可。下面是注册的xml格式The following list shows the default validators included in the framework and is an example of the syntax used to declare our own validators.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator Definition 1.0//EN"
"http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd">

<validators>
<validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
<validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
<validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
<validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
<validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
<validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
<validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
<validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
<validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
<validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
<validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
<validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
<validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
<validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
<validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
<validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>
</validators>


The
validators.xml
used to reference a DTD hosted by Opensymphony, the original location of the XWork project. Since the the move to Apache Struts, DTDs were changed. Please ensure in your projects to include the DTD header as described in the examples found here

第三、使用Validators

Turning on Validation

The default interceptor stack, "defaultStack", already has validation turned on. When creating your own interceptor-stack be sure to include both the
validation
and
workflow
interceptors. From
struts-default.xml
:
<interceptor-stack name="defaultStack">
...
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
Beginning with version 2.0.4 Struts provides an extension to XWork's
com.opensymphony.xwork2.validator.ValidationInterceptor
interceptor.
<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
This interceptor allows us to turn off validation for a specific method by using the
@org.apache.struts2.interceptor.validation.SkipValidation
annotation on the action method.

第一、自定义Validators

Writing custom validators

If you want to write custom validator use on of these classes as a starting point:com.opensymphony.xwork2.validator.validators.ValidatorSupport

com.opensymphony.xwork2.validator.validators.FieldValidatorSupport

com.opensymphony.xwork2.validator.validators.RangeValidatorSupport

com.opensymphony.xwork2.validator.validators.RepopulateConversionErrorFieldValidatorSupport

How Validators of an Action are Found

As mentioned above, the framework will also search up the inheritance tree of the action to find default validations for interfaces and parent classes of the Action. If you are using the short-circuit attribute and relying on default validators higher up in the inheritance tree, make sure you don't accidentally short-circuit things higher in the tree that you really want!struts framework会搜索action类的“继承树(父类和接口)”的验证规则 。注意:要避免“继承树(父类和接口)”的高层default validators中出现短路的情况。如果相同的验证规则,在下面两个配置文件中,如果出错了,会出现两次错误提示。The effect of having common validators on both<actionClass>-validation.xml

<actionClass>-<actionAlias>-validation.xml

It should be noted that the nett effect will be validation on both the validators available in both validation configuration file. For example if we have 'requiredstring' validators defined in both validation xml file for field named 'address', we will see 2 validation error indicating that the the address cannot be empty (assuming validation failed). This is due to WebWork will merge validators found in both validation configuration files.The logic behind this design decision is such that we could have common validators in <actionClass>-validation.xml and more context specific validators to be located in <actionClass>-<actionAlias>-validation.xml
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts