![](/img/trans.png)
[英]JSF 2 — Composite component with optional listener attribute on f:ajax
[英]JSF composite:attribute with f:attribute conversion error
我正在实现一个JSF组件,需要有条件地添加一些属性。 这个问题类似于以前的JSF:p:dataTable with f:attribute导致“参数类型不匹配”错误 ,但是有一个完全不同的错误消息,所以我提出了一个新问题。
<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
错误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}">
对于EL和复合组件属性中的数字,这确实是一个不幸的角落案例。 对此没有解决方案。 在<f:attribute>
使用时,类型信息在#{cc.attrs}
不可用,因此被视为String
。 #{15}
也不能在EL中表示为整数,当缺少类型信息时,所有数字总是被隐式地视为Long
。 可以使用标记文件而不是复合组件来防止ClassCastException
。
您最好的选择是检查实际的rows
属性本身。
<p:dataTable ... rows="#{cc.attrs.filter ? cc.attrs.rows : null}">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.