验证方法(Spring Action)
来源:互联网 发布:linux opengl 安装 编辑:程序博客网 时间:2024/06/02 00:21
- /*
- * Copyright 2002-2007 the original author or authors.
- *
- * 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 org.springframework.validation;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.util.Assert;
- import org.springframework.util.StringUtils;
- /**
- * Utility class offering convenient methods for invoking a {@link Validator}
- * and for rejecting empty fields.
- *
- * <p>Checks for an empty field in <code>Validator</code> implementations can become
- * one-liners when using {@link #rejectIfEmpty} or {@link #rejectIfEmptyOrWhitespace}.
- *
- * @author Juergen Hoeller
- * @author Dmitriy Kopylenko
- * @since 06.05.2003
- * @see Validator
- * @see Errors
- */
- public abstract class ValidationUtils {
- private static Log logger = LogFactory.getLog(ValidationUtils.class);
- /**
- * Invoke the given {@link Validator} for the supplied object and
- * {@link Errors} instance.
- * @param validator the <code>Validator</code> to be invoked (must not be <code>null</code>)
- * @param obj the object to bind the parameters to
- * @param errors the {@link Errors} instance that should store the errors (must not be <code>null</code>)
- * @throws IllegalArgumentException if either of the <code>Validator</code> or <code>Errors</code> arguments is <code>null</code>;
- * or if the supplied <code>Validator</code> does not {@link Validator#supports(Class) support}
- * the validation of the supplied object's type
- */
- public static void invokeValidator(Validator validator, Object obj, Errors errors) {
- Assert.notNull(validator, "Validator must not be null");
- Assert.notNull(errors, "Errors object must not be null");
- if (logger.isDebugEnabled()) {
- logger.debug("Invoking validator [" + validator + "]");
- }
- if (obj != null && !validator.supports(obj.getClass())) {
- throw new IllegalArgumentException(
- "Validator [" + validator.getClass() + "] does not support [" + obj.getClass() + "]");
- }
- validator.validate(obj, errors);
- if (logger.isDebugEnabled()) {
- if (errors.hasErrors()) {
- logger.debug("Validator found " + errors.getErrorCount() + " errors");
- }
- else {
- logger.debug("Validator found no errors");
- }
- }
- }
- /**
- * Reject the given field with the given error code if the value is empty.
- * <p>An 'empty' value in this context means either <code>null</code> or
- * the empty string "".
- * <p>The object whose field is being validated does not need to be passed
- * in because the {@link Errors} instance can resolve field values by itself
- * (it will usually hold an internal reference to the target object).
- * @param errors the <code>Errors</code> instance to register errors on
- * @param field the field name to check
- * @param errorCode the error code, interpretable as message key
- */
- public static void rejectIfEmpty(Errors errors, String field, String errorCode) {
- rejectIfEmpty(errors, field, errorCode, null, null);
- }
- /**
- * Reject the given field with the given error code and default message
- * if the value is empty.
- * <p>An 'empty' value in this context means either <code>null</code> or
- * the empty string "".
- * <p>The object whose field is being validated does not need to be passed
- * in because the {@link Errors} instance can resolve field values by itself
- * (it will usually hold an internal reference to the target object).
- * @param errors the <code>Errors</code> instance to register errors on
- * @param field the field name to check
- * @param errorCode error code, interpretable as message key
- * @param defaultMessage fallback default message
- */
- public static void rejectIfEmpty(Errors errors, String field, String errorCode, String defaultMessage) {
- rejectIfEmpty(errors, field, errorCode, null, defaultMessage);
- }
- /**
- * Reject the given field with the given error codea nd error arguments
- * if the value is empty.
- * <p>An 'empty' value in this context means either <code>null</code> or
- * the empty string "".
- * <p>The object whose field is being validated does not need to be passed
- * in because the {@link Errors} instance can resolve field values by itself
- * (it will usually hold an internal reference to the target object).
- * @param errors the <code>Errors</code> instance to register errors on
- * @param field the field name to check
- * @param errorCode the error code, interpretable as message key
- * @param errorArgs the error arguments, for argument binding via MessageFormat
- * (can be <code>null</code>)
- */
- public static void rejectIfEmpty(Errors errors, String field, String errorCode, Object[] errorArgs) {
- rejectIfEmpty(errors, field, errorCode, errorArgs, null);
- }
- /**
- * Reject the given field with the given error code, error arguments
- * and default message if the value is empty.
- * <p>An 'empty' value in this context means either <code>null</code> or
- * the empty string "".
- * <p>The object whose field is being validated does not need to be passed
- * in because the {@link Errors} instance can resolve field values by itself
- * (it will usually hold an internal reference to the target object).
- * @param errors the <code>Errors</code> instance to register errors on
- * @param field the field name to check
- * @param errorCode the error code, interpretable as message key
- * @param errorArgs the error arguments, for argument binding via MessageFormat
- * (can be <code>null</code>)
- * @param defaultMessage fallback default message
- */
- public static void rejectIfEmpty(
- Errors errors, String field, String errorCode, Object[] errorArgs, String defaultMessage) {
- Assert.notNull(errors, "Errors object must not be null");
- Object value = errors.getFieldValue(field);
- if (value == null || !StringUtils.hasLength(value.toString())) {
- errors.rejectValue(field, errorCode, errorArgs, defaultMessage);
- }
- }
- /**
- * Reject the given field with the given error code if the value is empty
- * or just contains whitespace.
- * <p>An 'empty' value in this context means either <code>null</code>,
- * the empty string "", or consisting wholly of whitespace.
- * <p>The object whose field is being validated does not need to be passed
- * in because the {@link Errors} instance can resolve field values by itself
- * (it will usually hold an internal reference to the target object).
- * @param errors the <code>Errors</code> instance to register errors on
- * @param field the field name to check
- * @param errorCode the error code, interpretable as message key
- */
- public static void rejectIfEmptyOrWhitespace(Errors errors, String field, String errorCode) {
- rejectIfEmptyOrWhitespace(errors, field, errorCode, null, null);
- }
- /**
- * Reject the given field with the given error code and default message
- * if the value is empty or just contains whitespace.
- * <p>An 'empty' value in this context means either <code>null</code>,
- * the empty string "", or consisting wholly of whitespace.
- * <p>The object whose field is being validated does not need to be passed
- * in because the {@link Errors} instance can resolve field values by itself
- * (it will usually hold an internal reference to the target object).
- * @param errors the <code>Errors</code> instance to register errors on
- * @param field the field name to check
- * @param errorCode the error code, interpretable as message key
- * @param defaultMessage fallback default message
- */
- public static void rejectIfEmptyOrWhitespace(
- Errors errors, String field, String errorCode, String defaultMessage) {
- rejectIfEmptyOrWhitespace(errors, field, errorCode, null, defaultMessage);
- }
- /**
- * Reject the given field with the given error code and error arguments
- * if the value is empty or just contains whitespace.
- * <p>An 'empty' value in this context means either <code>null</code>,
- * the empty string "", or consisting wholly of whitespace.
- * <p>The object whose field is being validated does not need to be passed
- * in because the {@link Errors} instance can resolve field values by itself
- * (it will usually hold an internal reference to the target object).
- * @param errors the <code>Errors</code> instance to register errors on
- * @param field the field name to check
- * @param errorCode the error code, interpretable as message key
- * @param errorArgs the error arguments, for argument binding via MessageFormat
- * (can be <code>null</code>)
- */
- public static void rejectIfEmptyOrWhitespace(
- Errors errors, String field, String errorCode, Object[] errorArgs) {
- rejectIfEmptyOrWhitespace(errors, field, errorCode, errorArgs, null);
- }
- /**
- * Reject the given field with the given error code, error arguments
- * and default message if the value is empty or just contains whitespace.
- * <p>An 'empty' value in this context means either <code>null</code>,
- * the empty string "", or consisting wholly of whitespace.
- * <p>The object whose field is being validated does not need to be passed
- * in because the {@link Errors} instance can resolve field values by itself
- * (it will usually hold an internal reference to the target object).
- * @param errors the <code>Errors</code> instance to register errors on
- * @param field the field name to check
- * @param errorCode the error code, interpretable as message key
- * @param errorArgs the error arguments, for argument binding via MessageFormat
- * (can be <code>null</code>)
- * @param defaultMessage fallback default message
- */
- public static void rejectIfEmptyOrWhitespace(
- Errors errors, String field, String errorCode, Object[] errorArgs, String defaultMessage) {
- Assert.notNull(errors, "Errors object must not be null");
- Object value = errors.getFieldValue(field);
- if (value == null ||!StringUtils.hasText(value.toString())) {
- errors.rejectValue(field, errorCode, errorArgs, defaultMessage);
- }
- }
- }
- 验证方法(Spring Action)
- spring+struts结合在第一次验证失败之后,不再走action的方法,直接进入input
- struts验证、jsp表单验证、action验证方法总结
- Struts2对Action提交方法进行验证
- 关于以validation方法的验证所有的action方法(代码实例)
- 使用Spring Mock测试Action中的方法
- Spring+Struts2整合:action方法找不到
- Spring boot 方法参数验证
- spring aop action中验证用户登录状态
- spring ioc 笔记(Spring in action)
- Struts2 方法动态调用,验证表签,xml配置Action
- Spring MVC 学习总结(三)——请求处理方法Action详解
- Spring MVC 学习总结(三)——请求处理方法Action详解
- spring in action 学习笔记(三)通过工厂方法创建bean
- spring中方法级验证参数
- spring mvc4 hibernate validator 方法参数验证
- Struts中action的方法不能纳入spring事务管理
- 浅析如何让Spring 来管理Action的配置方法
- Erlang概述收藏
- SOA标准发展混乱 国内业务缺少经验
- Erlang 是什么
- 迅雷协议分析
- 结合栈空间巧用单件
- 验证方法(Spring Action)
- COLORREF结构
- 唐朝“楼市”也曾崩盘 朝廷没救市
- 访TechEd2008独家7段视频曝光
- libnet使用举例(9)
- acpi 驱动有问题(显示黄叹号)解决
- 几种无线接入协议有什么区别?
- 数据库设计范式深入浅出
- 在不同操作系统中安装SNMP