⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 doublerangefieldvalidator.java

📁 在Struts2中的jar包xwork的源代码.版本为2.0.7
💻 JAVA
字号:
/* * Copyright (c) 2002-2006 by OpenSymphony * All rights reserved. */package com.opensymphony.xwork2.validator.validators;import com.opensymphony.xwork2.validator.ValidationException;/** * <!-- START SNIPPET: javadoc --> * Field Validator that checks if the double specified is within a certain range. * <!-- END SNIPPET: javadoc --> * * * <!-- START SNIPPET: parameters --> * <ul> *                 <li>fieldName - The field name this validator is validating. Required if usingPlain-Validator Syntax otherwise not required</li> *                 <li>minInclusive - the minimum inclusive value in FloatValue format specified by Java language (if none is specified, it willnot be checked) </li> *                 <li>maxInclusive - the maximum inclusive value in FloatValue format specified by Java language (if none is specified, it willnot be checked) </li> *                 <li>minExclusive - the minimum exclusive value in FloatValue format specified by Java language (if none is specified, it willnot be checked) </li> *                 <li>maxExclusive - the maximum exclusive value in FloatValue format specified by Java language (if none is specified, it willnot be checked) </li> * </ul> * <!-- END SNIPPET: parameters --> * * * <pre> * <!-- START SNIPPET: examples --> *                 &lt;validators> *           &lt;!-- Plain Validator Syntax --&gt; *           &lt;validator type="double"> *               &lt;param name="fieldName"&gt;percentage&lt;/param&gt; *               &lt;param name="minInclusive"&gt;20.1&lt;/param&gt; *               &lt;param name="maxInclusive"&gt;50.1&lt;/param&gt; *               &lt;message&gt;Age needs to be between ${minInclusive} and${maxInclusive} (inclusive)&lt;/message&gt; *           &lt;/validator&gt; * *           &lt;!-- Field Validator Syntax --&gt; *           &lt;field name="percentage"&gt; *               &lt;field-validator type="double"&gt; *                   &lt;param name="minExclusive"&gt;0.123&lt;/param&gt; *                   &lt;param name="maxExclusive"&gt;99.98&lt;/param&gt; *                   &lt;message&gt;Percentage needs to be between ${minExclusive}and ${maxExclusive} (exclusive)&lt;/message&gt; *               &lt;/field-validator&gt; *           &lt;/field&gt; *      &lt;/validators&gt; * <!-- END SNIPPET: examples --> * </pre> * * @author Rainer Hermanns * @author Rene Gielen * * @version $Id: DoubleRangeFieldValidator.java 1184 2006-11-12 07:30:31Z tm_jee $ */// START SNIPPET: field-level-validatorpublic class DoubleRangeFieldValidator extends FieldValidatorSupport {        String maxInclusive = null;    String minInclusive = null;    String minExclusive = null;    String maxExclusive = null;    Double maxInclusiveValue = null;    Double minInclusiveValue = null;    Double minExclusiveValue = null;    Double maxExclusiveValue = null;    public void validate(Object object) throws ValidationException {        String fieldName = getFieldName();        Double value;        try {            Object obj = this.getFieldValue(fieldName, object);            if (obj == null) {                return;            }            value = Double.valueOf(obj.toString());        } catch (NumberFormatException e) {            return;        }        parseParameterValues();        if ((maxInclusiveValue != null && value.compareTo(maxInclusiveValue) > 0) ||                (minInclusiveValue != null && value.compareTo(minInclusiveValue) < 0) ||                (maxExclusiveValue != null && value.compareTo(maxExclusiveValue) >= 0) ||                (minExclusiveValue != null && value.compareTo(minExclusiveValue) <= 0)) {            addFieldError(fieldName, object);        }    }    private void parseParameterValues() {        this.minInclusiveValue = parseDouble(minInclusive);        this.maxInclusiveValue = parseDouble(maxInclusive);        this.minExclusiveValue = parseDouble(minExclusive);        this.maxExclusiveValue = parseDouble(maxExclusive);    }    private Double parseDouble (String value) {        if (value != null) {            try {                return Double.valueOf(value);            } catch (NumberFormatException e) {                if (log.isWarnEnabled()) {                    log.warn("DoubleRangeFieldValidator - [parseDouble]: Unable to parse given double parameter " + value);                }            }        }        return null;    }    public void setMaxInclusive(String maxInclusive) {        this.maxInclusive = maxInclusive;    }    public String getMaxInclusive() {        return maxInclusive;    }    public void setMinInclusive(String minInclusive) {        this.minInclusive = minInclusive;    }    public String getMinInclusive() {        return minInclusive;    }    public String getMinExclusive() {        return minExclusive;    }    public void setMinExclusive(String minExclusive) {        this.minExclusive = minExclusive;    }    public String getMaxExclusive() {        return maxExclusive;    }    public void setMaxExclusive(String maxExclusive) {        this.maxExclusive = maxExclusive;    }}// END SNIPPET: field-level-validator

⌨️ 快捷键说明

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