3

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

8
  • is that java code in your Backing Bean? also, why do you need that empty managed-property set up in your faces-config? Commented Dec 18, 2012 at 19:41
  • @eljunior: 1) yes, all java code is from my menaged-bean 2) this property is empty because I add data to her after connection with sql server Commented Dec 18, 2012 at 19:47
  • 1
    Are you swallowing exceptions somewhere? The ResultSet#getString() would throw SQLException: invalid index when 0 is passed. The problem is at least not visibe in the code posted so far (at least, not the technical problem which you're talking about; there are however severe design problems). I'm also not sure why this question got 2 upvotes as it's unanswerable in its current form and exposes poor practices. Commented Dec 18, 2012 at 21:49
  • @BalusC: my "while" loop is in try catch statement and I dont have any SQLException. "I'm also not sure why this question got 2 upvotes..." - me to but I if You want to show me something just do it. I am just lerning JSF. I know that my skills are poor but I think that is unnecessary to write to me about this. If I do something wrong just show me where (or give me link if You dont have time at this moment) and I will try to improve my knowledge. Commented Dec 18, 2012 at 22:18
  • What is the catch block doing? If it's doing nothing, then that explains your concrete problem: you're completely swallowing and ignoring exceptions, hereby hiding any clues as to the underlying problem. You should not swallow exceptions. You should throw them. They contain valuable information about the underlying problem. Commented Dec 18, 2012 at 23:22

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please explain to me why there are 2 nested ui:repeat loops pls?
Because OP wants to print each item in a list of list. So the first iteration is for the outer list and then you iterate over items of each list in the outer list.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.