Bookmark Topic Watch 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I have an ArrayList of JavaBeans in my ActionForm and would like my JSP to display a table that has input fields for each bean in the list. How do I do that in Struts?

Note that this example is for Struts 1. There is a Struts 2 example in the Struts 2 FAQ

In order to do this, you must use "indexed properties". This term refers to Struts' ability to use a property name that has an index attached to it. It does take a bit of effort to learn how to use indexed properties, but once you get the hang of it, indexed properties can be a very useful tool.

You should start by reading these links:


  • One of our frequent JavaRanch contributors, Brent Sterling, has also written a simple example of using indexed properties. His code demonstrates the use of "lazy intitialization" in the indexed getter so that the ActionForm can be placed in request scope without causing an "index out of bounds" exception. The code and his comments are included below.

    In the following example, the page is a simplified shopping cart where the user sees a list of items in their shopping cart and can edit the quantities of each item.

    entries from struts-config.xml

    OrderIndexedForm: the ActionForm

    OrderItem: a simple data bean

    DisplayOrderIndexedAction: action that shows the page

    order_indexed.jsp: the form section

    SaveOrderIndexedAction: the action that saves the updated quantities

    Two tips that I have learned that seem to cause problems are:

    Use a different name for your indexed get method

    In my example I had a list of "order" items. For the get method that returns a List I chose the name getOrderList. For my indexed get method I chose the name getOrderItem. Some example use a name like "getOrders" for both methods, but this causes problems with some versions of Struts/BeanUtils, plus it seems more confusing to me. I also see examples that use names like getOrders and getOrder but to me the difference is subtle enough to cause confusion.

    Note that the method setOrderList is never called by Struts. It is only called from DisplayOrderIndexedAction.

    Make sure the id attribute of your iterate tag matches the property name of your indexed get method

    Note that the id attribute of my iterate tag is "orderItem" and my indexed get method is named "getOrderItem". If they do not match, then your page will display fine but your form will not be populated when the page is submitted.

    It took me a while to figure this one out. For the first two years after I found out about indexed properties I did not use the indexed="true" attribute. Instead I used JavaScript to create the index like in the "Dynamic Indexes for Indexed Properties" section of the Struts document.

    Note that my example completely avoided the topic of validation. I have always implemented validation for indexed properties by implementing a validate method in the form (plus I never use client side validation). My understanding is that the validator framework does not support indexed properties.

    I also did not do any translation between "business/data objects" and "presentation objects". Struts wants String properties for editable fields. Note that the quantity property in OrderItem is a String. In reality your business layer would return an object where quantity was an int or Integer (or long/Long) and the code would have to do some translation. This translation would occur in reverse when the quantity was updated.

    For completeness, here is my dummy/mock business layer object:

    Return to StrutsFaq
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
      Bookmark Topic Watch Topic
    • New Topic