【问题标题】:Validating int value验证 int 值
【发布时间】:2011-08-16 09:25:50
【问题描述】:

我在对具有值的文本字段进行简单验证时遇到了困难 - 一个 int 变量。

我需要以下;

  • 只允许数字
  • 不允许使用 0 或以下的值
  • 必须按照上述规则填写该字段。

这是我创建的验证器,但它没有按我的意愿工作。你能看看它并告诉我我哪里出错了吗?

public void validateProductValue(FacesContext context,
            UIComponent validate, Object value) {
        FacesMessage msg = new FacesMessage("");
        String inputFromField = (String) value;
        String simpleTextPatternText = "^([0-9]+$)?";
        Pattern textPattern = null;
        Matcher productValueMatcher = null;
        textPattern = Pattern.compile(simpleTextPatternText);
        productValueMatcher = textPattern.matcher(inputFromField);      

        if (!productValueMatcher.matches()) {
            msg = new FacesMessage("Only digits allowed");
            throw new ValidatorException(msg);
        }

        if (inputFromField.length() <= 0) {
            msg = new FacesMessage("You must enter a value greater than 0");
            throw new ValidatorException(msg);
        }

        if (Integer.parseInt(inputFromField.toString().trim()) <= 0) {
            msg = new FacesMessage("0 or bellow is not permited");
            throw new ValidatorException(msg);
        }       
    }

这就是我对这个领域的称呼:

<h:inputText id="productValue" value="#{newOfferSupportController.productValue}" validator="#{newOfferSupportController.validateProductValue}"/>

这是在浏览器中显示的验证:

文字

/newoffer.xhtml @44,184 
validator="#{newOfferSupportController.validateProductValue}":
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

【问题讨论】:

  • 验证信息难以阅读,能否复制文字或放大图片?
  • 您发布的错误表明您正在传递一个 Integer 对象,并且尝试将其转换为字符串失败。

标签: java validation jsf jsf-2


【解决方案1】:

您应该能够仅使用正则表达式来强制执行您查看的条件: ^[1-9]+[0-9]*$

【讨论】:

    【解决方案2】:

    首先你的正则表达式是错误的。你需要把$放在最后,像这样^([0-9]+)?$

    这并不像你想象的那样工作:

    if (inputFromField.length() <= 0) {
      msg = new FacesMessage("You must enter a value greater than 0");
      throw new ValidatorException(msg);
    }
    

    inputFormField.length() 是文本的长度,不能小于 0,即使大于 0 也可以输入负值,例如"-1",长度为 2。

    如果我正确地看到了异常,你会得到一个 ClassCastException。看一下第 44 行(异常消息告诉您),我猜是String inputFromField = (String) value; 您是否为该字段定义了转换器?如果是这样,value 可能是 Integer 而不是 String

    编辑:

    请注意,您的验证器实际上会尝试做两件事:将输入转换为整数并验证整数值。在 JSF 中,您通常有两个类来执行此操作:

    • 首先调用一个转换器,以便将字符串转换为整数,反之亦然,特别是如果您的模型字段是整数 - 在这里您可以检查您是否真的得到了整数
    • 第二个验证整数值,例如通过强制执行最低要求 - 在这里您使用的是验证器

    还要注意,已经有内置的验证器,可以做你想做的事。以&lt;f:validateLongRange minimum = "0"/&gt; 为例。

    【讨论】:

      【解决方案3】:

      首先, value 实际上是 String 的一个实例吗?还是可以将其转换为整数?

      如果没有,请执行以下操作:

      if (value == null) {
         throw new ValidatorException("No input value");
      }
      
      String inputValue = (String) value;
      
      try {
         int v = Integer.parse(inputValue);
      
         if (v <= 0) {
            throw new ValidatorException("Input less than or equal to 0");
         }     
      
      
      } catch (NumberFormatException e) {
         throw new ValidatorException("Input is not an integer");
      }
      

      【讨论】:

      • 如果 value 不是一个字符串,你无论如何都会得到 ClassCastException。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 2014-08-11
      • 2020-09-13
      • 2015-08-02
      • 2018-07-07
      • 2021-08-27
      • 1970-01-01
      相关资源
      最近更新 更多