Java using loop let usebean dynamically declare id

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xukaifeng
    New Member
    • Apr 2008
    • 1

    Java using loop let usebean dynamically declare id

    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:
    Code:
    <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="bookBeanBeg 1" and
    the name="bookBeanB eg1"

    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
    Code:
    <%@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>
    Last edited by Nepomuk; Dec 17 '08, 06:38 AM. Reason: Please use [CODE] tags!
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Heiya!

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

    Comment

    Working...