1

我正在开发一个自定义的 JSF UIInput,它将期望用户提供一个特制的值。为了验证用户输入,我需要运行一系列仅与手头的应用程序相关的检查。这不仅仅是一个普通的电子邮件或电话号码输入字段。这是一个文本输入,它有一个复合值,需要将其解构为多个部分,并且每个部分都需要进行验证。

我设法创建了组件本身。

@FacesComponent("MyComponent")
public class MyComponent extends UIInput {
   public String getFamily() {...}

   encodeBegin() {...}
   encodeEnd() {...}

}

mycomponent.taglib.xml捆绑在里面META-INF,我可以成功使用和测试组件。

我还实现了一个验证器类,它可以完成所有工作。准备好了。

public class AutomationDetectionValidator implements Validator {
   public void validate(...) throws ValidatorException {...}
}

但是,我需要在该组件中包含一个自定义验证器作为默认值。我可以手动执行此操作,如下所示:

<custom:myComponent>
    <f:validator validatorId=”myComponentValidator”/>
</custom:myComponent>

但预期的用法是

<custom:myComponent/>

验证器应该自动在那里。

我现在整天都在谷歌搜索,但我可以找到一些我们不想要的用法的基本示例。

4

1 回答 1

2

只需将其添加到组件的构造函数中即可。

public MyComponent() {
    addValidator(new AutomationDetectionValidator());
}
于 2013-06-21T19:25:45.827 回答