• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamically adding to a TABLE in a jsp

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is probably most appropriate to the Struts forum, so moved there.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 <c:out> 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Wow, very informative. I just learned a couple of new (and useful) tags. Thanks!

    Regards,

    - DM_Tim
    reply
      Bookmark Topic Watch Topic
    • New Topic