问题
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