• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Indexed Properties Example

 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*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
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic