设为首页 收藏本站
查看: 610|回复: 0

[经验分享] http://cwiki.apache.org/confluence/display/WW/OVal

[复制链接]

尚未签到

发表于 2017-1-11 10:48:56 | 显示全部楼层 |阅读模式
Interceptors
The plugin defines the interceptor "ovalValidation" and the interceptor stack "ovalValidationStack" in the "oval-default" package. To use this interceptor, extend the "oval-default" package and apply the interceptor to your action, like:

<struts>
   <package namespace="/myactions" name="myactions" extends="oval-default">
        <action name="simpleFieldsXMLChild" class="org.apache.struts2.interceptor.SimpleFieldsXMLChild">
            <interceptor-ref name="ovalValidationStack"/>
            <result type="void"></result>
        </action>
   </package>
</struts>Annotations
OVal provides many annotations for validations that can be used out of the box (custom validators can also be defined). Once the "ovalValidation" interceptor is applied to an action, you can annotate it:

public class SimpleField extends ActionSupport{
    @NotNull()
    @NotEmpty
    @Length(max = 3)
    private String name;
...
}XML Configuration
OVal provides support for defining the validation via XML. Validation files must end in "-validation.xml" and the rules to find them, are the same rules used to find the validation XML files used by the regular validation mechanisms (default validation in xwork):

Per Action class: in a file named ActionName-validation.xml
Per Action alias: in a file named ActionName-alias-validation.xml
Inheritance hierarchy and interfaces implemented by Action class: The plugin searches up the inheritance tree of the action to find default validations for parent classes of the Action and interfaces implemented
Here is an example of an XML validation file:

<?xml version="1.0" encoding="UTF-8"?>
<oval xmlns="http://oval.sf.net/oval-configuration" xmlns:xsi="http://http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://oval.sf.net/oval-configuration http://oval.sourceforge.net/oval-configuration-1.3.xsd">
    <class type="org.apache.struts2.interceptor.SimpleFieldsXML" overwrite="false"
           applyFieldConstraintsToSetters="true">
        <field name="firstName">
            <notNull/>
        </field>
    </class>
</oval>Profiles
A profile is a set of validations, that can be enabled for a method in an action, for example:

import org.apache.struts2.validation.Profiles;

public class FieldsWithProfiles extends ActionSupport {
    @NotNull(profiles = "1")
    private String firstName;

    @NotNull(profiles = "2")
    private String middleName;

    @NotNull(profiles = "3")
    private String lastName;

    @Profiles({"1", "3"})
    public String firstAndLast() {
        return SUCCESS;
    }

    @Profiles({"2"})
    public void middle() {
        return SUCCESS;
    }
}In this example, when firstAndLast() is executed, the fields firstName and lastName will be validated. When middle() is executed, only middleName will be validated. When a method is annotated with the Profiles annotation, only the validations in the specified profiles will be performed. If no profile is specified for an action method, all the validations in the class will be evaluated.

Internationalization of messages
The OVal annotations (and corresponding XML tags) have a message attribute that can be used to specify either the error message, or the key of the error message. If a key is found in a property file, matching the value of the massage attribute, it will be used as the message, otherwise the value will be used as a literal string. For example, given this property file:

BookAction.properties
notnull.field=${field.name} cannot be nulland this class:

BookAction.java
public class BookActionextends ActionSupport {
    @NotNull(message = "notnull.field")
    private String title;

    @NotNull(message = "You must enter a valid ISBN")
    private String isbn;
...
}When that action is validated, the field errors would be:

"title cannot be null"
"You must enter a valid ISBN"
The current OVal "context" object is pushed into the stack for each validator, so it can be accessed from the property file to build the error message. See the OVal javadoc for more properties available in the FieldContext class.

The OVal Validation Interceptor
This interceptor runs the action through the standard validation framework, which in turn checks the action against any validation rules (found in files such as ActionClass-validation.xml) and adds field-level and action-level error messages (provided that the action implements com.opensymphony.xwork2.ValidationAware). This interceptor is often one of the last (or second to last) interceptors applied in a stack, as it assumes that all values have already been set on the action.

This interceptor does nothing if the name of the method being invoked is specified in the excludeMethods parameter. excludeMethods accepts a comma-delimited list of method names. For example, requests to foo!input.action and foo!back.action will be skipped by this interceptor if you set the excludeMethods parameter to "input, back".

Note that this has nothing to do with the com.opensymphony.xwork2.Validateable interface and simply adds error messages to the action. The workflow of the action request does not change due to this interceptor. Rather, this interceptor is often used in conjuction with the workflow interceptor.

NOTE: As this method extends off MethodFilterInterceptor, it is capable of deciding if it is applicable only to selective methods in the action class. See MethodFilterInterceptor for more info.

The param alwaysInvokeValidate (default to true), will make the interceptor invoke validate() on the action, if the action implements Validateable.

The param programmatic (defaults to true), will make the plugin call validateX() where X is the name of the method that will be invoked in the action. If this param is set to false, alwaysInvokeValidate is ignored and validate() won't be invoked.

Installation
The jar plugin needs to be added to the lib directory of your application as well as other dependencies. If you are using XML validation, XStream needs to be included. Here is the maven dependency example:

<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.3.1</version>
</dependency>Configuration reference
Add a constant element to your struts config file to change the value of a configuration setting, like:

<constant name="struts.oval.validateJPAAnnotations" value="true" />Name Default Value Description
struts.oval.validateJPAAnnotations false Enables mapping of JPA annotations to Oval validators

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-326971-1-1.html 上篇帖子: Apache HTTP Server + SVN 版本控制方案(一) 下篇帖子: Interoperable WSDL faults in gSOAP and Apache Axis webservices
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表