我有一个ArrayList的ArrayList - 我用这种方式声明它:
ArrayList<ArrayList<String>> queryResult=new ArrayList<ArrayList<String>>();
然后我像这样向数组中添加一个新元素:
for(int i=1;i<colNumber;i++)
{
queryResult.add(new ArrayList<String>(20));
}
之后我为数组元素添加一个值:
while(r.next())
{
for(int i=0;i<colNumber;i++)
{
queryResult.get(i).add(r.getString(i));
}
}
但是当我尝试在DataTable标签中使用它时,我什么也看不见:(
<h:dataTable value="#{polaczenieSQL.queryResult}" var="w">
<h:column>
<f:facet name="head">typ</f:facet>
#{w[0]}
</h:column>
我做错了什么?我应该如何在JSF中使用这个数组?
这是我的faces.config:
<managed-property>
<property-name>queryResult</property-name>
<property-class>java.util.ArrayList</property-class>
<list-entries></list-entries>
</managed-property>
我发现第一个问题:
r.getString(i)
我加了一个
System.out.print("something")
循环后,但它不想打印。
当我更改变量'i'并输入例如:4时,我在控制台上看到“something”。变量'colNumber'设置为5(但我的sql表有7列,我使用“select * from mytable”,所以我不认为这是一个反问题)。
如果要打印内部列表中的所有值,则应执行以下操作:
<h:dataTable value="#{polaczenieSQL.queryResult}" var="w">
<h:column>
<f:facet name="head">typ</f:facet>
#{w[0]} <!--will print the first element in the inner list-->
</h:column>
<h:column>
<f:facet name="head">typ</f:facet>
#{w[2]} <!--will print the second element in the inner list-->
</h:column>
...
<h:column>
<f:facet name="head">typ</f:facet>
#{w[n]} <!--will print the nth element in the inner list-->
</h:column>
</h:dataTable>
所以基本上如果你想打印内部列表的所有值,你可以使用以下样式:
<ui:repeat value="#{activeUser.listInList}" var="innerList">
<ui:repeat value="#{innerList}" var="innerListValue">
#{innerListValue}
</ui:repeat>
</ui:repeat>
关于吞咽异常,除非你确切知道自己缺少什么,否则你应该在遇到异常时抛出异常。