2

Scenario:

I have a method gerRow() in a class DatabaseDAO that fetches some table rows and adds them in an object of class Row.

Each Row object is added to an ArrayList. The getRow() method returns that ArrayList.

Question:

My question is how do I iterate the text results in a JSP? I am planning to use the JSTL tags <c:out> and <c:ForEach>. I think that's the only option. How exactly can I do that?

Difficulty arisen by:

the method that I call returns an ArrayList. The ArraList has object of rows. I then have to separate each column in that row to print it out.

The Row object is of Row class, which has Row.getColum1, Row.getColum2, Row.getColum3 and so on.

Sample:

<jsp:useBean id="xxxxxxxxxxxx" class="xxxxxxxxxxxx" scope="request" />
<c:forEach var="xxxxxxxxxxxx" items="${xxxxxxxxxxxx}">
    <c:out value="${xxxxxxxxxxxx.xxxxxxxxxxxx}" />
    <c:out value="${xxxxxxxxxxxx.xxxxxxxxxxxx}" />
</c:forEach>
informatik01
  • 16,038
  • 10
  • 74
  • 104
kevin
  • 328
  • 4
  • 15
  • 33
  • 1
    This question is basically already answered in chapter 1 of a sane JSP book/tutorial. I suggest to start at our tag wiki pages to get started with some decent Hello World examples and links to sane tutorials/resources. Put your mouse on top of e.g. the `[jsp]` tag which you have put on the question until a black info box shows up and then click therein the *info* link. – BalusC Mar 18 '13 at 15:40

1 Answers1

1

I'm guessing you don't know what the x are. This is called expression language.

You don't need this <jsp:useBean id="xxxxxxxxxxxx" class="xxxxxxxxxxxx" scope="request" />

Expression language usually translates into get methods, but you don't need to put the "get" word. Things like myObj.getId(); are written like this: #{myObj.id}. And that's it, if you want to write this on an html table, do this (I assume html table because you said "I then have to separate each colum in that row to print it out").

<table>
    <c:forEach var="rowObj" items="${myClass.myArrayList}">
        <tr>
            <td>${rowObj.column1}</td>
            <td>${rowObj.column2}</td>
            <td>${rowObj.column3}</td>
        </tr>
    </c:forEach>
</table>

Note that the most important part of the answer is the link I provided, you should learn all of that. Do you already put the arraylist in the request? It's a little hard to tell which is the real question here.

See also: How to avoid Java code in JSP files?

Community
  • 1
  • 1
Roger
  • 2,912
  • 2
  • 31
  • 39
  • dont I need to import the 2 classes will use? DatabaseDAO class and Row class. and how? – kevin Mar 18 '13 at 14:03
  • I edited the answer `${myClass.myArrayList}`, which translates into, `myClass.getMyArrayList();`, so you should have this "get" method in your class (it is important that the method name starts with get). I don't remember correctly if you have to import the classes, I'd say no, jsp are converted to .java files and I believe the process is smart enough to do this for you. Try it though. – Roger Mar 18 '13 at 14:05
  • http://pastebin.ca/2335079 is not somehow working. no errors in console either. any clues for this practical example? – kevin Mar 18 '13 at 14:11
  • I will see this later, but with an overview, your query seems to be wrong, there is no subject in your result set. – Roger Mar 18 '13 at 14:27
  • changed that alrady in above mentioned like. but either way is there any other problem than my silly mistakes? – kevin Mar 18 '13 at 14:28
  • You are not making available your object to the request, please read the link i provided. You should do that like this, somwhere (in a servlet) `request.setAttribute("myObject", myObject);`, this works with objects. Please read the link I provided. – Roger Mar 18 '13 at 14:34
  • I already did, read the link. **Make objects available to EL** – Roger Mar 18 '13 at 14:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26391/discussion-between-masood-and-roger) – kevin Mar 18 '13 at 17:34