Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

JSP beans retrieval with a for loop

Thread view: 
Vittorix - 09 Mar 2008 19:49 GMT
Hi guys,

I retrieve books from a database and put each book with its data into a
bean.
this works well, the beans are called bookBeanBeg1, bookBeanBeg2,
bookBeanBeg3, bookBeanBeg4, etc.

then I retrive the first bean from an JSP page with:

<jsp:useBean id="bookBeanBeg1" type="p4Solution.BookBean" scope="session" />
<jsp:getProperty name="bookBeanBeg1" property="shortDescription"/>

and it works fine.

Now I try to make an automatic retrieval with a for loop.
The for cicle works well in I hardcode for example the id="bookBeanBeg1" and
the name="bookBeanBeg1"

Bu there is some problem in using an expression inside the two declarations,
it gives me an error. it doesn't accept the id the name attributes created
by the expression.

can you please help me fix this? thanks

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>JSP Page</title>
   </head>
   <body>
       <h2>Hello HTML beginner!</h2>
       <%
           int numBooks =
Integer.parseInt(request.getParameter("numBookBeg"));
       %>
       <%= numBooks %> books available for you.
       <br> <br>
       <UL>
           <%
           for (int i=1; i<=numBooks; i++)
           {
               String bookBeanBeg;
               bookBeanBeg = "bookBeanBeg" + i + "";
           %>
               <br> <br> NOTE: ERROR BELOW
               <jsp:useBean id="<%=bookBeanBeg%>"
type="p4Solution.BookBean" scope="session" />
               <LI><jsp:getProperty name="<%=bookBeanBeg%>"
property="shortDescription"/>
           <% } %>

       </UL>
   </body>
</html>

Signature

ciao
Vittorix

Arne Vajhøj - 09 Mar 2008 20:29 GMT
> I retrieve books from a database and put each book with its data into a
> bean.
[quoted text clipped - 11 lines]
> The for cicle works well in I hardcode for example the id="bookBeanBeg1" and
> the name="bookBeanBeg1"

Have your backend Jaca code return a collection of BookBean. It is very
easy to iterate over that.

Arne
Vittorix - 09 Mar 2008 20:39 GMT
> Have your backend Jaca code return a collection of BookBean. It is
> very easy to iterate over that.

thanks.
I tried that before, but I don't know how to iterate the list of beans

in my servlet I did:

       List list = (List) session.getAttribute("pageList");
       if (list == null)
       {
         list = new ArrayList();
         PageListBean page1 = new PageListBean("HTML Beginner",
"/beginner.jsp");
         PageListBean page2 = new PageListBean("HTML Pro", "/pro.jsp");
         list.add(page1);
         list.add(page2);
         session.setAttribute("pageList", list);
       }

then I tried to retrieve the list of beans but I couldn't have the JSP page
working iterating, can you please say how? thanks

<jsp:useBean id="pageList" type="p4Solution.PageBean" scope="session" />

.........

Signature

ciao
Vittorix

Vittorix - 09 Mar 2008 20:56 GMT
> then I tried to retrieve the list of beans but I couldn't have the
> JSP page working

trying and retrying, but still not working.

in the PageBean class I did

   private List items = new ArrayList( );
   public List getItems() {
       return items;
   }
   public void setItems(List items) {
       this.items = items;
   }

in my servlet I did:

       List list = (List) session.getAttribute("pageList");
       if (list == null)
       {
         list = new ArrayList();
         PageListBean page1 = new PageListBean("HTML Beginner",
"/beginner.jsp");
         PageListBean page2 = new PageListBean("HTML Pro", "/pro.jsp");
         list.add(page1);
         list.add(page2);
         session.setAttribute("pageList", list);
       }

then I tried to retrieve the list of beans:

 <jsp:useBean id="pageList" type="p4Solution.PageBean" scope="session" />

 <c:set var="itemsList" value="${ pageList.items }" />
 <UL>
       <c:forEach var="item" items="${pageList.items}">
          <LI>${item}</LI>
       </c:forEach>

but I couldn't have the JSP page working

Signature

ciao
Vittorix

Arne Vajhøj - 09 Mar 2008 20:56 GMT
>> Have your backend Jaca code return a collection of BookBean. It is
>> very easy to iterate over that.
>
> I tried that before, but I don't know how to iterate the list of beans

