Connecting Tech Pros Worldwide Forums | Help | Site Map

Java using loop let usebean dynamically declare id

Newbie
 
Join Date: Apr 2008
Posts: 1
#1: Apr 14 '08
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:
Expand|Select|Wrap|Line Numbers
  1. <jsp:useBean id="bookBeanBeg1" type="p4Solution.BookBean" scope="session" />
  2. <jsp:getProperty name="bookBeanBeg1" property="shortDescription"/>
  3.  
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
Expand|Select|Wrap|Line Numbers
  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  3. "http://www.w3.org/TR/html4/loose.dtd">
  4.  
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>JSP Page</title>
  9. </head>
  10. <body>
  11. <h2>Hello HTML beginner!</h2>
  12. <%
  13. int numBooks = 
  14. Integer.parseInt(request.getParameter("numBookBeg" ));
  15. %>
  16. <%= numBooks %> books available for you.
  17. <br> <br>
  18. <UL>
  19. <%
  20. for (int i=1; i<=numBooks; i++)
  21. {
  22. String bookBeanBeg;
  23. bookBeanBeg = "bookBeanBeg" + i + "";
  24. %>
  25. <br> <br> NOTE: ERROR BELOW
  26. <jsp:useBean id="<%=bookBeanBeg%>" 
  27. type="p4Solution.BookBean" scope="session" />
  28. <LI><jsp:getProperty name="<%=bookBeanBeg%>" 
  29. property="shortDescription"/>
  30. <% } %>
  31.  
  32. </UL>
  33. </body>
  34. </html>

Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,268
#2: Dec 17 '08

re: Java using loop let usebean dynamically declare id


Heiya!

I have not yet used id in this fashion 'id="<%=bookBeanBeg%>' could this be your problem?
Reply