Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Problem with JSF creating a loop

  Asked By: Steven    Date: Sep 21    Category: Java    Views: 5432

How can we create a for loop using JSF tags and having the variables in to our control. for example:

<h:dataTable var="b"value="#{PollJSFManagedBean.retrieveCatQues}">
<h:outputText value="#{b.title}" />

// below there is a list called tbQuestioncategory within the //variable b that has an index called "i" when i have this element constant lets //say "1" then i get the value of tbQuestioncategory[1] which works good, but i //would like to have a for loop which can alter between the list indeces in the way //i want e.g: for (int i=0;i<SOMEINTEGER;i++)

<h:outputText value="#{b.tbQuestioncategory[i]}" />

can i ask for some help?



4 Answers Found

Answer #1    Answered By: Stacie Martin     Answered On: Sep 21

If you are using myfaces implementation use tomahawk <t:dataTable />
tag therefore its rowIndexVar attribute works  as i in your given
example refer to the following link for tag reference:


In case you are using standard JSF or your utilized implementation of
JSF doesn't have any facility like above ,still you can use the
following trick, declare loop  index in your bean class and define
getter , setter and increment methods for it. Explore the details of
this solution and the way you can use these methods.

Another solution is available if you are using JSF 1.2 that is to use
JSTL mixed with your JSF tags. I hope it helps.

Answer #2    Answered By: Adali Fischer     Answered On: Sep 21

you should use Tomahawk in JSF to solve your problem  such as :

<t:dataList value="#{bean.collection}" var="item">
<h:outputText value="#{item.name}"/>

Answer #3    Answered By: Olga Kates     Answered On: Sep 21

the easiet way to solve your problems is to use "JSTL" loop  tags in JSF pages.
this will solve your problem.

a better more sophisticated approach is using JSF iterator components which can be found in some JSF components sets, like ADF Rich client which is going to be used in your company.

Answer #4    Answered By: Milind Mishra     Answered On: Sep 21

I don't know why you've chosen JSF for web development, but one of the
most issue that JSF persists on, is to simplify the views and to move
the complexity to controllers and models.
I don't believe JSF needs such a iterator you are searching for,
that's why you can't find it.

I do know exactly what are going to do, but i guess you are displaying
a string collection in a column of datatable.

There are three solutions
1. Converts the array to a string in the model
2. Use the following code

<h:dataTable var="b"value="#{PollJSFManagedBean.retrieveCatQues}">

<h:outputText value="#{b.title}" />
<h:dataTable var="cat" value="#{b.tbQuestioncategory}">

<h:outputText value="#{cat}"

3. Or make your own JSF compatible tag library to display a list

Didn't find what you were looking for? Find more on Problem with JSF creating a loop Or get search suggestion and latest updates.