<jsp:useBean> iterations

Hi all,Could any body tell me how to iterate the ArrayList object which contains list of same JavaBean objects through the <jsp:useBean> tag?I am getting the ArrayList object from a servlet successfully.Thanks.
[244 byte] By [jini4javaa] at [2007-11-27 8:32:17]
# 1

[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]

evnafetsa at 2007-7-12 20:28:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

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>

nitin_talwara at 2007-7-12 20:28:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

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.

jini4javaa at 2007-7-12 20:28:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Hi Evnafets,Yes, that is true. And the code works fine too. But, can I access the Struts ActionForm object using the standard JSTL and not the Struts TLDs?Regards.
jini4javaa at 2007-7-12 20:28:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Within the <html:form> tags I believe the form is available as a scoped attribute under the name of the form.So if you have a form called "myForm" on the page (name defined in struts-config) then you should be able to access ${myForm} as an EL variable.
evnafetsa at 2007-7-12 20:28:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Hey...It is perfect. I got it right...Thanks a lot mate,Cheers!!!
jini4javaa at 2007-7-12 20:28:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

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>

jini4javaa at 2007-7-12 20:28:05 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...