在 JSF 中验证没有自定义验证器的数字值

Validate a number value without custom validator in JSF

我有一个 JSF 表单。当用户在 qty 字段中输入 0 并单击 Add To Card 按钮时,我希望显示一条消息。

这是 JSF 表单:

<h:form>
      <h:inputText id="qtyField" value="#{booksBean.qty}">
          <!--What kind of validation should i use here?-->
          <f:ajax event="blur" render="qtyMsg"/>
       </h:inputText>

       <h:message id="qtyMsg" for="qtyField"/>

       <h:commandButton value="Add To Card"
                        action="#{booksBean.orderBook()}"
                        rendered="#{booksBean.qty>0}">
           <f:ajax execute="@form" rendered="@form"/>
       </h:commandButton>
</h:form>

我是否需要自定义验证器 class 只是为了简单地将数字值与零进行比较?

像这样:

@FacesValidator("myValidator")
public class MyValidator implements Validator {

@Override
public void validate(FacesContext context, UIComponent component, Object value) {

   if (intValue== 0 || intValue <0) {
      throw new ValidatorException(new FacesMessage(...));
   }
    //...
}

有没有不创建自定义验证器的更短的方法class?

您可以为此使用 f:validateLongRange

<h:inputText value="#{backingBean.input1}">
    <f:validateLongRange minimum="1" />
</h:inputText>

Checks whether the local value of a component is within a certain range. The value must be any numeric type or String that can be converted to a long.