Meaningless Drivel is fun!*
The moose likes Struts and the fly likes forms submit with collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "forms submit with collection" Watch "forms submit with collection" New topic
Author

forms submit with collection

Ahsan Hadi
Greenhorn

Joined: Jul 12, 2002
Posts: 19
Hi everyone,
I have a complex struts situation on hand, atleast complex for me. I have a jsp with several rows generated by the resultset, each row should correspond to one form bean. But we want to submitt all the data in string arrays to one form bean.
How should this achieved, do we need to use the indexed property. If yes then how.
need serious help.
thanks.


HADI
Srikanth Shenoy
author
Ranch Hand

Joined: Jan 24, 2004
Posts: 184
Hi Ahsan,
You can achieve the ListForm functionality by using the indexed properties.
You dont have to have each row as a seperate Form bean.
In fact you have to have them as a form bean if you want to submit the entire collection at once.
I cover this in detail in my book - Struts Survival Guide. However I will go over how to do it briefly here.
1) To start with create a ActionForm that has a collection (a java.util.List or something of that sorts that will hold your collection.) Make sure to initialize the List in the constructor and the reset method. Otherwise you will get unexpected results.
2) Add two getters to the Struts Form - One for the Collection and one for accesing the object in the collection. If the object in the collection is a String, add a setter for that one. Otherwise the object in the collection will have its own getters and setters. No setter is required for the collection itself.
3) In the JSP, use the Iterate Tag to traverse and render the collection. For each item in the collection use the Text Tag as follows:
<html:text name="timing" property="closingTime" indexed="true"/>
Hope this helps.
Srikanth Shenoy
Author: Struts Survival Guide - Basics to Best Practices
[ February 17, 2004: Message edited by: Srikanth Shenoy ]
[ February 17, 2004: Message edited by: Srikanth Shenoy ]

Srikanth Shenoy
Author of Struts Survival Guide : Basics to Best Practices
Ahsan Hadi
Greenhorn

Joined: Jul 12, 2002
Posts: 19
Thanks for you valued suggestions. Here is what we are doing, kindly advise if the approach is correct since we are either hitting the nullpointerexception or not getter method exception:
in jsp:
<logic:iterate id="fcst" name="fcst" indexId="counter">
<html:select name="fcst" property="strType">
<html ptions collection="type" property="strType" />
</html:select>
The iterate create this HTML component for 10 rows.
</logic:iterate>
It doesnot allow us to set the indexed=true property in the select, it complains that it is not part of the tag lib.
if we do:
<html:select property="strType[0]">
<html ptions collection="type" property="strType" />
</html:select>
for testing, it doesnot find the correct getter method.
in ActionForm:
public class FinancesUpdateForm extends ActionForm {
private List strCategory;
public void setStrCategory(int index, String strCategory) {
this.strCategory.set(index, strCategory);
}
public String getStrCategory(int index) {
return (String)this.strCategory.get(index);
}
in Action class:
public class FinancesUpdateAction
extends Action {
public ActionForward perform(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
FinancesUpdateForm frm = (FinancesUpdateForm)actionForm;
System.out.println(frm.getStrCategory(0));
return actionMapping.findForward("success");
}
}
will extremely appreciate your early response.
thanks.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

strType does not match the naming of getStrCategory


A good workman is known by his tools.
Ahsan Hadi
Greenhorn

Joined: Jul 12, 2002
Posts: 19
Sorry my mistake, but that is just cut and paste mistake. We have property strCategory in the jsp and trying to using getstrCategory as its getter.
but the error persits.
thanks.
mr Cranberry
Greenhorn

Joined: Jul 18, 2007
Posts: 5
I'm trying very hard to use the "indexed='true'" feature of struts to manage a very simple ArrayList of objects in a page. It displays just fin, and I've checked the html code and it's populating the indexed properties just fine. But, when I post the form to an action, the constructor on the form gets called just fine but what I can NOT seem to get to happen correctly, is the part where Struts uses reflection to populated the list with the data on the html page. I just don't get why that part isn't happening... any ideas?

Config excerpt:

<action path="/VehicleDisplay"
type="packagename.presentation.action.vehicle.VehicleDisplayAction"
name="VehicleMaintenanceForm"
scope="request"
validate="false">
<forward name="success" path="vehicle.maintenanceThirdParty"/>
</action>

<action path="/SaveVehicleInfo"
type="packagename.presentation.action.vehicle.SaveVehicleInfoAction"
name="VehicleMaintenanceForm"
scope="request"
validate="true">
<forward name="success" path="vehicle.maintenanceThirdParty"/>
<forward name="stay" path="/SetupNewAccount.do"/>
<forward nam

Form:

public class VehicleMaintenanceForm extends ValidatorForm {

private List vehicleList;

public VehicleMaintenanceForm() {
vehicleList=new ArrayList(4);
for (int i=0; i<4; i++) {
vehicleList.add( new TagDTO());
}
}

public List getVehicleList() {
return vehicleList;
}

public void setVehicleList(List vehicleList) {
this.vehicleList = vehicleList;
}

}

Jsp excerpt:

<html:form action="/SaveVehicleInfo">

<logic:iterate name="VehicleMaintenanceForm" property="vehicleList" id="lineItem" indexId="rowNum">
<tr class="oddd">

<td align="center">
<html:text name="lineItem" indexed="true" property="licPlate"/>
</td>

</logic:iterate>

A breakpoint and trace thru the action "SaveVehicleInfo" confirms that the list is empty and was never populated by the framework...

Any ideas anyone?? What piece am I missing here? Thanks
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
When you use indexed="true" you must also create an indexed getter for the indexed propery. To make your example work, you'd add the following to your VehicleMaintenanceForm:

For more information plus a working example, see question 6 in the JavaRanch Struts FAQ


Merrill
Consultant, Sima Solutions
mr Cranberry
Greenhorn

Joined: Jul 18, 2007
Posts: 5
Thanks; I finally figured that out late last night lol.

Now the problem I'm having is, it calls that getLineItem method if I'm posting to an action that's in the html:submit action clause. But then when I use javascript to set the action of the form to another action, struts does call the constructor on the form but it never calls the getLineItem and so I wind up with blank rows and it doesn't populate the form
mr Cranberry
Greenhorn

Joined: Jul 18, 2007
Posts: 5
I wish I could specify an "action=" on the html:submit tag, in other words.
mr Cranberry
Greenhorn

Joined: Jul 18, 2007
Posts: 5
I wish I could specify an "action=" on the html:submit tag, in other words.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forms submit with collection