0

I have 2 java classes. One is a java bean and the other is a collection of instances from this java bean class.

public class Quiz{ 
  private int id;
  private String title;
  //getters and setters 
}

public class Quizzes{
  List<Quiz> quizList = new ArrayList<Quiz>;
  public Quizzes{
    //constructor to add Quiz objects to quizList
    Quiz curQuiz = new Quiz();
    ...
  }
  public List<Quiz> getQuizList(){
    return this.quizList;
  }

}

In my JSP I want to access for example the id (or title) of a Quiz. How can I do this? The only Idea I have is to iterate over the Objects in quizlist and call getId Method, and then add this Integer Values to a List. For example like this:

public List<Integer> getId(){
    List<Integer> idList = new ArrayList<Integer>();
    ListIterator<Quiz> ir = quizlist.listIterator();
    int id;
    while (ir.hasNext()){
        Quiz curQuiz = ir.next();
        id = curQuiz.getId();
        idList.add(id);
    }
    return idList;
}

But isn't there a better solution?

Brenne
  • 265
  • 1
  • 3
  • 11

2 Answers2

3

You can look into <jsp:useBean> tag. The syntax for the same is

<jsp:useBean id= "attributeName" scope= "page | request | session | application" class= "yourPackage.yourClass"> </jsp:useBean>.

here attributeName is the name of attribute you have set in page, request, session or application scope. yourPackage.yourClass is your beanClass i.e. Quiz if you want to access Quiz member variables.

You can find good example here

If you want to iterate over a list which is accessible in jsp, then you can use forEach tag.

<c:foreach jsp iterate over list

<c:forEach var="window" items="${requestScope.quizList}">
    <c:out value="${quiz.id}"/> 
    <c:out value="${quiz.name}"/>
</c:forEach>
Community
  • 1
  • 1
Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • I know this tag but don't know how to use it exactly in this case. `` then I could access to `${quiz.id}` but the Problem is I only have this quizList (a List with Quiz Objects). If I use `` I will only have access to a Quiz object but I want to have access to a property for example the id or title of one quiz. – Brenne Jul 04 '13 at 08:35
  • I have edited the answer as per your clarification. You can use `forEach` tag to iterate over the list – Prasad Kharkar Jul 04 '13 at 08:45
  • Thanks, I think the problem is, that I actually have no instances of the class Quiz. These objects are only saved in quizList. if I try `` `` `Test
    ` `
    ` I get no errors! And my JSP contains the word "Test" twice, which is okay because in my quizList there are two Quiz objects. In the head of my JSP I have both: ` ` Do you understand the problem?
    – Brenne Jul 04 '13 at 09:18
  • I found the solution: in the head of the JSP I use both ` ` when iterrating `` `${quizzes.quizlist[status.index].id}` `` – Brenne Jul 04 '13 at 11:17
0

if you only need the Id per quiz, you can overwrite toString() function in the quiz:

public class Quiz{ 
  private int id;
  private String title;
  //getters and setters 
}

public class Quizzes{
  List<Quiz> quizList = new ArrayList<Quiz>;
  public Quizzes{
    //constructor to add Quiz objects to quizList
    Quiz curQuiz = new Quiz();
    ...
  }
  public List<Quiz> getQuizList(){
    return this.quizList;
  }

   @Override 
   public String toString(){
return getId();
  }

}

This way when you call toString() it will return appropriate Id:

Quiz.toString(); 

You can change your iterations to:

public List<Integer> getId(){
    List<Integer> idList = new ArrayList<Integer>();
    ListIterator<Quiz> ir = quizlist.listIterator();
    while (ir.hasNext()){
        idList.add(ir.next().toString());
    }
    return idList;
}
mel3kings
  • 8,857
  • 3
  • 60
  • 68