• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why should an array variable be declared as a Bean

 
A Bhattacharya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic