JSF 2.0:设置验证后,复合组件内的h:inputText会因非String对象而失败 [英] JSF 2.0: h:inputText inside composite component fails with non-String objects when validation is set

查看:92
本文介绍了JSF 2.0:设置验证后,复合组件内的h:inputText会因非String对象而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在备用bean中:

@Min(3)
Integer foo;

如果我有这样的表格:

<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>

并在内部表单中调用它,而不是直接在h:inputText中调用:

and call this inside form instead of directly h:inputText as in:

<!-- <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,则它也可以与验证一起使用.

If I remove validation, it works. Also, if foo is of type String, it works also with validations.

我尝试使用cc:editableValueHolder,定义了不同的类型(也省略了它)和其他一些技巧,但是我不确定如何实际实现它.还是一个错误?好像忘记了使用转换器?我误会了吗?

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?

推荐答案

根据对,事实证明,您可以作为解决方法来明确指定类型转换器.

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>

<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>

这篇关于JSF 2.0:设置验证后,复合组件内的h:inputText会因非String对象而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