gpt4 book ai didi

JSF 复合 :attribute with f:attribute conversion error

转载 作者:行者123 更新时间:2023-12-01 02:30:56 25 4
gpt4 key购买 nike

我正在实现一个 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}">

最佳答案

这确实是一个不幸的极端情况,EL 中的数字和复合组件属性。对此没有解决办法。类型信息在 #{cc.attrs} 中不可用在 <f:attribute> 中使用时并因此被视为 String . #{15}也不能在 EL 中表示为整数,所有数字总是被隐式处理为 Long当类型信息不存在时。 ClassCastException可以通过使用标记文件而不是复合组件来防止。

您最好的选择是在实际中进行检查 rows属性本身。

<p:dataTable ... rows="#{cc.attrs.filter ? cc.attrs.rows : null}">

关于JSF 复合 :attribute with f:attribute conversion error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13009280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com