tags:

views:

359

answers:

1

Suppose that I have two objects with the same property name that I am iterating over with Struts 2.

class Book {

// assume that there is a public getter and setter
public String title;

public List<Chapter> chapterList;

}

class Chapter {

public String title;

}

In my JSP page, I want to iterate over the Book and Chapter. While iterating, how would I display a special message when the Book's title is the same as the Chapter's title?

<s:iterator value="bookList">
 <s:iterator value="chapterList">
  <s:if test="book.title.equals(chapter.title)">
   Same title
  </s:if>
 </s:iterator>
</s:iterator>

How would I fix the s:if tag in the above snippet to compare the book title with the chapter title?

Thanks!

Note: This is very similar to the following stackoverflow question (but in that question they only print the property name without doing a comparison and the property name is differently on the parent and child objects):

Struts 2 nesting iterators

+1  A: 

You can use the standard EL operators == or eq to test the (String) values for equality:

<s:if test="%{book.title == chapter.title}">

or (a bit more XHTML friendly)

<s:if test="%{book.title eq chapter.title}">
BalusC
Would that really work? I have an entire list of books and a list of chapters. I imagine that I would need something like bookList.title == bookList.chapterList.title (which would not work since it doesn't know which element of the list to examine).
David
Erm, in your (pseudo)code example you're already iterating over the both lists. Or is your problem also that you don't know how to iterate over them?
BalusC