• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamically adding to a TABLE in a jsp

 
Tim Manchester
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

Regards,

- DM_Tim
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64716
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably most appropriate to the Struts forum, so moved there.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
     
    Tim Manchester
    Ranch Hand
    Posts: 62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wow, very informative. I just learned a couple of new (and useful) tags. Thanks!

    Regards,

    - DM_Tim
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic