File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Dynamically adding to a TABLE in a jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Dynamically adding to a TABLE in a jsp" Watch "Dynamically adding to a TABLE in a jsp" New topic

Dynamically adding to a TABLE in a jsp

Tim Manchester
Ranch Hand

Joined: Nov 03, 2004
Posts: 62
OK, this question has dynamic HTML, jsp and Struts elements in it so I hope I'm posting to the correct forum (only your moderator knows for sure) so here goes...

I have a very basic Struts app that I've built in WebSphere (probably a moot point). In addition I have a simple, 1 table, 3 column MySQL database with the following structure:
Table Name: Trek_Reviews
Movie Name: varchar
Reviewer: varchar
Review: varchar

Back in the app, I have a jsp called "star_trek_movie_reviews.jsp" that has a form called "star_trek_movie_reviews_ActionForm".

Hopefully, you're still with me...

So my question is what would the data member in star_trek_movie_reviews_ActionForm look like that contained all the rows of the Trek_Reviews table and how would I code the jsp to populate a <TABLE> with all of the elements in the database?

I will happily add any other details necessary to answer this question (except what the reviews were as some folks have religion about this, though generally odd numbered trek movies suck).


- DM_Tim
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

This is probably most appropriate to the Struts forum, so moved there.

[Asking smart questions] [About Bear] [Books by Bear]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
This is a pretty common scenario, and can be handled by struts quite easily. Here's the procedure:
  • Code a javaBean (Review, for example) containing the movieName, reviewer, and review properties.
  • In your Struts ActionForm, code a reviews property of type List
  • In your Struts Action, call Data Access Objects that will retrive your table from the database, instantiate the Review bean for each row, and add each bean to a List.
  • Instantiate your actionForm and put the list you created in the reviews property.
  • Put the ActionForm in the request using request.setAttribute()
  • In your jsp, create html for a table and table headings
  • Use either the struts <logic:iterate> or the jstl <c:forEach> tag to iterate over the list of reviews.
  • For each bean in the list, use a combination of plain html and <bean:write> tags (or <cut> tags) to create rows and cells in the table
  • close your html table tag.

  • Check the struts User Guide for details on how to use the tags.
    [ March 16, 2005: Message edited by: Merrill Higginson ]

    Consultant, Sima Solutions
    Tim Manchester
    Ranch Hand

    Joined: Nov 03, 2004
    Posts: 62
    Wow, very informative. I just learned a couple of new (and useful) tags. Thanks!


    - DM_Tim
    I agree. Here's the link:
    subject: Dynamically adding to a TABLE in a jsp
    jQuery in Action, 3rd edition