iam able to populate the collection values in JSP but not able to retrieve in Action class. iam pasting the code below. Can someone help me whats wrong with the code here.
public ArrayList getVendorList() { System.out.println("form bean 1"); System.out.println("vendor list = " + vendorList); return (this.vendorList); }
public void setVendorList(ArrayList vendorList) { this.vendorList = vendorList; } }
iam able to populate the values in <html:select> tag. But once after selecting the values and click the submit button iam getting error. The values are not populated into "Form Bean". iam expecting an array list of vendors to be populated into Form bean array list.
We're pleased to have you here with us here at JavaRanch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.
In particular, your display name must be a first and a last name separated by a space character.
Thanks, Jeanne Forum Sheriff [ March 23, 2008: Message edited by: Jeanne Boyarsky ]
First of all, it's important to understand that anything that comes from an HTML form when it's submitted is a String or an array of Strings. HTML knows nothing about any other Java object types such as ArrayLists.
When Struts populates an ActionForm, it is using data from the HTML form as it was submitted, so the only data that it can accept as parameters to the setters on your Action form is either a String or an array of Strings. There are some exeptions to this rule. If your setter has an int parameter, for example, Struts will try to convert it. However, an ArrayList is not one of those exceptions, and is never allowed as a parameter to an ActionForm setter method that will be called by Struts.
So, in your example, the only way Struts will properly call the setVendorList method is if it accepts a String array as a parameter, not an ArrayList. That string array will consist of only the vendor numbers for each vendor selected, since that's what you specified as the property attribute.
One more word about best practices: It's generally not a good idea to build a list such as your list of vendors in the JSP. In a real situation, you would likely be getting the list from a DataBase, and in a Model/View/Controller architecture, a view object such as a JSP should not be getting data from a database.
I'd suggest you create a preparatory action for this JSP that populates the list of vendors, places it either in the ActionForm as a property or by itself in request scope, and then forwards to the JSP. [ March 23, 2008: Message edited by: Merrill Higginson ]
I have converted the ArrayList to String[] and i was able to see multiple values selected.
I still have one more question. How do i retrieve "vendorName" in the above scenario along with "vendorNumber" as i need to populate the Vendor object and pass it back to business tier.
Thanks, Raj Chukka
Merrill Higginson
Ranch Hand
Joined: Feb 15, 2005
Posts: 4864
posted
0
Raj,
Don't forget to change your display name... It's important.
regarding your question:
The only thing the HTML form is going to send is the Vendor number, not the vendor name. There's no way around that. However, you can structure your application so that you can look up the complete vendor object given the vendor number. In a database application, you can just query the database for a given vendor number and get the rest of the vendor information. If you already have all the vendors in a list, you can look up the vendor in the list. When doing this type of lookup in Java, it's helpful to ovverride the object's equals() method.
In the example below, I've overriden the equals method such that two Vendors are considered equal if they both have the same vendor number. That way later on when I'm looking for a specific vendor number in a list of vendors, I can create a search object with only the vendor number populated and still have the statement vendorlist.indexOf(searchVendor) find the correct object.
Here's the example:
The new Vendor object:
The new PrepareVendor Action:
The entries in the struts-config.xml file:
The JSP:
The new TestAction. Note the use of vendorList.indexOf(searchVendor) that I talked about earlier. If I hadn't overridden the equals method of the Vendor class, this would not work.