JSP beans retrieval with a for loop

V

Vittorix

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>
 
A

Arne Vajhøj

Vittorix said:
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"

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

Arne
 
V

Vittorix

Vittorix said:
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!
 
V

Vittorix

Arne said:
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" />

..........
 
V

Vittorix

Vittorix wrote:

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
 
A

Arne Vajhøj

Vittorix said:
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:eek:ut value="${c.name}"/></td>
<td><c:eek:ut value="${c.phone}"/></td>
<td><c:eek:ut 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 !
 
V

Vittorix

Arne said:
[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:eek:ut value="${c.name}"/></td>
<td><c:eek:ut value="${c.phone}"/></td>
<td><c:eek:ut value="${c.email}"/></td>
<tr>
</c:forEach>
</table>
</body>
</html>
 
A

Arne Vajhøj

Vittorix said:
Arne said:
[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 :(

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
 
V

Vittorix

Arne said:
[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 :(

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top