GeeCON Prague 2014*
The moose likes Struts and the fly likes save indexed properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "save indexed properties" Watch "save indexed properties" New topic
Author

save indexed properties

robert sturn
Greenhorn

Joined: Jul 17, 2008
Posts: 17
I am using indexed property to display lables and a select box.
I want to know how we can save the changes done by user in the selection in select box.

Here is my code
my.jsp
<blockquote>code:
<pre name="code" class="core">
<logic:iterate id="routeVO" name="mlpiForm" property="allRoutes" indexId="index" >
<tr bgcolor="#EEEEEE" >
<td class="mdtabletext" align="center" width="50%">
<html:text name="routeVO" property="routeName" indexed="true" />
<html:hidden name="routeVO" property="unitId" indexed="true"/>
<html:hidden name="routeVO" property="unitName" indexed="true" />
<html:hidden name="routeVO" property="routeId" indexed="true" />
<html:hidden name="routeVO" property="routeName" indexed="true" />
<html:hidden name="routeVO" property="routeDescription" indexed="true" />
</td>
<td class="mdtabletext" align="center" width="50%">
<html:select name="routeVO" property="unitId" indexed="true">
<html ptions collection="allRoutes" labelProperty="unitName" property="unitId" />
</html:select>
</td>
</tr>
</logic:iterate>
</pre>
</blockquote>
myform.java
<blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre name="code" class="core"><font size="2">
private List allRoutes

public RouteVO getRouteVO(int index)
{
System.out.println("2136..entering getRouteVO " + index);
// make sure that allRoutes is not null
if(this.allRoutes == null)
{
this.allRoutes = new ArrayList();
}

// indexes do not come in order, populate empty spots
while(index >= this.allRoutes.size())
{
System.out.println("2136..Adding new RouteVO() " + index);
this.allRoutes.add(new RouteVO());
}

// return the requested item
System.out.println("2136..Leaving getRouteVO " + ((RouteVO)(allRoutes.get(index))).getUnitName());
return (RouteVO) allRoutes.get(index);
}

public void setRouteVO(int index,RouteVO rVO)
{
System.out.println("Changing the routeVO" + index);
allRoutes.set(index,rVO);
System.out.println("Change complete");
}

</font></pre><hr></blockquote>
myAction.java

<blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre name="code" class="core"><font size="2">
inside execute method
routes = (ArrayList)myDAO.getAllRoutes();
myform.setAllRoutes(routes);

//During save
List displayedList = myform.getAllRoutes(); <<=== this doesnt give me the correct values of select selected by user.

</font></pre><hr></blockquote>

[ July 17, 2008: Message edited by: robert ]
[ July 17, 2008: Message edited by: robert ]
robert sturn
Greenhorn

Joined: Jul 17, 2008
Posts: 17
I have already gone through the faq
http://faq.javaranch.com/java/IndexedProperties
This faq gives a way to display the text elements.
I am having problem with html:select tag.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Unless I'm totally missing the point, I'm seeing some circular logic here. routeVO is a member of the allRoutes list, and you're trying to set a property of routeVO in the select statement. That means that you're trying to make the object that collects the selected route Id the same as the object that's used to build the collection of possible routes. You need to have a list or array of selected route IDs that's separate from the list of possible route IDs.
[ July 18, 2008: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
robert sturn
Greenhorn

Joined: Jul 17, 2008
Posts: 17
I did not understand Merril.Let me explain further.

allRoutes is a List containing RouteVO objects.


JSP


Here I am trying to use index property so that I get the values selected by user correctly.

Index property is woring correctly for html:text but not for html:select



These options are populated by same list ie allRoutes.So it displays only unitIds from allRoutes collection.

I dont know what is wrong with this.Page is displayed correctly.But when i try to save it saves only html:text not html:select.

html:select values are the same which are rendered during original page load.
[ July 18, 2008: Message edited by: robert sturn ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
First of all let me say that there is no difference whatever in the way indexed properties behave between an html:text tag and an html:select tag. That is not the problem.

What I'm trying to tell you is that it doesn't seem right to use the same collection to reflect the selection made by the user that you're using as the list of available options. It would seem to make more sense to have the select tag reference a different array to reflect the selection made by the user than is used to produce the list of options. Maybe if I understood your application better, I would understand why you need to do this.

That being said, I believe the reason your code does not behave as you expect is that you reference the same property twice in the same iteration: Once in an html:hidden tag and once in an html:select tag:

Doing so will produce unpredictable results because each one will cause struts to call the setter in the ActionForm, and whichever gets called last will supersede the other one. If you want the html:select tag to change this property, you must remove the corresponding html:hidden tag.
robert sturn
Greenhorn

Joined: Jul 17, 2008
Posts: 17
Merril,

Thankx for your reply.I realized that changed by jsp like this.
Now this code is like this.

[ July 18, 2008: Message edited by: robert sturn ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Putting comment tags around Struts tags does not stop them from being executed. You need to remove them entirely.

I still believe there is a fundamental design flaw in your application, but can't make suggestions on what to do about it since I don't know what you're trying to accomplish.
robert sturn
Greenhorn

Joined: Jul 17, 2008
Posts: 17
Merri,

Thankx for your patience.This is what I am trying to accomplish.

RouteVO represents has members unitId,routeName.(String values)

End users can see the routeName,unitId stored in database in a html table.
They should be able to change the unitId for any routeName.

This is the reason I display want to display unitId as a dropdown.

Now jsp part.In my jsp i have a iterator to iterate through RouteVOs (allRoutes).
So each html row represents one RouteVO object.But in this row instead of displaying ony one unitId I want to display list of available unitIds.

Hope this is clear.This is the reson why I use same collection for iterating rows and populating dropdown.Is this the correct way of using or I need some change in the design.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you're going to change a n ID in a database table, you will need to know both what the original ID was and what the new ID is. I'd suggest you add a property to your routeVO named something like "newUnitId" and use that property in your html:select tag. If you do this, you can refer to the old ID with the unitID property and the new one with the newUnitId property.
robert sturn
Greenhorn

Joined: Jul 17, 2008
Posts: 17
Merrill,

I appriciate your help.I am stiil not able to retain and get the values changed by the user.When user submitts the form with changed values,in my action class I get the old values.

Say for example dropdwon displays unit1,unti2,unit3 and user
initial state
row1 --- unit1 (dropdown)
row2 --- unit2 (dropdown)
row3 --- unit3 (dropdown)


User changes
row1 --- unit3 (dropdown)
row2 --- unit1 (dropdown)
row3 --- unit1 (dropdown)

I am expecting that I get these values in action class.

But I always get this
row1 --- unit1 (dropdown)
row2 --- unit2 (dropdown)
row3 --- unit3 (dropdown)

I dont know why...
robert sturn
Greenhorn

Joined: Jul 17, 2008
Posts: 17
Merrill,

I figured out the problem.
All the problem was in RouteVO.java,

unitId was an integer.I made it string and all started working great.

So finally here is my jsp


myform.java
private List allRoutes ; //Collection to store all the lots contains RouteVOs within.
private List routeOptions ;

and respectibe getter and setter
and


Thanks for all you time and suggestions.
 
GeeCON Prague 2014
 
subject: save indexed properties