This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Struts and the fly likes Indexed Properties Example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Indexed Properties Example" Watch "Indexed Properties Example" New topic
Author

Indexed Properties Example

Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
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 ==


- Brent
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Excellent example, Brent! I've added this example to the JavaRanch Struts FAQ crediting you as the author.
[ November 21, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
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:


- Brent
[ November 22, 2006: Message edited by: Merrill Higginson ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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 ]
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
*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 ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
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.

- Brent
[ November 27, 2006: Message edited by: Brent Sterling ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Indexed Properties Example
 
Similar Threads
Dynamic Row creation at client side and populating the form bean
Check box and Indexed properties
html:hidden within logic:iterate
Struts1: Nested/Indexed properties issue
Not able to submit data