如何在JSF中创建自定义验证器

如何在JSF中创建自定义验证器

本文介绍了如何在JSF中执行验证,如何在JSF中创建自定义验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想使用某些Java bean方法在某些输入组件(例如<h:inputText>)中执行验证.我应该为此使用<f:validator><f:validateBean>吗?我在哪里可以了解到更多信息?

I would like to perform validation in some of my input components such as <h:inputText> using some Java bean method. Should I use <f:validator> or <f:validateBean> for this? Where can I read more about it?

推荐答案

您只需要实现 Validator 界面.

You just need to implement the Validator interface.

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

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        // ...

        if (valueIsInvalid) {
            throw new ValidatorException(new FacesMessage("Value is invalid!"));
        }
    }

}

@FacesValidator 将使用验证者ID myValidator将其注册到JSF,以便您可以在任何<h:inputXxx>/<h:selectXxx>组件的validator属性中引用它,如下所示:

The @FacesValidator will register it to JSF with validator ID myValidator so that you can reference it in validator attribute of any <h:inputXxx>/<h:selectXxx> component as follows:

<h:inputText id="foo" value="#{bean.foo}" validator="myValidator" />
<h:message for="foo" />

您还可以使用<f:validator>,这是打算在同一组件上附加多个验证器的唯一方法:

You can also use <f:validator>, which would be the only way if you intend to attach multiple validator on the same component:

<h:inputText id="foo" value="#{bean.foo}">
    <f:validator validatorId="myValidator" />
</h:inputText>
<h:message for="foo" />

每当验证者抛出ValidatorException时,其消息就会显示在与输入字段关联的<h:message>中.

Whenever the validator throws a ValidatorException, then its message will be displayed in the <h:message> associated with the input field.

您可以使用<f:validator binding>引用EL范围内某处的具体验证器实例,而后者又可以很容易地以lambda的形式提供:

You can use <f:validator binding> to reference a concrete validator instance somewhere in the EL scope, which in turn can easily be supplied as a lambda:

<h:inputText id="foo" value="#{bean.foo}">
    <f:validator binding="#{bean.validator}" />
</h:inputText>
<h:message for="foo" />
public Validator getValidator() {
    return (context, component, value) -> {
        // ...

        if (valueIsInvalid) {
            throw new ValidatorException(new FacesMessage("Value is invalid!"));
        }
    };
}

要更进一步,您可以使用JSR303 bean验证.这将基于注释验证字段.由于这将是一个完整的故事,因此这里只是一些入门指南:

To get a step further, you can use JSR303 bean validation. This validates fields based on annotations. Since it's going to be a whole story, here are just some links to get started:

  • Hibernate Validator - Getting started
  • JSF 2.0 tutorial - Finetuning validation

<f:validateBean>仅在您打算禁用 JSR303 bean验证时才有用.然后,将输入组件(甚至整个表单)放入<f:validateBean disabled="true">.

The <f:validateBean> is only useful if you intend to disable JSR303 bean validation. You then put the input components (or even the whole form) inside <f:validateBean disabled="true">.

  • JSF doesn't support cross-field validation, is there a workaround?
  • How to perform JSF validation in actionListener or action method?

这篇关于如何在JSF中执行验证,如何在JSF中创建自定义验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:36