There have been a number of questions related to indexed properties over the past few months. There is some good information to be found on the web, but I figured that a working example might be helpful so I put together 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.
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:
Thanks, Brent. I have added the above to the FAQ as well.
note: I use edit mode to capture your code including indents. I inadvertently hit the edit button while in there, so that's why the message at the bottom indicating that I edited the message. I didn't actually change anything, however. [ November 22, 2006: Message edited by: Merrill Higginson ]
*Edit* missed that the businessObject was included in another post. Thanks again for such a great and through example. [ November 22, 2006: Message edited by: A knibbs ]
Joined: Feb 08, 2006
About the hidden indexed field for productId...In my example I am using request scope. Each "item" has three properties: productId, productName and quantity. The only one that is editable on web page is quantity. With just the indexed text field on the form, the only data that gets submitted back is the quantity. The result is that that code would have a Collection of OrderItem objects with just the quantity property filled. With the hidden tag for the productId attribute, the productId property will also be populated so I know which "line item" to tie the quantity value to.