JSF 2.0: h:inputText inside composite component fa

2019-02-15 19:53发布

问题:

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>