|
Struts example source code file (ValidationInterceptor.java)
The Struts ValidationInterceptor.java source code/* * Copyright 2002-2007,2009 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.opensymphony.xwork2.validator; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ActionProxy; import com.opensymphony.xwork2.Validateable; import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; import com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; /** * <!-- START SNIPPET: description --> * * 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 <i>ActionClass-validation.xml) and adds field-level and action-level * error messages (provided that the action implements {@link 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. * * <p/>This interceptor does nothing if the name of the method being invoked is specified in the excludeMethods * parameter. <b>excludeMethods accepts a comma-delimited list of method names. For example, requests to * <b>foo!input.action and foo!back.action will be skipped by this interceptor if you set the * <b>excludeMethods parameter to "input, back". * * </ol> * * <p/> The workflow of the action request does not change due to this interceptor. Rather, * this interceptor is often used in conjuction with the <b>workflow interceptor. * * <p/> * * <b>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 * <code>MethodFilterInterceptor for more info. * * <!-- END SNIPPET: description --> * * <p/> Interceptor parameters: * * <!-- START SNIPPET: parameters --> * * <ul> * * <li>alwaysInvokeValidate - Defaults to true. If true validate() method will always * be invoked, otherwise it will not.</li> * * <li>programmatic - Defaults to true. If true and the action is Validateable call validate(), * and any method that starts with "validate". * </li> * * <li>declarative - Defaults to true. Perform validation based on xml or annotations. * * </ul> * * <!-- END SNIPPET: parameters --> * * <p/> Extending the interceptor: * * <p/> * * <!-- START SNIPPET: extending --> * * There are no known extension points for this interceptor. * * <!-- END SNIPPET: extending --> * * <p/> Example code: * * <pre> * <!-- START SNIPPET: example --> * * <action name="someAction" class="com.examples.SomeAction"> * <interceptor-ref name="params"/> * <interceptor-ref name="validation"/> * <interceptor-ref name="workflow"/> * <result name="success">good_result.ftl</result> * </action> * * <-- in the following case myMethod of the action class will not * get validated --> * <action name="someAction" class="com.examples.SomeAction"> * <interceptor-ref name="params"/> * <interceptor-ref name="validation"> * <param name="excludeMethods">myMethod</param> * </interceptor-ref> * <interceptor-ref name="workflow"/> * <result name="success">good_result.ftl</result> * </action> * * <-- in the following case only annotated methods of the action class will * be validated --> * <action name="someAction" class="com.examples.SomeAction"> * <interceptor-ref name="params"/> * <interceptor-ref name="validation"> * <param name="validateAnnotatedMethodOnly">true</param> * </interceptor-ref> * <interceptor-ref name="workflow"/> * <result name="success">good_result.ftl</result> * </action> * * * <!-- END SNIPPET: example --> * </pre> * * @author Jason Carreira * @author Rainer Hermanns * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu * @see ActionValidatorManager * @see com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor */ public class ValidationInterceptor extends MethodFilterInterceptor { private boolean validateAnnotatedMethodOnly; private ActionValidatorManager actionValidatorManager; private static final Logger LOG = LoggerFactory.getLogger(ValidationInterceptor.class); private final static String VALIDATE_PREFIX = "validate"; private final static String ALT_VALIDATE_PREFIX = "validateDo"; private boolean alwaysInvokeValidate = true; private boolean programmatic = true; private boolean declarative = true; @Inject public void setActionValidatorManager(ActionValidatorManager mgr) { this.actionValidatorManager = mgr; } /** * Determines if {@link Validateable}'s <code>validate() should be called, * as well as methods whose name that start with "validate". Defaults to "true". * * @param programmatic <tt>true then Other Struts examples (source code examples)Here is a short list of links related to this Struts ValidationInterceptor.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.