Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Indexed Properties Help!!!

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to display the following table in a jsp:

Channel Name value
1 mych1
2 testch2
3 greench3
6 daves6
9 bilston9

[Where the value column displays a column of <html:text> fields]

When I submit the <html:form> I want to be able to extract all the channelIds and their respective values that were entered by the user. Someone suggested to do this via indexed properties. Please can someone guide me on how to accomplish this.
 
Gobind Singh
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for any confusion. My question above has 3 columns (Channel , Name, and Value)
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the process:

1. Create a bean that has the properties of a single row in the table. In this example, channel, name, and value. I'll call it "ChannelBean".

2. In your ActionForm, create a property of type java.util.List that contains a List of ChannelBeans. Your actionForm should have both a regular getter and setter and an "indexed" getter and setter. The indexed getter and setter both use an int paramaeter to tell which item in the list to act upon. Here's an example:



3. In your JSP, you can write something like this:

<logic:iterate id="channelBean" name="myActionForm" property="channelBeans" >
<html:text name="channelBean" property="channel" indexed="true" />
<html:text name="channelBean" property="name" indexed="true" />
<html:text name="channelBean" property="value" indexed="true" />
</logic:iterate >

The following URLs explain more about indexed properties.

http://struts.apache.org/1.2.9/faqs/indexedprops.html

http://wiki.apache.org/struts/StrutsCatalogLazyList
 
Gobind Singh
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not want to have text fields for the channel and name. I want them to simply be labels on the screen. Their values are to be hardcoded for display on the screen. How do I go about this?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use <bean:write> instead of <html:text>

Example: <bean:write name="channelBean" property="channel" />

Note: This time we do not use indexed="true" because it's not a input field.
 
Gobind Singh
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a ChannelBean class which has 3 properties Channel, Name and Value.

I created a subclass of ActionForm with 1 property called 'lines' which is an ArrayList of ChannelBean objects.
I defined the regular and indexed getter and setters in my action form class.

Before displaying my JSP, I go through an Action class (InitChannelsAction) with the action form in request SCOPE. All this action class does is creates 6 instances of ChannelBean and sets the channel Id and Name to the correct value. We leave the value property empty. And then it sets those ChannelBeans in the action form. At the end of the execute method, I forward to a tile which displays my JSP.

In the JSP the results come out ok. I get the data I expected. My html:form action property is set to another action mapping which takes the form again in request scope. But when I submit the form I find that the list of ChannelBean's is empty.

In the action class I do something like myForm.getChannelBeans() which should return my a list of all the ChannelBeans. But this comes out as size=0.
 
Gobind Singh
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried overriding the reset() method of ActionForm to do nothing. But it doesnt have any effect.

I tried changing the scope of the ActionForm in the action-mapping to "session" instead of request. This was slightly better. I was able to pull out the channel Id and name but not the values entered by the user in the "value" text fields.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have time to give a detailed response right now, but the crux of the problem is this:

Request scope means one request. When the form is submitted, you have started a new request. If you put the ActionForm in request scope, the arraylist is gone when the form is submitted because you have started a new request. Struts instantiates the ActionForm, but it doesn't re-populate the ArrayList for you.

The simple solution is to put the ActionForm in session scope. If you really insist on it being in request scope, you need to have hidden fields for the channel Id and Name, and you need to very carefully read this link which I gave you earlier.
 
Gobind Singh
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Merril, thanks for the help. I cracked it with hidden indexed fields.
 
Good night. Drive safely. Here's a tiny ad for the road:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!