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