[example from a contact page - you should easily be able
to translate to books]

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

...

<table border>
<tr>
<th>Navn</th>
<th>Telefon</th>
<th>Email</th>
</tr>
<c:forEach var="c" items="${allc}">
<tr>
<td><c:out value="${c.name}"/></td>
<td><c:out value="${c.phone}"/></td>
<td><c:out value="${c.email}"/></td>
<tr>
</c:forEach>
</table>

The above just assume that request has an attribute under the
name "allc" that is a collection of a bean class with name,
phone and email properties.

Arne

PS: It is not even utilizing JSP 2.0 !
Vittorix - 09 Mar 2008 23:35 GMT
> [example from a contact page - you should easily be able
> to translate to books]

I'm trying this, but it doesn't work :(

########### INSIDE THE SERVLET:

           List list = (List) request.getAttribute("allc");
           if (list == null)
           {
             list = new ArrayList();
             Bean b1 = new Bean("Vittorio", "123", "email1");
             Bean b2 = new Bean("Arne", "345", "email2");
             Bean b3 = new Bean("Barne", "567", "email3");
             Bean b4 = new Bean("Carne", "789", "email4");
             list.add(b1);
             list.add(b2);
             list.add(b3);
             list.add(b4);
             request.setAttribute("allc", list);
           }
           RequestDispatcher dispatcher =
request.getRequestDispatcher("/p4Solution/echart.jsp");
           dispatcher.forward(request, response);

######## JSP PAGE

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%--
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
--%>
<!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>JSP Page</title>
   </head>
   <body>
       <h2>Your Shopping Cart</h2>
<table border>
<tr>
<th>Navn</th>
<th>Telefon</th>
<th>Email</th>
</tr>
<c:forEach var="c" items="${allc}">
<tr>
<td><c:out value="${c.name}"/></td>
<td><c:out value="${c.phone}"/></td>
<td><c:out value="${c.email}"/></td>
<tr>
</c:forEach>
</table>
   </body>
</html>

Signature

ciao
Vittorix

Arne Vajhøj - 10 Mar 2008 00:48 GMT
>> [example from a contact page - you should easily be able
>> to translate to books]
>
> I'm trying this, but it doesn't work :(

Care to reveal what the error is ?

Arne
Vittorix - 10 Mar 2008 01:23 GMT
>>> [example from a contact page - you should easily be able
>>> to translate to books]
>>
>> I'm trying this, but it doesn't work :(
>
> Care to reveal what the error is ?

no error the page compiles fine, it just doesn't show anything, only the
table headers :(

Signature

ciao
Vittorix

Arne Vajhøj - 10 Mar 2008 02:32 GMT
>>>> [example from a contact page - you should easily be able
>>>> to translate to books]
[quoted text clipped - 3 lines]
> no error the page compiles fine, it just doesn't show anything, only the
> table headers :(

Sounds as if the content of the list is somehow lost.

Try put in some debug print in the JSP page to check
if the list actually contains something there.

Arne
Vittorix - 10 Mar 2008 17:23 GMT
>>>>> [example from a contact page - you should easily be able
>>>>> to translate to books]
[quoted text clipped - 8 lines]
> Try put in some debug print in the JSP page to check
> if the list actually contains something there.

because I couldn't find a solution, I'm using a scriplet inside the JSP that
does the job:

   int numBooks = Integer.parseInt(request.getParameter("numBook"));
   for (int i=1; i<=numBooks; i++)
   {
       String bookBeanS = "bookBeanBeg" + i + "";
       p4Solution.BookBean bookBean = null;
       bookBean = (p4Solution.BookBean) session.getAttribute(bookBeanS);
       out.write(bookBean.getSeq_no());

thanks anyway.

Signature

ciao
Vittorix

Vittorix - 09 Mar 2008 20:33 GMT
> Now I try to make an automatic retrieval with a for loop.

even simpliified, without for loop:

           <%
               String bookBeanBeg;
               bookBeanBeg = "bookBeanBeg1";
           %>
               <jsp:useBean id="<%=bookBeanBeg%>"
type="p4Solution.BookBean" scope="session" />
               <jsp:getProperty name="<%=bookBeanBeg%>"
property="shortDescription"/>

it doesn't work.

please help. I'm going crazy with it!

Signature

ciao
Vittorix



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.