JSF 2.0: h:inputText inside composite component fails with non-String objects when validation is set

落爺英雄遲暮 提交于 2019-11-30 19:59:52

问题


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?


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

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


来源:https://stackoverflow.com/questions/4943490/jsf-2-0-hinputtext-inside-composite-component-fails-with-non-string-objects-wh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!