簡體   English   中英

JSF composite:具有f:屬性轉換錯誤的屬性

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM