fieldchecks.java

来自「这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用s」· Java 代码 · 共 888 行 · 第 1/3 页

JAVA
888
字号
     *any validation errors occur.
     *@param request Current request object.
     *@return A Byte if valid, null otherwise.
     */
    public static Byte validateByte(Object bean,
                                    ValidatorAction va, Field field,
                                    ActionErrors errors,
                                    HttpServletRequest request) {

        Byte result = null;
        String value = null;
        if (isString(bean)) {
            value = (String) bean;
        } else {
            value = ValidatorUtil.getValueAsString(bean, field.getProperty());
        }

        if (!GenericValidator.isBlankOrNull(value)) {
            result = GenericTypeValidator.formatByte(value);

            if (result == null) {
                errors.add(field.getKey(), Resources.getActionError(request, va, field));
            }
        }

        return result;
    }


    /**
     * Checks if the field can safely be converted to a short primitive.
     *
     * @param bean The bean validation is being performed on.
     * @param va The <code>ValidatorAction</code> that is currently being performed.
     * @param field The <code>Field</code> object associated with the current 
     * field being validated.
     * @param errors The <code>ActionErrors</code> object to add errors to if 
     * any validation errors occur.
     * @param request Current request object.
     * @return A Short if valid, otherwise null.
     */
    public static Short validateShort(Object bean,
                                      ValidatorAction va, Field field,
                                      ActionErrors errors,
                                      HttpServletRequest request) {
        Short result = null;
        String value = null;
        if (isString(bean)) {
            value = (String) bean;
        } else {
            value = ValidatorUtil.getValueAsString(bean, field.getProperty());
        }

        if (!GenericValidator.isBlankOrNull(value)) {
            result = GenericTypeValidator.formatShort(value);

            if (result == null) {
                errors.add(field.getKey(), Resources.getActionError(request, va, field));
            }
        }

        return result;
    }


    /**
     * Checks if the field can safely be converted to an int primitive.
     *
     * @param  bean     The bean validation is being performed on.
     * @param  va       The <code>ValidatorAction</code> that is currently being performed.
     * @param  field    The <code>Field</code> object associated with the current
     *      field being validated.
     * @param  errors   The <code>ActionErrors</code> object to add errors to if any
     *      validation errors occur.
     * @param  request  Current request object.
     * @return          An Integer if valid, a null otherwise.
     */
    public static Integer validateInteger(Object bean,
                                          ValidatorAction va, Field field,
                                          ActionErrors errors,
                                          HttpServletRequest request) {
        Integer result = null;
        String value = null;
        if (isString(bean)) {
            value = (String) bean;
        } else {
            value = ValidatorUtil.getValueAsString(bean, field.getProperty());
        }

        if (!GenericValidator.isBlankOrNull(value)) {
            result = GenericTypeValidator.formatInt(value);

            if (result == null) {
                errors.add(field.getKey(), Resources.getActionError(request, va, field));
            }
        }

        return result;
    }


    /**
     * Checks if the field can safely be converted to a long primitive.
     *
     * @param  bean     The bean validation is being performed on.
     * @param  va       The <code>ValidatorAction</code> that is currently being performed.
     * @param  field    The <code>Field</code> object associated with the current
     *      field being validated.
     * @param  errors   The <code>ActionErrors</code> object to add errors to if any
     *      validation errors occur.
     * @param  request  Current request object.
     * @return          A Long if valid, a null otherwise.
     */
    public static Long validateLong(Object bean,
                                    ValidatorAction va, Field field,
                                    ActionErrors errors,
                                    HttpServletRequest request) {
        Long result = null;
        String value = null;
        if (isString(bean)) {
            value = (String) bean;
        } else {
            value = ValidatorUtil.getValueAsString(bean, field.getProperty());
        }

        if (!GenericValidator.isBlankOrNull(value)) {
            result = GenericTypeValidator.formatLong(value);

            if (result == null) {
                errors.add(field.getKey(), Resources.getActionError(request, va, field));
            }
        }

        return result;
    }


    /**
     * Checks if the field can safely be converted to a float primitive.
     *
     * @param  bean     The bean validation is being performed on.
     * @param  va       The <code>ValidatorAction</code> that is currently being performed.
     * @param  field    The <code>Field</code> object associated with the current
     *      field being validated.
     * @param  errors   The <code>ActionErrors</code> object to add errors to if any
     *      validation errors occur.
     * @param  request  Current request object.
     * @return A Float if valid, a null otherwise.
     */
    public static Float validateFloat(Object bean,
                                      ValidatorAction va, Field field,
                                      ActionErrors errors,
                                      HttpServletRequest request) {
        Float result = null;
        String value = null;
        if (isString(bean)) {
            value = (String) bean;
        } else {
            value = ValidatorUtil.getValueAsString(bean, field.getProperty());
        }

        if (!GenericValidator.isBlankOrNull(value)) {
            result = GenericTypeValidator.formatFloat(value);

            if (result == null) {
                errors.add(field.getKey(), Resources.getActionError(request, va, field));
            }
        }

        return result;
    }


    /**
     *  Checks if the field can safely be converted to a double primitive.
     *
     * @param  bean     The bean validation is being performed on.
     * @param  va       The <code>ValidatorAction</code> that is currently being performed.
     * @param  field    The <code>Field</code> object associated with the current
     *      field being validated.
     * @param  errors   The <code>ActionErrors</code> object to add errors to if any
     *      validation errors occur.
     * @param  request  Current request object.
     * @return A Double if valid, a null otherwise.
     */
    public static Double validateDouble(Object bean,
                                        ValidatorAction va, Field field,
                                        ActionErrors errors,
                                        HttpServletRequest request) {
        Double result = null;
        String value = null;
        if (isString(bean)) {
            value = (String) bean;
        } else {
            value = ValidatorUtil.getValueAsString(bean, field.getProperty());
        }

        if (!GenericValidator.isBlankOrNull(value)) {
            result = GenericTypeValidator.formatDouble(value);

            if (result == null) {
                errors.add(field.getKey(), Resources.getActionError(request, va, field));
            }
        }

        return result;
    }


    /**
     *  Checks if the field is a valid date. If the field has a datePattern variable,
     *  that will be used to format <code>java.text.SimpleDateFormat</code>. If the
     *  field has a datePatternStrict variable, that will be used to format <code>java.text.SimpleDateFormat</code>
     *  and the length will be checked so '2/12/1999' will not pass validation with
     *  the format 'MM/dd/yyyy' because the month isn't two digits. If no datePattern
     *  variable is specified, then the field gets the DateFormat.SHORT format for
     *  the locale. The setLenient method is set to <code>false</code> for all variations.
     *
     * @param  bean     The bean validation is being performed on.
     * @param  va       The <code>ValidatorAction</code> that is currently being performed.
     * @param  field    The <code>Field</code> object associated with the current
     *      field being validated.
     * @param  errors   The <code>ActionErrors</code> object to add errors to if any
     *      validation errors occur.
     * @param  request  Current request object.
     * @return A Date if valid, a null if blank or invalid.
     */
    public static Date validateDate(Object bean,
                                    ValidatorAction va, Field field,
                                    ActionErrors errors,
                                    HttpServletRequest request) {

        Date result = null;
        String value = null;
        if (isString(bean)) {
            value = (String) bean;
        } else {
            value = ValidatorUtil.getValueAsString(bean, field.getProperty());
        }
        String datePattern = field.getVarValue("datePattern");
        String datePatternStrict = field.getVarValue("datePatternStrict");
        Locale locale = Resources.getLocale(request);

        if (!GenericValidator.isBlankOrNull(value)) {
            try {
                if (datePattern != null && datePattern.length() > 0) {
                    result = GenericTypeValidator.formatDate(value, datePattern, false);
                } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
                    result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
                } else {
                    result = GenericTypeValidator.formatDate(value, locale);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }

            if (result == null) {
                errors.add(field.getKey(), Resources.getActionError(request, va, field));
            }
        }

        return result;
    }

    /**
     * Checks if a fields value is within a range (min &amp; max specified in the
     * vars attribute).
     * 
     *@deprecated  As of Struts 1.1, replaced by {@link #validateIntRange(java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionErrors,javax.servlet.http.HttpServletRequest)}
     *@param  bean     The bean validation is being performed on.
     *@param  va       The <code>ValidatorAction</code> that is currently being performed.
     *@param  field    The <code>Field</code> object associated with the current
     *      field being validated.
     *@param  errors   The <code>ActionErrors</code> object to add errors to if any
     *      validation errors occur.
     *@param  request  Current request object.
     *@return          True if in range, false otherwise.
     */
    public static boolean validateRange(Object bean,
                                        ValidatorAction va, Field field,
                                        ActionErrors errors,
                                        HttpServletRequest request) {
        return validateIntRange(bean, va, field, errors, request);
    }

    /**
     * Checks if a fields value is within a range (min &amp; max specified in the
     * vars attribute).
     *
     * @param  bean     The bean validation is being performed on.
     * @param  va       The <code>ValidatorAction</code> that is currently being performed.
     * @param  field    The <code>Field</code> object associated with the current
     *      field being validated.
     * @param  errors   The <code>ActionErrors</code> object to add errors to if any
     *      validation errors occur.
     * @param  request  Current request object.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?