Help please!
Process flow
============
Index.jsp is supposed to execute the getCustomer() method in customerAction class. The customerAction is supposed to put a customers ArrayList object in the request scope. the customer.jsp screen is supposed to iterate through the customer ArrayList object that was placed in the request scope and display the data in their respective fields.
My objective is to display the data brought back from the database.
When I debugged CustomerAction class with these lines of code:
Iterator i = customers.iterator();
while( i.hasNext() ){
System.out.println( "((CustomerBean)i.next()).getFirstName(): " + ((CustomerBean)i.next()).getFirstName() );
}
, I got the following out from the Arraylist object that is placed the the request scope:
((CustomerBean)i.next()).getFirstName(): OLA
((CustomerBean)i.next()).getFirstName(): OLATEST
((CustomerBean)i.next()).getFirstName(): SAUD
Please tell me the cause of the error and how to work round it. In any case, is there a more efficient way of achieving my objective?
Thanks.
Ola Oke
Error
=====
When I execute/run index.jsp screen I get the error below:
org.apache.jasper.JasperException: Cannot create iterator for this collection
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:432)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:356)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)
...
JSP FILES:
==========
index.jsp (This is the JSP the processing that leads to the error):
===================================================================
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<jsp:forward page="/customer.do?methodToExecute=getCustomer"/>
<%--
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<logic:redirect forward="/customer.do"/>
Redirect default requests to Welcome global ActionForward.
By using a redirect, the user-agent will change address to match the path of our Welcome ActionForward.
--%>
customer.jsp:
=============
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html>
<head>
<title><bean:message key="fieldL.title" /></title>
</head>
<body bgcolor="#999966" text="#000000" link="#0033FF" vlink="#FFFF00" alink="#00FF00">
<table width="500"
border="0" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
</tr>
<tr>
<td height="68" width="48%">
<div align="left">
<img src="file:///D|/Apps/Apache/Tomcat-4.1.27/webapps/employees/images/wxmainlogowhitespace.gif">
</div>
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<html:form action="/customer"
name="CustomerForm"
type="com.cargo.gen.CustomerForm" >
<table width="500" border="0">
<tr>
<td width="54"><bean:message key="fieldL.customerNo"/>:</td>
<td width="37"><html:text property="customerNo" maxlength="5" size="5"/></td>
<td width="54"><bean:message key="fieldL.firstName" />:</td>
<td width="37"><html:text property="firstName" /></td>
<td width="54"><bean:message key="fieldL.lastName" />:</td>
<td width="37"><html:text property="lastName" /></td>
</tr>
<tr>
<td><bean:message key="fieldL.dob"/>:</td>
<td><html:text property="dob" /></td>
<td><bean:message key="fieldL.sex" />:</td>
<td><html:text property="sex" /></td>
</tr>
<tr>
<td><bean:message key="fieldL.address"/>:</td>
<td><html:text property="address" size="30"/></td>
<td><bean:message key="fieldL.addressCode"/>:</td>
<td><html:text property="addressCode" size="10"/></td>
</tr>
<tr>
<td height="37"><bean:message key="fieldL.email" />:</td>
<td><html:text property="email" /></td>
<td><bean:message key="fieldL.phone" />:</td>
<td><html:text property="phone" /></td>
<td><bean:message key="fieldL.regDate" />:</td>
<td><html:text property="regDate" /></td>
<td width="54"><bean:message key="fieldL.status"/>:</td>
<td width="37"><html:text property="status" maxlength="6" size=""/></td>
<td width="64"><bean:message key="fieldL.creditRating"/>:</td>
<td width="30"><html:text property="creditRating" maxlength="6" size="1"/></td>
</tr>
<logic:iterate id="customer" name="customers">
<tr align="left">
<td><bean:write name="customer" property="customerNo"/>:</td>
<td><bean:write name="customer" property="firstName"/>:</td>
</tr>
</logic:iterate>
</table>
<html:hidden property="methodToExecute" value="error"/>
<SCRIPT>function set(target) {document.forms[0].methodToExecute.value=target;}</SCRIPT>
<html:submit on click="set('regCustomer')">Register Customer</html:submit>
<html:submit on click="set('getCustomer')">Get Customer</html:submit>
<html:submit on click="set('updateCustomer');">Save Changes</html:submit>
</html:form>
</body>
</html>
struts-config.xml file:
=======================
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD
Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- ==================================== Data Source Configuration -->
<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassName" value="com.mysql.jdbc.Driver" />
<set-property property="url" value="jdbc:mysql://localhost/cargo" />
<set-property property="username" value="ola" />
<set-property property="password" value="ola" />
</data-source>
</data-sources>
<!-- ======================================== Form Bean Definitions -->
<form-beans>
<form-bean name="customerForm" type="com.cargo.gen.CustomerForm"/>
<!-- sample form bean descriptor for a DynaActionForm
<form-bean
name="logonForm"
type="org.apache.struts.action.DynaActionForm">
<form-property
name="username"
type="java.lang.String"/>
<form-property
name="password"
type="java.lang.String"/>
end sample -->
</form-beans>
<global-forwards>
<forward name="cust" path="/customer"/>
</global-forwards>
<action-mappings>
<!-- Default "Welcome" action -->
<!-- Forwards to Welcome.jsp -->
<action path="/customer"
type="com.cargo.gen.CustomerAction"
name="customerForm"
scope="request"
parameter="methodToExecute">
<forward name="success" path="/jsp/customer.jsp"/>
<forward name="failure" path="/jsp/failure.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.cargo.resources.application"/>
</struts-config>
Action Class:
=============
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.DynaActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import java.util.ArrayList;
import java.util.Iterator;
public class CustomerAction extends DispatchAction {
public ActionForward getCustomer(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
DbInterface dbInterface = new DbInterface();
ArrayList customers = dbInterface.getCustomer( getDataSource(request) );
request.setAttribute("customers", "customers");
Iterator i = customers.iterator();
while( i.hasNext() ){
System.out.println( "((CustomerBean)i.next()).getFirstName(): " + ((CustomerBean)i.next()).getFirstName() );
}
return mapping.findForward("success");
}
public ActionForward updateCustomer(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("updateCustomer");
return mapping.findForward("success");
}
public ActionForward regCustomer(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("Register customer");
return mapping.findForward("success");
}
}