Bookmark Topic Watch Topic
  • Mark post as helpful
  • send pies
  • Report post to moderator
I have an ArrayList of JavaBeans in my Action and would like my JSP to display a table of line items that has input fields for each bean in the list. How do I do that in Struts 2?

We have an example of how to do this in Struts 1 here: IndexedProperties

I have updated our example for Struts 2. 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. The user can also add items to the order. For the purposes of this example, we will skip many things, like validation, that one would want in production code.

entries from struts-config.xml

We can update the entire order (indexedPropertyUpdate) and insert a new line item (indexedPropertyInsert)

OrderItem: a simple data bean

This bean holds a single line item.

OrderAction: action that processes adding or updating line items

Note that in these actions I'm using the session to store the order line items between requests. Normally, one would use a Data Access Object to persist the order in a database (though you may also choose to build the order in the session then submit it to the DAO when the order is finalized).

order.jsp: the forms

This form displays the line items for an order. One can edit the order and click on the "Update Order" button to save changes.

The important thing to notice here is how the textfields inside the iterator are configured. The "key" attribute contains the all-important OGNL to traverse the List of OrderItems. If you view the source of the form once it is in the browser, you will see that the index is preserved in the HTML attribute names. This is how Struts rebuilds the List when the form is submitted back to the Action.

This form displays fields to create a new line item.

    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!