Hi, What do i need to display an arralist of items in a JSP? I know about the JSTL tags. The doubt i have is whether i should add a get arraylist in the actionform?
From my EJB i receive an arraylist. Then action calls it, and JSP displays it.
Ot i just call the variable arraylist in the jsp and iterate using tags?
Thanks in advance
//Struts action.
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
BookingLineActionForm bookinglineform = (BookingLineActionForm) form; // get the form bean
Bookingline bookingline = new Bookingline();
//ArrayList mylist = new ArrayList();
String placefrom = bookinglineform.getBkLPlaceFrom();
String placeto = bookinglineform.getBkLPlaceTo();
bookingline = lookupBookinglineFacade().tripSearch(placefrom,placeto);
//BeanUtils.copyProperties(bookinglineform,bookingline);
//EJB
public ArrayList findTrip(String placefrom, String placeto) {
ArrayList q = null;
try {
q = (ArrayList) em.createQuery("SELECT c FROM Bookingline c WHERE" +
"c.bkLPlaceFrom = :sessionName AND c.bkLPlaceTo " +
"= :sessionName2").setParameter("sessionName",placefrom)
.setParameter("sessionName2",placeto)
.getResultList();
} catch (NoResultException en) {
// en.printStackTrace();
return q = null;
}
return q;
}
public ArrayList tripSearch(String placefrom, String placeto) {
ArrayList q = findTrip(placefrom,placeto);
return q;
}
Found answer, you do not need to use actionform.
You use request.setattribute in actionform and then use JSTL or Struts tags to iterate over list in JSP.
The struts framework automaticallty takes care of this for you
which is the beauty of being able to develop sites in java 
Hi,
What do u mean,
I am getting an error and cannot figure out if the problem is in the struts or EJB.
What is happening is that an empty rectangle is displayed in the jsp. And i don’t know if the error is in jsp, struts action or ejb.
-----------JSP-----------------
<table border="1">
<c:forEach var="bookinglist" items="${requestScope.bookingList}" >
<tr>
<td><c:out value="${bookinglist.bkLNo}"/></td>
<td><c:out value="${bookinglist.BkLTransType}"/></td>
<td><c:out value="${bookinglist.BkLCost}"/></td>
<td><c:out value="${bookinglist.BkLArrival}"/></td>
<td><c:out value="${bookinglist.BkLDepart}"/></td>
<td><c:out value="${bookinglist.BkLPlaceTo}"/></td>
<td><c:out value="${bookinglist.BkLDepartDay}"/></td>
<td><c:out value="${bookinglist.BkLPlaceFrom}"/></td>
<td><c:out value="${bookinglist.BkLTCNo}"/></td>
</tr>
</c:forEach>
</table>
Struts Action
BookingLineActionForm bookinglineform = (BookingLineActionForm) form; // get the form bean
Bookingline bookingline = new Bookingline();
//ArrayList mylist = new ArrayList();
String placefrom = bookinglineform.getBkLPlaceFrom();
String placeto = bookinglineform.getBkLPlaceTo();
bookingline = lookupBookinglineFacade().tripSearch(placefrom,placeto);
//BeanUtils.copyProperties(bookinglineform,bookingline);
request.setAttribute("bookingList", bookingline);
Not sure if this may help, but when i click on view source in the jsp, after getting the result, this is displayed.
<table border=“1”>
<c:forEach var=“bookinglist” items=“” >
<tr>
<td><c:out value=“”/></td>
<td><c:out value=“”/></td>
<td><c:out value=“”/></td>
<td><c:out value=“”/></td>
<td><c:out value=“”/></td>
<td><c:out value=“”/></td>
<td><c:out value=“”/></td>
<td><c:out value=“”/></td>
<td><c:out value=“”/></td>
</tr>
</c:forEach>
</table>
are you missing the decleration tag at the top saying that tags beginning with <c: belong to the core JSTL library?
Actually i had it missing. However i added it, and it was still displaying null.
Someone checked my EJB and it seemed ok, so i guess it is a problem in my action.
A question:
What i did was
- Call method which returns arraylist of objects
- Set arraylist object in session using reqiest.setAttribute()
This means i do not use any actionform.
Now, without modifying any code, just adding jstl taglib, i get
<table border="1">
</table>
When i click view source in JSP. The above appears.
This is my action.
BookingLineActionForm bookinglineform = (BookingLineActionForm) form; // get the form bean
Bookingline bookingline = new Bookingline();
//ArrayList mylist = new ArrayList();
String placefrom = bookinglineform.getBkLPlaceFrom();
String placeto = bookinglineform.getBkLPlaceTo();
bookingline = lookupBookinglineFacade().tripSearch(placefrom,placeto);
//BeanUtils.copyProperties(bookinglineform,bookingline);
request.setAttribute("bookingList", bookingline);
//request.setAttribute("bookingList",lookupBookinglineFacade().tripSearch(placefrom,placeto));