开发者

Comparing Struts 2 Nested Iterators

开发者 https://www.devze.com 2022-12-22 19:38 出处:网络
Suppose that I have two objects with the same property name that I am iterating over with Struts 2. class Book {

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:iter开发者_Python百科ator>

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


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}">
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号