复合中的Validator在使用java.lang.Double值时抛出ClassCastException

时间:2015-04-29 13:43:36

标签: validation jsf double classcastexception composite-component

我有两个字段验证器在没有复合的情况下工作如下

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>

但转换为复合后,我只在

上得到java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double:

Double firstDouble = (Double) value;而不是

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

这里是复合

<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}" 
     />

奇怪的是,一个值作为String而另一个值作为Double出现。我只是为了我的第一个双倍更改为Double firstDouble = Double.parseDouble(value);而不担心为什么component.getAttributes()仍然作为double(Java Bean中的属性的实际类型)传递给验证器,即使复合似乎这两个领域一样,只是继续前进?

修改

这是我的复合界面。即使在指定类型之后,我仍然会得到类强制转换异常

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

1 个答案:

答案 0 :(得分:1)

我无法重现你的问题。即使在type中没有明确的<cc:attribute type="java.lang.Double">属性,它也适用于Tomcat 8.0.21上的Mojarra 2.2.10。

作为一种解决方法,您可以在复合的输入组件中明确声明转换器:

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

但是Mojarra 2.1.7真的很老了(2012年2月)。您应该考虑升级到最新的2.1.x,目前是2.1。29(2014年7月),甚至可能是2.2.x.您可以在以下答案中找到JBoss服务器Mojarra升级说明:Upgrade JSF / Mojarra in JBoss AS / EAP / WildFly