Struts <logic:iterate> example
In Struts, you can use <logic:iterate> tag to iterate over collections. Here’re two examples :
- Iterate over a list (primitive type)
- Iterate over a list (object)
1. Iterate over a list array (primitive type)
Create a normal list with some dummy Strings and store it into HttpServletRequest, name “listMsg“.
... 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 <logic:iterate> 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)
Create a normal list with few “user” objects and store it into HttpServletRequest as name “listUsers“.
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 <logic:iterate> 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>







employeelist.jsp
****************
<a href="Edit.do?username=”>Edit
<a href="Delete.do?username=”>Delete
Add New Employee
*************
below is the code from action class
ArrayList employees = null;
employees = EmployeeData.getEmployees(getDataSource(request));
request.setAttribute(“employees”, employees);
********
Problem is here that not a single output displaying over webpage under written inside iterate tag. only table header and add employee link is displaying over webpage.
Superb
Hi ,
In the given example while Iterating a list, we are not doing any type casting to User.How will this JSP know about user class.
What happend when we write our own general iterate logic to iterate list do we need to type cast .
please clarify my question.
List Users ,
Thanks,
Sha
Hi Sha, He did not provide a complete listing. There are lots of configurations missing from this example, but basically you have a struts-config.xml where you can do something like
<form-beans> <form-bean name=”listUsers” type=”Users” >
Very Helpful example
Thanks for this :)
try running the example but encountered an error – org.apache.jasper.JasperException: Cannot find bean listMsg in any scope
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
already solved it.
Mind to share me your solution?
same error showing here also. please do mind to share the solution.
[...] <logic:iterate> example Struts tag to iterate over collections. [...]
form-beans form-bean name=”listUsers” type=”Users”