如何为复合组件内的输入组件指定验证器?
如果我使用 JSF 2.0.4,是否应该在 faces-config.xml 中注册自定义验证器? 我的自定义验证器使用 Validator 接口,即 javax.faces.validator.Validator。
<cc:myComp id="customcomp1" ... />
<cc:myComp id="customcomp2" ...>
<f:validator id="myvalidator" for="myComp" />
</cc:myComp>
myComp.xhtml
<cc:interface>
<cc:attribute ... />
<!-- more attributes -->
</cc:interface>
<cc:implementation>
<h:panelGroup layout="block">
<h:inputText id="firstName" ... />
<h:inputText id="middleName" ... />
<h:inputText id="lastName" ... />
</h:panelGroup>
</cc:implementation>
Should I register a custom validator in faces-config.xml
if I'm using JSF 2.0.4?
My custom validator uses Validator interface which is javax.faces.validator.Validator
.
<cc:myComp id="customcomp1" ... />
<cc:myComp id="customcomp2" ...>
<f:validator id="myvalidator" for="myComp" />
</cc:myComp>
myComp.xhtml
<cc:interface>
<cc:attribute ... />
<!-- more attributes -->
</cc:interface>
<cc:implementation>
<h:panelGroup layout="block">
<h:inputText id="firstName" ... />
<h:inputText id="middleName" ... />
<h:inputText id="lastName" ... />
</h:panelGroup>
</cc:implementation>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据更新问题中的代码示例,您似乎根本没有将验证器委托给正确的输入,因此验证器完全被忽略。
您需要将所需的输入(您想要为其附加验证器)定义为
中<复合:接口>
。上面的
基本上告诉我们,任何
都必须应用于
。因此,应该执行以下操作:您甚至可以在
name
和targets
中使用相同的值,但关键点是必须有一个<; composite:editableValueHolder>
的存在是为了让 JSF 知道验证器应该针对哪个输入组件,即复合体中可以有多个输入组件,您瞧。As per the code example in your updated question, you seem to not be delegating the validator to the right input at all, so the validator is simply ignored altogether.
You need to define the desired input (for which you'd like to attach a validator) as a
<composite:editableValueHolder>
in the<composite:interface>
.The above
<composite:editableValueHolder>
basically tells that any<f:validator for="forName">
must be applied on the<h:inputText id="inputId">
. So, the following should then do it:You can even use the same value in
name
andtargets
, but the key point is that there must be a<composite:editableValueHolder>
present so that JSF knows on what input component exactly the validator should be targeted, there can namely be more than one input component in the composite, you see.如果您正在使用 JSF 2,我认为您不需要修改faces-config.xml 文件来创建客户验证器。您可以简单地使用注释
@FacesValidator
来声明Validator
。它应该是这样的:然后您可以开始在 .xhtml 页面中使用它,例如
标记:If you are working with JSF 2, I don't think you need to touch the
faces-config.xml
file to create a customerValidator
. You can simply use the annotation@FacesValidator
to declare aValidator
. It should be something like this:Then you can start using it in your .xhtml page with, for instance,
<f:validator>
tag:不,这对于 Jsf 2.0 来说是不必要的。只需使用
@FacesValidator
注释您的验证器。注释会自动注册您的验证器。不需要 XML。No. That is not necessary with Jsf 2.0. Just annotate your validator with
@FacesValidator
. The annotation registers your validator automatically. No xml needed.