[nobr]<jsp:useBean> tag doesn't support iteration.
The best way to iterate is to use the JSTL <c:forEach> tag
You may need to install JSTL on your server.
<c:forEach var="item" items="${myListOfItems}" varStatus = "status">
item #${status.index} : ${item}<br>
</c:forEach>
[/nobr]
Here TicketsActionForm contains a collection :
public class TicketsActionForm extends BaseActionForm {
private Collection ticketsDTO;
public void setTicketsDTO(Collection ticketsObjList) {
this.ticketsDTO = ticketsObjList;
}
public Collection getTicketsDTO() {
return ticketsDTO;
}
}
A list of tickets object get saved to the collection.
U can use that in ur jsp as
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
<logic:notEmpty name="TicketsActionForm" property="ticketsDTO">
<logic:iterate name="TicketsActionForm" property="ticketsDTO"id="ticket">
<tr>
<%-- print out the tickets informations --%>
<td><font size="1"><bean:write name="ticket" property="submitDate" /></font></td>
<td valign="top"><a href="javascript:viewTicket('<bean:write name="ticket" property="ticketId" />');"></a></td>
</td>
<td><font size="1"><bean:write name="ticket" property="parkName" /></font></td>
<td><font size="1"><bean:write name="ticket" property="noOfTicketsRequired" /></font></td>
<td><font size="1"><bean:write name="ticket" property="statusName" /></font></td>
<%-- print out the edit link for each ticket --%>
<td><html:link action="ticketsUserAction.do?action=editTicketByUser" paramName="ticket" paramProperty="ticketId" paramId="ticketId" >EDIT
</html:link>
</td>
</tr>
</logic:iterate>
</logic:notEmpty>
Hi Nitin,
Thank for the code and advice. It is really useful to iterate the collection object as I implemented it.
But, I am not sure how to render only one property , for instance a String, of a bean (ActionForm or a standard java bean) without using EL in JSTL.
Can I do it with Struts TLDs?
Regards.
Hi Nitin,
Well i have tried the same approach...but I am not able to get properties of a bean object on a jsp page.
The situation is, I am creating a new java bean instances for every row in the result set. I am storing those bean objects into an ArrayList. Then I am setting that arraylist object into my session scope in my action servlet.
The problem is, I can see the arraylist in the debugger, it has all the properties loaded init. But, I was not able to iterate and print those values on my jsp page. Here is my code for my ActionServlet, JavaNean and JSP page...
detailSearchAction.java- an ActionServlet
***************************************************************************
public class detailSearchAction extends Action {
private String TARGETPAGE = null;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
detailSearchActionForm dsa = (detailSearchActionForm) form;
ArrayList resultObjects = new PerformSearch().primarySearchUsecaseOne();
HttpSession session = request.getSession();
if(dsa.getName() != null && dsa.getName() != ""){
TARGETPAGE = "success";
session.setAttribute("INITIAL_RESULTS",resultObjects);
//request.setAttribute("SEARCH_FORM",dsa);
}else{
TARGETPAGE = "failure";
}
return mapping.findForward(TARGETPAGE);
}
}
initialSearch.java - JavaBean
***************************************************************************
public class initialSearch extends Object implements Serializable {
public static final String HOST_ID = "hostid";
public static final String PLANT_GENUS = "plantgenus";
private String hostid;
private String plantgenus;
private PropertyChangeSupport propertySupport;
public initialSearch() {
propertySupport = new PropertyChangeSupport(this);
}
public String getHostId() {
return hostid;
}
public void setHostId(String value) {
String oldValue = hostid;
hostid = value;
propertySupport.firePropertyChange(HOST_ID, oldValue, hostid);
}
public String getPlantGenus(){
return plantgenus;
}
public void setPlantGenus(String value){
String oldValue = plantgenus;
plantgenus = value;
propertySupport.firePropertyChange(PLANT_GENUS, oldValue, plantgenus);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertySupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertySupport.removePropertyChangeListener(listener);
}
}
detailResults.jsp- JSP page for Printing the results.
***************************************************************************
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="http://displaytag.sf.net" prefix="dispaly"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Success!</title>
</head>
<body>
<h1>The search results are as under.</h1>
The search result string is: ${INITIAL_RESULTS[1]}
<c:set var="initialsearch" value="${INITIAL_RESULTS}"/>
<c:set var="size" value="${fn:length(initialsearch)}"/>
<c:forEach begin="0" end="${size-1}" var="count">
<jsp:useBean class="resultBeans.initialSearch" id="initS">
<jsp:getProperty name="initS" property="hostid"/>
</jsp:useBean>
</c:forEach>
Modify your search <a href="indexLeafMiners.jsp">here</a>!
</body>
</html>