• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

forms submit with collection

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ahsan Hadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
strType does not match the naming of getStrCategory
 
Ahsan Hadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
mr Cranberry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wish I could specify an "action=" on the html:submit tag, in other words.
 
mr Cranberry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wish I could specify an "action=" on the html:submit tag, in other words.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic