Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsf-arrayList of arrayList in JSF

Tags:

java

jsf

jdbc

I have a ArrayList of ArrayList - I declare it in this way:

ArrayList<ArrayList<String>> queryResult=new ArrayList<ArrayList<String>>();

Then I add a new element to array like this:

for(int i=1;i<colNumber;i++)
{
    queryResult.add(new ArrayList<String>(20));
}

after that I add a value to elements of array:

while(r.next())
{   
    for(int i=0;i<colNumber;i++)
    {
        queryResult.get(i).add(r.getString(i));  
    }     
}

But when I try to use it in DataTable tag I see nothing :(

<h:dataTable value="#{polaczenieSQL.queryResult}" var="w">
          <h:column>
             <f:facet name="head">typ</f:facet>
             #{w[0]}
          </h:column>

What I am doing wrong? How should I use this array in JSF?

Ps this is my faces.config:

     <managed-property>
        <property-name>queryResult</property-name>
        <property-class>java.util.ArrayList</property-class>
        <list-entries></list-entries>
     </managed-property>

I found first problem:

r.getString(i)

I added a

System.out.print("something")

after a loop but it doesn't want to print.

When I change a variable 'i' and type for example: 4 I see "something" on my console . Variable 'colNumber' is set to 5 (but my sql table have 7 columns and I use "select * from mytable" so I dont think that is a counter problem ).

like image 412
Krzysztof Avatar asked Jun 10 '26 04:06

Krzysztof


1 Answers

If you want to print all the values in the inner list you should do as following:

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

so basically if you want to print all the values of a inner list you can use the following style:

<ui:repeat value="#{activeUser.listInList}" var="innerList">
    <ui:repeat value="#{innerList}" var="innerListValue">
        #{innerListValue}
    </ui:repeat>
</ui:repeat>

And about exception swallowing, you should throw an exception whenever you catch one unless you know exactly what you are missing.

like image 57
cubbuk Avatar answered Jun 14 '26 15:06

cubbuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!