`
luotianwen456123
  • 浏览: 6103 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

http://cwiki.apache.org/confluence/display/WW/OVal

阅读更多
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
分享到:
评论

相关推荐

    struts2_S016_S017_repair

    S2-016:https://cwiki.apache.org/confluence/display/WW/S2-016 S2-017:https://cwiki.apache.org/confluence/display/WW/S2-017 官方建议修复方案:升级到最新版本 struts-2.3.15.1 但通常现有系统升级,可能...

    Struts2.rar

    S2-057 CVE-2018-11776 Struts 2.3 to 2.3.34,Struts 2.5 to 2.5.16 https://cwiki.apache.org/confluence/display/WW/S2-057 影响范围非常小 S2-048 CVE-2017-9791 Struts 2.3.X ...

    Struts2漏洞检查工具2019版 V2.3.exe

    S2-057 CVE-2018-11776 Struts 2.3 to 2.3.34,Struts 2.5 to 2.5.16 https://cwiki.apache.org/confluence/display/WW/S2-057 影响范围非常小 S2-048 CVE-2017-9791 Struts 2.3.X ...

    apache-maven-3.5.0-bin

    Apache Maven What is it? ----------- Maven is a software project ... Wiki: https://cwiki.apache.org/confluence/display/MAVEN/ Available Plugins: https://maven.apache.org/plugins/index.html

    Struts2漏洞检查工具2018版.exe

    S2-057 CVE-2018-11776 Struts 2.3 to 2.3.34,Struts 2.5 to 2.5.16 https://cwiki.apache.org/confluence/display/WW/S2-057 影响范围非常小 S2-048 CVE-2017-9791 Struts 2.3.X ...

    kafka学习文档

    kafka 的 wiki 是徆丌错的学习文档: https://cwiki.apache.org/confluence/display/KAFKA/Index 接下来就是一系列文章,文章都是循序渐迕的方式带你了览 kafka: 关亍 kafka 的基本知识,分布式的基础:《分布式消息...

    struts2-core-2.3.31.jar

    一、漏洞简介  Apache Struts是美国阿帕奇(Apache)软件基金会负责维护的一个开源项目,是一套用于创建企业级Java Web应用的开源MVC框架,主要提供两个版本...https://cwiki.apache.org/confluence/display/WW/S2-046

    Struts2 Convention Plugin中文文档 Annotion

    原文:http://cwiki.apache.org/WW/convention-plugin.html 翻译:石太祥 Introduction 从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。 • 包命名习惯来指定Action...

    快速学习-Hive 安装

    https://cwiki.apache.org/confluence/display/Hive/GettingStarted 下载地址 http://archive.apache.org/dist/hive/ 2.2 Hive 安装部署 Hive 安装及配置 (1)把 apache-hive-1.2.1-bin.tar.gz 上传到 linux 的/opt...

    Struts2-007简单分析1

    官方通告:https://cwiki.apache.org/confluence/display/WW/S2-007漏洞环境或者在Struts2-001 dem

    Struts02-002简单分析1

    漏洞版本官方通告:https://cwiki.apache.org/confluence/display/WW/S2-002环境搭建这里的环境我们使用上一个的

    centos7下Redis哨兵集群和kafka集群和zookeeper集群搭建

    centos7下Redis哨兵集群和kafka集群和zookeeper集群搭建 http://blog.csdn.net/gaowenhui2008/article/details/71516901 https://cwiki.apache.org/confluence/display/KAFKA/Clients

    winutils-master.zip

    在windows下调试Hadoop时缺少winutils,该文件一般都是源码编译完成,也有少部分在GitHub里面...以下时Hadoop官方说明:https://cwiki.apache.org/confluence/display/HADOOP2/WindowsProblems

    sip-branches

    sip-branches,New project "red5sip" is available as a branch at ... Unzip and move the sip folder to webapps.

    hive常用函数参数手册

    hive常用的函数参考手册,仅供参考,可能版本不一样,函数方法不一致,请以进官方文档为准,https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

    Struts 2.0.0 - Struts 2.3.14.1 URL及A标签远程命令执行漏洞探测 V1.1

    lation and XSS attacks.(see:https://cwiki.apache.org/confluence/display/WW/S2-01 4) It help you to scan detects whether the websites of your customers has these security vulnerability.Please do not ...

    spark-doc-zh:Apache Spark官方文档中文版

    记得留言和更新翻译进度地址: : 项目看板项目Spark 2.4.4看板负责人:记得更新和优化地址: : 贡献指南项目负责人格式:GitHub + QQ第一期(2016-10-31)贡献者名单: ://cwiki.apachecn.org/pages/viewpage.action...

    0738-6.2.0-如何在Hive中使用多分隔符

    https://cwiki.apache.org/confluence/display/Hive/MultiDelimitSerDe 而Fayson在以前的文章中也基于C5的环境介绍过如何在Hive中使用多分隔符,参考《Hive多分隔符支持示例》。本文主要介绍在CDH6中如何让Hive支持...

    配置hive元数据到Mysql中的全过程记录

    https://cwiki.apache.org/confluence/display/Hive/AdminManual+MetastoreAdmin 注意:先创建一个metastore数据库,字符集要用latin1 将mysql驱动复制到hive的lib目录里。(这有关上一篇安装MySQL的内容) 解压...

Global site tag (gtag.js) - Google Analytics