验证方法(Spring Action)

来源:互联网 发布:linux opengl 安装 编辑:程序博客网 时间:2024/06/02 00:21
 
  1. /*
  2.  * Copyright 2002-2007 the original author or authors.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.springframework.validation;
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. import org.springframework.util.Assert;
  20. import org.springframework.util.StringUtils;
  21. /**
  22.  * Utility class offering convenient methods for invoking a {@link Validator}
  23.  * and for rejecting empty fields.
  24.  * 
  25.  * <p>Checks for an empty field in <code>Validator</code> implementations can become
  26.  * one-liners when using {@link #rejectIfEmpty} or {@link #rejectIfEmptyOrWhitespace}.
  27.  *
  28.  * @author Juergen Hoeller
  29.  * @author Dmitriy Kopylenko
  30.  * @since 06.05.2003
  31.  * @see Validator
  32.  * @see Errors
  33.  */
  34. public abstract class ValidationUtils {
  35.     private static Log logger = LogFactory.getLog(ValidationUtils.class);
  36.     /**
  37.      * Invoke the given {@link Validator} for the supplied object and
  38.      * {@link Errors} instance.
  39.      * @param validator the <code>Validator</code> to be invoked (must not be <code>null</code>)
  40.      * @param obj the object to bind the parameters to
  41.      * @param errors the {@link Errors} instance that should store the errors (must not be <code>null</code>)
  42.      * @throws IllegalArgumentException if either of the <code>Validator</code> or <code>Errors</code> arguments is <code>null</code>;
  43.      * or if the supplied <code>Validator</code> does not {@link Validator#supports(Class) support}
  44.      * the validation of the supplied object's type
  45.      */
  46.     public static void invokeValidator(Validator validator, Object obj, Errors errors) {
  47.         Assert.notNull(validator, "Validator must not be null");
  48.         Assert.notNull(errors, "Errors object must not be null");
  49.         if (logger.isDebugEnabled()) {
  50.             logger.debug("Invoking validator [" + validator + "]");
  51.         }
  52.         if (obj != null && !validator.supports(obj.getClass())) {
  53.             throw new IllegalArgumentException(
  54.                     "Validator [" + validator.getClass() + "] does not support [" + obj.getClass() + "]");
  55.         }
  56.         validator.validate(obj, errors);
  57.         if (logger.isDebugEnabled()) {
  58.             if (errors.hasErrors()) {
  59.                 logger.debug("Validator found " + errors.getErrorCount() + " errors");
  60.             }
  61.             else {
  62.                 logger.debug("Validator found no errors");
  63.             }
  64.         }
  65.     }
  66.     /**
  67.      * Reject the given field with the given error code if the value is empty.
  68.      * <p>An 'empty' value in this context means either <code>null</code> or
  69.      * the empty string "". 
  70.      * <p>The object whose field is being validated does not need to be passed
  71.      * in because the {@link Errors} instance can resolve field values by itself
  72.      * (it will usually hold an internal reference to the target object).
  73.      * @param errors the <code>Errors</code> instance to register errors on
  74.      * @param field the field name to check
  75.      * @param errorCode the error code, interpretable as message key
  76.      */
  77.     public static void rejectIfEmpty(Errors errors, String field, String errorCode) {
  78.         rejectIfEmpty(errors, field, errorCode, nullnull);
  79.     }
  80.     /**
  81.      * Reject the given field with the given error code and default message
  82.      * if the value is empty.
  83.      * <p>An 'empty' value in this context means either <code>null</code> or
  84.      * the empty string "". 
  85.      * <p>The object whose field is being validated does not need to be passed
  86.      * in because the {@link Errors} instance can resolve field values by itself
  87.      * (it will usually hold an internal reference to the target object).
  88.      * @param errors the <code>Errors</code> instance to register errors on
  89.      * @param field the field name to check
  90.      * @param errorCode error code, interpretable as message key
  91.      * @param defaultMessage fallback default message
  92.      */
  93.     public static void rejectIfEmpty(Errors errors, String field, String errorCode, String defaultMessage) {
  94.         rejectIfEmpty(errors, field, errorCode, null, defaultMessage);
  95.     }
  96.     /**
  97.      * Reject the given field with the given error codea nd error arguments
  98.      * if the value is empty.
  99.      * <p>An 'empty' value in this context means either <code>null</code> or
  100.      * the empty string "".
  101.      * <p>The object whose field is being validated does not need to be passed
  102.      * in because the {@link Errors} instance can resolve field values by itself
  103.      * (it will usually hold an internal reference to the target object).
  104.      * @param errors the <code>Errors</code> instance to register errors on
  105.      * @param field the field name to check
  106.      * @param errorCode the error code, interpretable as message key
  107.      * @param errorArgs the error arguments, for argument binding via MessageFormat
  108.      * (can be <code>null</code>)
  109.      */
  110.     public static void rejectIfEmpty(Errors errors, String field, String errorCode, Object[] errorArgs) {
  111.         rejectIfEmpty(errors, field, errorCode, errorArgs, null);
  112.     }
  113.     /**
  114.      * Reject the given field with the given error code, error arguments
  115.      * and default message if the value is empty.
  116.      * <p>An 'empty' value in this context means either <code>null</code> or
  117.      * the empty string "". 
  118.      * <p>The object whose field is being validated does not need to be passed
  119.      * in because the {@link Errors} instance can resolve field values by itself
  120.      * (it will usually hold an internal reference to the target object).
  121.      * @param errors the <code>Errors</code> instance to register errors on
  122.      * @param field the field name to check
  123.      * @param errorCode the error code, interpretable as message key
  124.      * @param errorArgs the error arguments, for argument binding via MessageFormat
  125.      * (can be <code>null</code>)
  126.      * @param defaultMessage fallback default message
  127.      */
  128.     public static void rejectIfEmpty(
  129.             Errors errors, String field, String errorCode, Object[] errorArgs, String defaultMessage) {
  130.         Assert.notNull(errors, "Errors object must not be null");
  131.         Object value = errors.getFieldValue(field);
  132.         if (value == null || !StringUtils.hasLength(value.toString())) {
  133.             errors.rejectValue(field, errorCode, errorArgs, defaultMessage);
  134.         }
  135.     }
  136.     /**
  137.      * Reject the given field with the given error code if the value is empty
  138.      * or just contains whitespace.
  139.      * <p>An 'empty' value in this context means either <code>null</code>,
  140.      * the empty string "", or consisting wholly of whitespace.
  141.      * <p>The object whose field is being validated does not need to be passed
  142.      * in because the {@link Errors} instance can resolve field values by itself
  143.      * (it will usually hold an internal reference to the target object).
  144.      * @param errors the <code>Errors</code> instance to register errors on
  145.      * @param field the field name to check
  146.      * @param errorCode the error code, interpretable as message key
  147.      */
  148.     public static void rejectIfEmptyOrWhitespace(Errors errors, String field, String errorCode) {
  149.         rejectIfEmptyOrWhitespace(errors, field, errorCode, nullnull);
  150.     }
  151.     /**
  152.      * Reject the given field with the given error code and default message
  153.      * if the value is empty or just contains whitespace.
  154.      * <p>An 'empty' value in this context means either <code>null</code>,
  155.      * the empty string "", or consisting wholly of whitespace.
  156.      * <p>The object whose field is being validated does not need to be passed
  157.      * in because the {@link Errors} instance can resolve field values by itself
  158.      * (it will usually hold an internal reference to the target object).
  159.      * @param errors the <code>Errors</code> instance to register errors on
  160.      * @param field the field name to check
  161.      * @param errorCode the error code, interpretable as message key
  162.      * @param defaultMessage fallback default message
  163.      */
  164.     public static void rejectIfEmptyOrWhitespace(
  165.             Errors errors, String field, String errorCode, String defaultMessage) {
  166.         rejectIfEmptyOrWhitespace(errors, field, errorCode, null, defaultMessage);
  167.     }
  168.     /**
  169.      * Reject the given field with the given error code and error arguments
  170.      * if the value is empty or just contains whitespace.
  171.      * <p>An 'empty' value in this context means either <code>null</code>,
  172.      * the empty string "", or consisting wholly of whitespace.
  173.      * <p>The object whose field is being validated does not need to be passed
  174.      * in because the {@link Errors} instance can resolve field values by itself
  175.      * (it will usually hold an internal reference to the target object).
  176.      * @param errors the <code>Errors</code> instance to register errors on
  177.      * @param field the field name to check
  178.      * @param errorCode the error code, interpretable as message key
  179.      * @param errorArgs the error arguments, for argument binding via MessageFormat
  180.      * (can be <code>null</code>)
  181.      */
  182.     public static void rejectIfEmptyOrWhitespace(
  183.             Errors errors, String field, String errorCode, Object[] errorArgs) {
  184.         rejectIfEmptyOrWhitespace(errors, field, errorCode, errorArgs, null);
  185.     }
  186.     /**
  187.      * Reject the given field with the given error code, error arguments
  188.      * and default message if the value is empty or just contains whitespace.
  189.      * <p>An 'empty' value in this context means either <code>null</code>,
  190.      * the empty string "", or consisting wholly of whitespace.
  191.      * <p>The object whose field is being validated does not need to be passed
  192.      * in because the {@link Errors} instance can resolve field values by itself
  193.      * (it will usually hold an internal reference to the target object).
  194.      * @param errors the <code>Errors</code> instance to register errors on
  195.      * @param field the field name to check
  196.      * @param errorCode the error code, interpretable as message key
  197.      * @param errorArgs the error arguments, for argument binding via MessageFormat
  198.      * (can be <code>null</code>)
  199.      * @param defaultMessage fallback default message
  200.      */
  201.     public static void rejectIfEmptyOrWhitespace(
  202.             Errors errors, String field, String errorCode, Object[] errorArgs, String defaultMessage) {
  203.         Assert.notNull(errors, "Errors object must not be null");
  204.         Object value = errors.getFieldValue(field);
  205.         if (value == null ||!StringUtils.hasText(value.toString())) {
  206.             errors.rejectValue(field, errorCode, errorArgs, defaultMessage);
  207.         }
  208.     }
  209. }

原创粉丝点击