This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

html:hidden within logic:iterate

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jamie,
Sounds like an ideal candidate for Indexed Properties.
-Jim
 
Jamie Lee
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)")
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try with this:

<logic:iterate id="beanID" name="beanName" property="collectionName">
<input type="hidden" value=<bean:write name="beanID"/>>
</logic:iterate>
 
Nacho Espinosa
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for all the help, everyone!
 
Jim Hardin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Brent -- have a good one.
-Jim
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic