wood burning stoves 2.0*
The moose likes JSP and the fly likes Why should an array variable be declared as a Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Why should an array variable be declared as a Bean" Watch "Why should an array variable be declared as a Bean" New topic
Author

Why should an array variable be declared as a Bean

A Bhattacharya
Ranch Hand

Joined: Oct 22, 2007
Posts: 125
In the below exampl why should the variable bookRow be decalred as a java bean of type Object[] in order for "${bookRow[0]} to work?

<sql:query var="books">
select * from books where id = ?
<sql:param value="${bid}" />
</sql:query>

<c:forEach var="bookRow" begin="0" items="${books.rowsByIndex}">
<jsp:useBean id="bid" type="java.lang.String" />
<jsp:useBean id="bookRow" type="java.lang.Object[]" />
<jsp:useBean id="addedBook" class="database.BookDetails" scope="page" >
<jsp:setProperty name="addedBook" property="bookId" value="${bookRow[0]}" />
[ January 27, 2008: Message edited by: Bear Bibeault ]
A Bhattacharya
Ranch Hand

Joined: Oct 22, 2007
Posts: 125
Whereas in another example below, the varaible bookRow is not defined as a bean of type Map.

<sql:query var="books">
select * from books
</sql:query>

<c:forEach var="book" begin="0" items="${books.rows}">
<tr>
<c:set var="bookId" value="${book.id}" />
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
I am not really familiar with the JSTL SQL tags. Its a drawback of sticking with the good application architecture or in this case particularly, MVC. However, after looking at your code and using a bit of programming experience, it looks like in the former snippet it is expecting the record as an array. The elements would be in the same sequential order as they were queried. You have to manipulate the record using array index. In the latter, unlike former, it is expecting a bean object so you can manipulate values using appropriate getters instead.
[ January 27, 2008: Message edited by: Adeel Ansari ]
 
Don't get me started about those stupid light bulbs.
 
subject: Why should an array variable be declared as a Bean