c:foreach inside h:datatable to show two dimensional array

偶尔善良 提交于 2019-12-25 09:36:39

问题


I am trying to use h:datatable tag to display values of two-dimensional int array. But cannot make it. Could help me with a solution?

So my backing bean is:

public class MC {
...........

public int[][] getAr() {
    return ar;
}

public int getColCount(){
    return ar[0].length;
}
}

I can display the array with code in foreach tag referring to the array size:

<h:dataTable value="#{mC.ar}" var="dt">
<c:forEach var="fe" begin="0" end="#{mC.colCount-1}">
    <h:column>
        <f:facet name="header">X</f:facet>
            #{dt[fe]}
    </h:column>
</c:forEach> 
</h:dataTable>

But nothing is printed if I try to use variable from dataTable:

 <h:dataTable value="#{mC.ar}" var="dt">
 <c:forEach var="fe" items="#{dt}">
    <h:column>
        <f:facet name="header">XX</f:facet>
            #{fe}
    </h:column>
</c:forEach> 
</h:dataTable>

Could you help me to make it work? Or perhaps you could suggest some better solution to display an array? Thank you


回答1:


There is quite common mistake done in assumption that c:forEach is processed in the same time the iteration of dataTable is processed. c:forEach is processed once in build JSF tree, in this phase there is NO "dt" variable defined. h:dataTable defines the "dt" variable in JSF restore/render phases. Use the ui:repeat instead of c:forEach if you need to iterate in this phases.



来源:https://stackoverflow.com/questions/20688858/cforeach-inside-hdatatable-to-show-two-dimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!