• 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

Problem with html:options

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

1. JSP.

<%@ page language="java"%>
<%@ page import="java.util.ArrayList,strutsexamples.Vendor" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<head>
<title>Test html:multibox Tag</title>

<%

ArrayList vendors = new ArrayList();

vendors.add(new Vendor("V1", "Vendor 1"));
vendors.add(new Vendor("V2", "Vendor 2"));
vendors.add(new Vendor("V3", "Vendor 3"));
vendors.add(new Vendor("V4", "Vendor 4"));
vendors.add(new Vendor("V5", "Vendor 5"));
vendors.add(new Vendor("V6", "Vendor 6"));
vendors.add(new Vendor("V7", "Vendor 7"));
vendors.add(new Vendor("V8", "Vendor 8"));
vendors.add(new Vendor("V9", "Vendor 9"));
vendors.add(new Vendor("V10", "Vendor 10"));
pageContext.setAttribute("vendors", vendors);

%>

</head>
<body bgcolor="white">

<html:form action="example.do">

<table border="0" width="100%" align="left">

<tr>
<th align="right">Multiple Select From A Collection (Using <html ptions> :</th>
<td align="left">
<html:select property="vendorList" size="10" multiple="true">
<html ptions collection="vendors" property="vendorNumber" labelProperty="vendorName"/>
</html:select>
</td>
</tr>
<tr>
<td> </td>
<td><html:submit value="submit"/></td>
</tr>
</table>
</html:form>
</body>
</html:html>


2. Vendor.java

package strutsexamples;

public class Vendor
{

private String vendorNumber;
private String vendorName;

public void setVendorNumber(String number) {
System.out.println("1"+number);
this.vendorNumber = number;
}
public void setVendorName(String name) {
System.out.println("1"+name);
this.vendorName = name;
}
public String getVendorNumber() {
return vendorNumber;
}

public String getVendorName() {
return vendorName;
}

public Vendor (String vendorNumber, String vendorName) {
this.vendorNumber = vendorNumber;
this.vendorName = vendorName;
}

}


3. Action class

package strutsexamples;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


public class TestAction extends Action {

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

TestFormBean testFormBean = (TestFormBean)form;

System.out.println("action 1");
ArrayList vendors = testFormBean.getVendorList();

for (int i=0; i < vendors.size(); i++) {
Vendor vendor = (Vendor) vendors.get(i);
System.out.println("Vendor Number = "+ vendor.getVendorNumber());
System.out.println("Vendor Name = "+vendor.getVendorName());
}
return mapping.findForward("success");

}
}

5. Form Bean

package strutsexamples;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import java.util.ArrayList;

public class TestFormBean extends ActionForm {


private ArrayList vendorList = new ArrayList();

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.

Thanks in advance.

Thanks,
Raj
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Raj",

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 ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Raj Chukka
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Merrill for the Reply.

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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



You would start this application with the URL: http://localhost:8080/strutstest/prepareVendor.do

[ March 23, 2008: Message edited by: Merrill Higginson ]
[ March 23, 2008: Message edited by: Merrill Higginson ]
reply
    Bookmark Topic Watch Topic
  • New Topic