Struts comes with <logic:iterate> tag to iterate over collections.

Here’s two <logic:iterate> examples

  1. Iterate over a list (primitive type)
  2. Iterate over a list (object)
Download this example – Struts-logic-Iterate-example.zip

1. Iterate over a list array (primitive type)

In this example, you initial a normal list with some Strings and store it into HttpServletRequest as name “listMsg“. In JSP page, you can use the <logic:iterate> to iterate the stored list and display the values.

...
public class PrintMsgAction extends Action{
 
	public ActionForward execute(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) 
        throws Exception {
 
		List<String> listMsg = new ArrayList<String>();
 
		listMsg.add("Message A");
		listMsg.add("Message B");
		listMsg.add("Message C");
		listMsg.add("Message D");
 
		request.setAttribute("listMsg", listMsg);
 
		return mapping.findForward("success");
	}
 
}

Inside the logic tag, you can use the “name” attribute (listMsg) to get the list value.

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<html>
<head>
</head>
<body>
<h1>Struts &lt;logic:iterate&gt; example</h1>
 
<logic:iterate name="listMsg" id="listMsgId">
<p>
	List Messages <bean:write name="listMsgId"/>
</p>
</logic:iterate>
 
</body>
</html>

2. Iterate over a list array (object)

In this example, you initial a normal list with users object and store it into HttpServletRequest as name “listUsers“. In JSP page, you can use the <logic:iterate> to iterate the stored list and display the values.

public class User{
 
	String username;
	String url;
 
    //getter and setter methods
}
... 
public class PrintMsgAction extends Action{
 
	public ActionForward execute(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) 
        throws Exception {
 
		List<User> listUsers = new ArrayList<User>();
 
		listUsers.add(new User("user1", "http://www.user1.com"));
		listUsers.add(new User("user2", "http://www.user2.com"));
		listUsers.add(new User("user3", "http://www.user3.com"));
		listUsers.add(new User("user4", "http://www.user4.com"));
 
		request.setAttribute("listUsers", listUsers);
 
		return mapping.findForward("success");
	}
 
}

Inside the logic tag, you can use the “name” attribute (listUsers) to get the list value; while “property” attribute to display the object property value.

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<html>
<head>
</head>
<body>
<h1>Struts &lt;logic:iterate&gt; example</h1>
 
<logic:iterate name="listUsers" id="listUserId">
<p>
	List Users <bean:write name="listUserId" property="username"/> , 
	<bean:write name="listUserId" property="url"/>
</p>
</logic:iterate>
 
</body>
</html>
struts-logic-iterate-example
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at Struts 1.x Tutorials