1

I had a two field validator working without the composite as follows

public class DoubleMustBeGreaterThanSecondDouble implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        Double firstDouble = (Double) value;
        Double secondDouble = (Double) component.getAttributes().get("secondDouble");

        if (firstDouble <= secondDouble) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, null, null);
            throw new ValidatorException(msg);
        }
    }
}
<p:inputText size="5"
    validatorMessage="Plate maximum thickness must exceed plate minimum thickness"
    value="#{calculator.boltAnalysis.plate1.maximum_thickness}">
                <f:validator
                    validatorId="doubleMustBeGreaterThanSecondDouble" />
                <f:attribute name="secondDouble"
                    value="#{calculator.boltAnalysis.plate1.minimum_thickness}" />                                                                                                                                  
    <p:ajax event="change" update="@this" />
</p:inputText>

But after converting to a composite I get java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double: on just the

Double firstDouble = (Double) value; and not on the

Double secondDouble = (Double) component.getAttributes().get("secondDouble");

here it is as the composite

<p:inputText size="5"
    validatorMessage="Plate maximum thickness must exceed plate minimum thickness"
    value="#{cc.attrs.plateMaximumThickness}">
                <f:validator
                    validatorId="doubleMustBeGreaterThanSecondDouble" />
                <f:attribute name="secondDouble"
                    value="#{cc.attrs.plateMinimumThickness}" />                                                                                                                                  
    <p:ajax event="change" update="@this" />
</p:inputText>
<stk:plate
        plateLabel="3" 
        plateMinimumThickness="#{calculator.boltAnalysis.plate3.minimum_thickness}"  
        plateMaximumThickness="#{calculator.boltAnalysis.plate3.maximum_thickness}" 
     />

It is weird that one value comes over as a String and the other as a Double. Do I just change to Double firstDouble = Double.parseDouble(value); for my first double and not worry why the component.getAttributes() is still passed to the validator as a double (the actual type of the attribute in the Java Bean) even though the composite seems the same as far as those two fields go and just move on?

EDIT

Here is my composite interface. Even after specifying the type, I still get class cast exception

<composite:interface>
   <composite:attribute name="plateLabel" />
   <composite:attribute name="plateMinimumThickness" type="java.lang.Double"  />
   <composite:attribute name="plateMaximumThickness" type="java.lang.Double" />
</composite:interface>

<composite:implementation>
   ...
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
jeff
  • 3,618
  • 9
  • 48
  • 101
  • This information is missing in the question, but did you explicitly specify the value type in composite attribute as in ``? – BalusC Apr 29 '15 at 13:51
  • at first no, but then I tried. . Seeing you have it as fully qualified, looks like I need to try that next. – jeff Apr 29 '15 at 14:04
  • @BalusC specifying type="java.lang.Double" in my interface did not work. Should it? – jeff Apr 29 '15 at 14:12
  • 1
    I can't reproduce your problem. It works for me with Mojarra 2.2.10 on Tomcat 8.0.21 even without explicit `type` attribute. Which JSF impl/version are you using? As a workaround you could try to explicitly declare a converter as in ``. – BalusC Apr 30 '15 at 07:50
  • Mojarra 2.1.7-jbossorg-1, JSF 2.0.1.Final, jboss-as-7.1.1.Final. Adding converter="javax.faces.Double" works! – jeff Apr 30 '15 at 14:11

1 Answers1

1

I can't reproduce your problem. It works for me with Mojarra 2.2.10 on Tomcat 8.0.21 even without explicit type attribute in <cc:attribute type="java.lang.Double">.

As a workaround you could explicitly declare a converter as below in composite's input component:

<p:inputText ... converter="javax.faces.Double">

But Mojarra 2.1.7 is really old (Feb 2012). You should consider upgrading to latest 2.1.x, which is currently 2.1.29 (Jul 2014) or perhaps even 2.2.x. You can find JBoss server Mojarra upgrade instructions in this answer: Upgrade JSF / Mojarra in JBoss AS / EAP / WildFly.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555