JSF 2.0:设置验证时,复合组件内的 h:inputText 因非 String 对象而失败

发布于 2024-10-16 11:21:14 字数 1807 浏览 4 评论 0原文

在支持 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2024-10-23 11:21:14

根据对 您的票 的评论,事实证明,您可以作为解决方法明确指定类型转换器。

你可以按如下方式进行

<pref:fieldComponent text="#{bean.foo}">
    <f:converter converterId="javax.faces.Integer" />
</pref:fieldComponent>

<cc:implementation>
    <h:inputText id="field" value="#{cc.attrs.text}">
        <cc:insertChildren />
    </h:inputText>
</cc:implementation>

或者也许

<pref:fieldComponent text="#{bean.foo}" converter="javax.faces.Integer" />

并且

<cc:implementation>
    <h:inputText id="field" value="#{cc.attrs.text}" converter="#{cc.attrs.converter}" />
</cc:implementation>

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

<pref:fieldComponent text="#{bean.foo}">
    <f:converter converterId="javax.faces.Integer" />
</pref:fieldComponent>

and

<cc:implementation>
    <h:inputText id="field" value="#{cc.attrs.text}">
        <cc:insertChildren />
    </h:inputText>
</cc:implementation>

or maybe

<pref:fieldComponent text="#{bean.foo}" converter="javax.faces.Integer" />

and

<cc:implementation>
    <h:inputText id="field" value="#{cc.attrs.text}" converter="#{cc.attrs.converter}" />
</cc:implementation>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文