How to iterate over a list of arrays from beans in JSP using struts2 tags

My JSP gets an ArrayList from beans from a Struts2 action.

I want to iterate over them and print each bean and its properties on each line.

How to do this using Struts2 tags?

+4
source share
5 answers

Use the <s:iterator> .

 <s:iterator value="beans"> <p>Property foo: <s:property name="foo" /></p> <p>Property bar: <s:property name="bar" /></p> </s:iterator> 

An overview of all tags can be found in their own documentation: link to tags . Bookmark

+6
source

Using JSTL:

 <c:forEach items="${list}" var="item"> <c:out value="${item.property}" /> </c:forEach> 

You will need to add JSTL to the classpath because it does not ship with Struts, but it works. Of course, using your own struts tag (as shown by BalusC) is a better option.

+2
source

To do this with struts2, you need an iterator:

 <s:iterator value="collection"> Describe object </s:iterator> 

But I suggest using displaytag: http://www.displaytag.org/1.2/ Only 1 line describes the entire bean, and you can also sort and export. Here is a usage example:

 <display:table name="collection" /> 

and it generates a table, thead and tbody.

+1
source

Here 's a working example (Netbeans project 6.9) that illustrates how to iterate over an array or list of objects.

In addition, how to submit a form so that the list of objects is recreated when submitted.

Just allow the links and get started.

0
source

I did something similar in my base application.

Here searchForm bean and ArrayList are the results

 <logic:present name="searchForm" property="results"> <bean:size id="size" name="searchForm" property="results"/> <logic:greaterThan name="size" value="0"> <logic:iterate id="res" name="searchForm" property="results"> <p> <bean:write name="res" property="firstname" /> <bean:write name="res" property="lastname" /> </p> </logic:iterate> </logic:greaterThan> </logic:present> 
0
source

Source: https://habr.com/ru/post/1301945/


All Articles