File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes html:hidden within logic:iterate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "html:hidden within logic:iterate" Watch "html:hidden within logic:iterate" New topic
Author

html:hidden within logic:iterate

Jamie Lee
Greenhorn

Joined: Jan 31, 2007
Posts: 17
Hello everyone,

Does anyone know how I can use a html:hidden tag to specify a collection value within a logic:iterate tag?
For example, I have something like the following code:



The logic:iterate tag is iterating through a collection ("collectionName") which is a property of the bean "beanName" and "beanID" is the name of the bean holding the current element of the collection.
I'd like to set the html:hidden tag to the value of the bean exposed by the iterator ("beanID") - this code hasn't worked for me thus far. It correctly sets the name attribute of the hidden tag, but will not set the value attribute.

Any ideas?
Thanks so much!
Jim Hardin
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
Jamie,
Sounds like an ideal candidate for Indexed Properties.
-Jim
Jamie Lee
Greenhorn

Joined: Jan 31, 2007
Posts: 17
Thanks for the reply, Jim. However, I don't see how I will be able to use this to set the value of the hidden tag. I've tried collectionName.get(i) where i is the indexId, and it is interpreted literally as a String (the value is literally "collectionName.get(i)")
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
The sample code that I submitted a while back for the FAQ includes a hidden field using indexed properties: http://faq.javaranch.com/view?IndexedProperties

- Brent
Jim Hardin
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
Brent,
I applaud the effort you put into creating an example -- to minimize confusion, perhaps it should be minimilly updated as follows:

For struts-config.xml
For the action "/DisplayOrderIndexed", remove /WEB-INF/ from the "success" forward. Nothing can be served out of WEB-INF.
For the action "/SaveOrderIndexed", remove ficticious attribute "indexed", specifically, delete the line which reads indexed="/DisplayOrderIndexed.do".

For OrderIndexedForm (form bean)
The general problem is that the indexed property setter, with signature public void setOrderItem(int index, OrderItem o) is missing, and the comment which is currently adjacent to "getOrderItem" goes instead with the missing "setOrderItem" method. Plus, body of "getOrderItem" indexed-property getter need only contain:

// return the requested item
return (OrderItem) orderList.get(index);

, the lines:

// make sure that orderList is not null
if(this.orderList == null)
{
this.orderList = new ArrayList();
}

// indexes do not come in order, populate empty spots
while(index >= this.orderList.size())
{
this.orderList.add(new OrderItem());
}

serve only to create a bug which would clear orderList collection each time the indexed getter is invoked. This bug might be serious enough to suggest removal of the example from publication until corrected.
The missing "setOrderItem" should look something like:


For order_indexed.jsp:
Perhaps indexed="true" attributes should be added to:

<td><bean:write name="orderItem" property="productId" /></td>
<td><bean:write name="orderItem" property="productName" /></td>

something like:


I was just looking over the example, and I saw the *big* problems with OrderIndexedForm and was just concerned that a neophyte might get frustrated with trying to make this work, and be left with a bad impression of the Struts framework. Again, excellent effort, and I do appreciate any consideration you may give to this feedback. Thank you very much.
-Jim
Jamie Lee
Greenhorn

Joined: Jan 31, 2007
Posts: 17
Thank you both for your replies! However, the tutorial is a bit different than what I'm trying to do...the Collection I'm iterating through contains String values instead of beans. When I simply specify the iteration "id" property as the html:hidden property, the result is null...

Thanks again!
Nacho Espinosa
Ranch Hand

Joined: Jan 17, 2007
Posts: 30
Try with this:

<logic:iterate id="beanID" name="beanName" property="collectionName">
<input type="hidden" value=<bean:write name="beanID"/>>
</logic:iterate>


Greetings<br />Nacho Espinosa<br />SCJP 1.4
Nacho Espinosa
Ranch Hand

Joined: Jan 17, 2007
Posts: 30
I forgot the property beanID.


<logic:iterate id="beanID" name="beanName" property="collectionName">
<input type="hidden" name="beanID" value=<bean:write name="beanID"/>>
</logic:iterate>
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Jim:

Looking over your comments, I do see one valid issue. I am pretty sure that the line in the action mapping that reads indexed="/DisplayOrderIndexed.do" should read input="/DisplayOrderIndexed.do".

You suggest the addition of a method with this signature:

Struts would never call this method when dealing with objects. It would call a method like this if instead of using an object like OrderItem it was dealing with basic types like String. This is easy to describe in person but tough to write in text. The example uses indexed properties for the quantity value. When the user enters a value and submits the form, these will translate into a call like this:

This is why the getOrderItem method has to be written in the way that it is. Let me know if you would like me to explain further.

The bean:write usages are read-only so there is no reason to make them as indexed properties.

Edit...also it is fine to put your jsp files under WEB-INF and in fact this is generally the recommended practice. External users cannot access files under WEB-INF so this forces all jsp access to go through actions.

- Brent
[ April 16, 2007: Message edited by: Brent Sterling ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Jamie...back to your question....When dealing with types like Strings instead of object I have not figured out how to use the indexed attribute. Here is an example that builds the index using some scriptlet code...you should be able to apply this to your html:hidden usage.


- Brent
[ April 16, 2007: Message edited by: Brent Sterling ]
Jamie Lee
Greenhorn

Joined: Jan 31, 2007
Posts: 17
Thank you for all the help, everyone!
Jim Hardin
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
Brent,
Whoa, very good -- I stand corrected, OrderIndexedForm is quite correct (a setter is not required -- and your explanation makes sense). I was *erroneously* thinking BeanUtils was using reflection and invoking the indexed property mutator on submit (I *erroneously* had Indexed Properties on my brain). My apologies, thank you very much for straightening me out! I am appropriately humbled.
Could you do me a favor, regarding JSP(s) in WEB-INF folder, would you mind citing a source for:
and in fact this is generally the recommended practice

if you have one? I didn't know it was standard practice. Thanks, Brent.
-Jim
By the way, I too have had little luck with using the indexed="true" attribute on the HTML tags, and resort to scriptlet **exactly** as you suggested... Thanks again.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
The usage of WEB-INF is something that I learned from this forum. I searched around to see if I could find other references...

Here is a thread on the struts-user mailing list with a little debate about this practice:
http://www.servlets.com/archive/servlet/ReadMsg?msgId=294051&listName=struts-user

Here is a JavaWorld article where the author states "the best practice is to keep the pages behind WEB-INF."
http://www.javaworld.com/javaworld/jw-09-2004/jw-0913-struts.html

- Brent
Jim Hardin
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
Thank you, Brent -- have a good one.
-Jim
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: html:hidden within logic:iterate