JSF 2.0:设置验证时,复合组件内的 h:inputText 因非 String 对象而失败
在支持 bean 中:
@Min(3)
Integer foo;
如果我有这样的形式:
<h:form>
<h:commandButton value="Submit" />
<h:inputText value="#{bean.foo}" />
</h:form>
这可以正常工作。但是,如果我做类似的事情
<cc:interface>
<cc:attribute name="text" />
<cc:editableValueHolder name="text" targets="field" />
<cc:interface>
<cc:implementation>
<h:inputText id="field" value="#{cc.attrs.text}" />
</cc:implementation>
并在表单内部调用它而不是直接 h:inputText
,如下所示:
<!-- <h:inputText value="#{bean.foo}" /> -->
<pref:fieldComponent text="#{bean.foo}" />
但然后我得到:
javax.validation.ValidationException: Unexpected exception during isValid call
at org.hibernate.validator.engine.ConstraintTree.validateSingleConstraint(ConstraintTree.java:144)
at org.hibernate.validator.engine.ConstraintTree.validateConstraints(ConstraintTree.java:118)
at org.hibernate.validator.metadata.MetaConstraint.validateConstraint(MetaConstraint.java:121)
at org.hibernate.validator.engine.ValidatorImpl.validateValueForGroup(ValidatorImpl.java:655)
...
根本原因是:
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number
at org.hibernate.validator.constraints.impl.MinValidatorForNumber.isValid(MinValidatorForNumber.java:32)
at org.hibernate.validator.engine.ConstraintTree.validateSingleConstraint(ConstraintTree.java:141)
... 69 more
如果我删除验证,它就会起作用。此外,如果 foo
的类型为 String
,它也适用于验证。
我尝试使用 cc:editableValueHolder,定义不同的类型(也省略它)和其他一些技巧,但我有点不确定如何实际实现它。或者这是一个错误?似乎忘记使用转换器?我是不是误会了什么?
In a backing bean:
@Min(3)
Integer foo;
If I have form like:
<h:form>
<h:commandButton value="Submit" />
<h:inputText value="#{bean.foo}" />
</h:form>
This works ok. However, if I do something like
<cc:interface>
<cc:attribute name="text" />
<cc:editableValueHolder name="text" targets="field" />
<cc:interface>
<cc:implementation>
<h:inputText id="field" value="#{cc.attrs.text}" />
</cc:implementation>
and call this inside form instead of directly h:inputText
as in:
<!-- <h:inputText value="#{bean.foo}" /> -->
<pref:fieldComponent text="#{bean.foo}" />
But then I get:
javax.validation.ValidationException: Unexpected exception during isValid call
at org.hibernate.validator.engine.ConstraintTree.validateSingleConstraint(ConstraintTree.java:144)
at org.hibernate.validator.engine.ConstraintTree.validateConstraints(ConstraintTree.java:118)
at org.hibernate.validator.metadata.MetaConstraint.validateConstraint(MetaConstraint.java:121)
at org.hibernate.validator.engine.ValidatorImpl.validateValueForGroup(ValidatorImpl.java:655)
...
And the root cause is:
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number
at org.hibernate.validator.constraints.impl.MinValidatorForNumber.isValid(MinValidatorForNumber.java:32)
at org.hibernate.validator.engine.ConstraintTree.validateSingleConstraint(ConstraintTree.java:141)
... 69 more
If I remove validation, it works. Also, if foo
is of type String
, it works also with validations.
I tried playing with cc:editableValueHolder
, defining different types (also omitting it) and a few other tricks but I am a bit unsure how to actually implement this. Or is it a bug? Seems like it's forgetting to use a converter? Have I misunderstood something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据对 您的票 的评论,事实证明,您可以作为解决方法明确指定类型转换器。
你可以按如下方式进行
,
或者也许
并且
As per a comment on your ticket, it turns out that you could as workaround explicitly specify the type converter.
You could do it as follows
and
or maybe
and