By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,786 Members | 2,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,786 IT Pros & Developers. It's quick & easy.

Java using loop let usebean dynamically declare id

P: 1
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>
Apr 14 '08 #1
Share this Question
Share on Google+
1 Reply


Dököll
Expert 100+
P: 2,364
Heiya!

I have not yet used id in this fashion 'id="<%=bookBeanBeg%>' could this be your problem?
Dec 17 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.