我正在实现一个JSF组件,并且需要有条件地添加一些属性。这个问题与之前的JSF: p:dataTable with f:attribute results in "argument type mismatch" error类似,但错误消息完全不同,所以我提出了一个新问题。
<composite:interface>
<composite:attribute name="filter" required="false" default="false"
type="java.lang.Boolean"/>
<composite:attribute name="rows" required="false" default="15"
type="java.lang.Integer"/>
...
</composite:interface>
<composite:implementation>
<p:dataTable ivar="p" value="#{cc.attrs.dm}">
<c:if test="#{cc.attrs.filter}">
<f:attribute name="paginator" value="#{true}"/>
<f:attribute name="rows" value="#{cc.attrs.rows}"/>
</c:if>
...
<p:dataTable>
</composite:implementation>这将导致错误java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer。即使我手动设置,我也会得到错误:
<f:attribute name="rows" value="15"/> ... argument type mismatch
<f:attribute name="rows" value="#{15}"/> ... java.lang.Long cannot be cast
to java.lang.Integer如果我直接添加属性,则没有任何异常,并显示正确的行数:
<p:dataTable var="p" value="#{cc.attrs.dm}" rows="#{cc.attrs.rows}">发布于 2012-10-22 20:47:43
这确实是一个不幸的角落案例,在EL和复合组件属性中有数字。这是没有解决方案的。当在<f:attribute>中使用时,类型信息在#{cc.attrs}中不可用,因此被视为String。在EL中,#{15}也不能表示为整数,当缺少类型信息时,所有数字都被隐式地视为Long。可以通过使用标记文件而不是复合组件来防止ClassCastException。
最好的方法是检查实际的rows属性本身。
<p:dataTable ... rows="#{cc.attrs.filter ? cc.attrs.rows : null}">https://stackoverflow.com/questions/13009280
复制相似问题