[英]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.