permaculture playing cards*
The moose likes Struts and the fly likes indexed=true attribute in [html:text] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "indexed=true attribute in [html:text]" Watch "indexed=true attribute in [html:text]" New topic
Author

indexed=true attribute in [html:text]

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
What would happend if I use this
<html:text indexed="true" property="something">

The spec says the generated html tag will have a name attribute of the format 'id[index].propertyName'. I know the 'index' comes from the enclosing logic:iterate tag and the 'propertyName' is the value of the property= attribute. But where does 'id' come from? I tried this myself & got 'org.apache.struts.taglib.html.BEAN' used as the 'id' which is the attribute name of the FormBean corresponding to the enclosing <html:form> tag!

I know the 'indexed=true' attribute is needed to work with Indexed Properties and LazyActionForm but it seems that it was never properly documented.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The id refers to the id attribute of the enclosing <logic:iterate> tag. Here's an example.

Suppose you have a Customer bean with name, address and customerNumber properties, and that you have a List of these customer beans as a property of your ActionForm bean. In order to use indexed properties, you will have to create an indexed getter and setter for an individual customer in the list like this:


You could then code the following in your JSP:

Struts would then create a property name of customer[index].customerNumber.
There's one more thing: If your ActionForm is in request scope, you will have to give your indexed getter "lazy initialization" capability. This link will explain how to do that.


Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
This link has a better description of indexed properties: http://struts.apache.org/1.x/struts-taglib/indexedprops.html

For the group of tags that includes html:text, it says: these tags will generate an HTML "name" attribute of "name[nn].property"

It took me a long time to figure out something that is mentioned in the wiki page that Merrill linked: "The trick is to name the id attribute to the same as the indexed property."

Notice how in Merrill's example he has an id value of "customer" a name value of "customer" and a method named getCustomer.

- Brent
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Thanks. It also me a very long time to figure what is going on with this
single attribute! Another question, why would we ever want to use indexed Properties in the Form bean?
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Indexed properties is a topic that comes up often on this forum. An example would be a shopping checkout page where you had a list of products in the person's shopping bag and you had a text field on each line where the user could change the quantity. This type of example where you have a variable number of items to edit is a perfect use for indexed properties.

- Brent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: indexed=true attribute in [html:text]