5 Replies Latest reply on Aug 10, 2012 3:56 AM by 25631

    JSTL - Array of objects?


      I am using JSTL in a JSP to access data from my MySQL database.
      Thus I am looping through doing a foreach:
      <c:forEach var="row" items="${rs.rows}">
      <c:set var="menuId" value="${row.id}"/>
      <c:set var="nextPage" value="${row.homePage}"/>
      <c:set var="promptText" value="${row.prompt}"/>
      <c:set var="menuType" value="${row.menuType}"/>
      What I would like to do is capture this in an Array of objects.
      So that I can iterate through the list numerous times without
      having to requery the database each time.

      Thus is there a way to create an ArrayList of objects using JSTL?
      Or do I need to create the ArrayList outside of JSTL and then pass it
      to the JSTL?

      Thanks in advance!
        • 1. Re: JSTL - Array of objects?
          Like any decent JSP book or tutorial will tell you, you should be doing that kind of stuff in a servlet. You'd fetch the data, stick it in an array or list (note that JPA makes that process so much easier) and pass it to request.setAttribute() (which puts the data in the view scope). When all business logic has been performed you forward control to a JSP which will render the view based on the data prepared in the servlet.
          • 2. Re: JSTL - Array of objects?
            Cross posted

            • 3. Re: JSTL - Array of objects?
              Actually I didn't get an answer to my question. I understand that I am doing it "wrong" there is a reason for that.
              Is it not possible to do what I am proposing is the answer I am looking for...
              • 4. Re: JSTL - Array of objects?
                You seem to think that iterating through that "rs" thing somehow uses it up, so that you can't iterate through it again. That isn't the case. You can iterate through it as often as you like, as long as it's still accessible.

                If your question involved using it in several different requests, then the way to deal with that is to make your "rs" be a session attribute. However you didn't say that. In fact you didn't say much of anything, you just demanded that your unstated reason for doing it wrong should be treated as if it were a good reason. I'm not inclined to look for ugly workarounds when fixing the original error would be a better strategy, so I'm not going to play along with that. If you had a good reason for bad programming then let's hear it. For all we know (remember we know very little) you may just be misunderstanding things.
                • 5. Re: JSTL - Array of objects?
                  Sorry I didn't mean to demand anything if that is how you took it.
                  I was simply trying to build a rapid prototype for demonstration
                  purposes. It doesn't need to be perfect just quick. I figured it out
                  myself. And it works and works fine. It is not meant to be production
                  code by any means. So maybe it is "wrong" and those who answered
                  were not willing to help and rather bash me for wanting to do something
                  "wrong". Thanks for all of your help!