aspose file tools*
The moose likes Struts and the fly likes problem in accessing a  list through a iterate tag. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "problem in accessing a  list through a iterate tag." Watch "problem in accessing a  list through a iterate tag." New topic
Author

problem in accessing a list through a iterate tag.

tijendra kumar
Greenhorn

Joined: Oct 11, 2007
Posts: 7
org.apache.jasper.JasperException: Exception in JSP: /Success.jsp:26

23:
24:
25: <logic:iterate id="customer" name="CustomerForm" property="cusList" indexId="index">
26: <bean:write name="customer" property="firstName"/>
27:
28: </logic:iterate>
29:

javax.servlet.ServletException: No getter method for property firstName of bean customer
-------------------------------------------------------------------------------------------CustomerForm.java-------------
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


/**
* CustomerForm is the object representation of the html form for entering
* customer's first and last name in Struts.
*
* @author Srikanth Shenoy
* @version $Revision: $ $Date: $
*/
public class CustomerForm extends ActionForm {
/**
*
*/
private static final long serialVersionUID = 1L;

/** Customer First Name */
private String firstName;

/** Customer Last Name */
private List cusList;
private String lastName;
private String gender;


/**
* This is the string that will hold the name of the submit button that
* submitted the CustomerForm. This will hold the name of the submit
* button only if the <html:submit property="step"> is used
*/
private String step;

public CustomerForm() {
firstName = "";
lastName = "";
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String s) {
this.firstName = s;
}

public String getLastName() {
return lastName;
}

public void setLastName(String s) {
this.lastName = s;
}

public String getStep() {
return step;
}

public void setStep(String string) {
step = string;
}

/**
* This method is called by the RequestProcessor immediately after populating the
* CustomerForm only if the validate=true flag is set in the struts-config.xml
* Do validation here and return ActionErrors if any, to the RequestProcessor
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();

// Firstname cannot be empty
if ((firstName == null) || firstName.trim().equals("")) {
errors.add("firstName",
new ActionError("error.cust.firstname.empty"));
}

// Lastname cannot be empty
if ((lastName == null) || lastName.trim().equals("")) {
errors.add("lastName", new ActionError("error.cust.lastname.empty"));
}

return errors;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

@SuppressWarnings("unchecked")
public void setcustomerFormObj(int index, Customer empObj) {
cusList.set(index, empObj);
}

@SuppressWarnings("unchecked")



public List getCusList() {
return cusList;
}

public void setCusList(List cusList) {
this.cusList = cusList;
}

public Customer getCustomer(int index)
{
// make sure that cusList is not null
if(this.cusList == null)
{
this.cusList = new ArrayList();
}

// indexes do not come in cus, populate empty spots
while(index >= this.cusList.size())
{
this.cusList.add(new Customer());
}

// return the requested item
return (Customer) cusList.get(index);
}
}


------------------------Success.jsp----------------------

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html xhtml="true">

<head>
<title><bean:message key="exercise01.successpage.title" /></title>
<html:base/>

</head>

<body>
<h2><bean:message key="exercise01.successpage.title" />
<bean:write name="CustomerForm" property="firstName" />
<bean:write name="CustomerForm" property="lastName" />
<bean:write name="CustomerForm" property="gender" />
</h2>

<h3><bean:message key="exercise01.successpage.message" /></h3>



<logic:iterate id="customer" name="CustomerForm" property="cusList" indexId="index">
<bean:write name="customer" property="firstName"/>

</logic:iterate>




<html:link page="/showCustomerForm.do">Go Back</html:link>
</body>

</html:html>

-------------------------Customer.java-----------------------------------


public class Customer {
private String firstName;


private String lastName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

}
-------------------customeraction.java--------------

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.sql.*;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
* CustomerAction handles all form submission requests for CustomerForm.
*
* @author Srikanth Shenoy
* @version $Revision: $ $Date: $
*/
public class CustomerAction extends Action {
@SuppressWarnings("unchecked")
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionForward nextPage = null;
javax.sql.DataSource dataSource;
java.sql.Connection myConnection = null;
ArrayList arlist = new ArrayList();
if (isCancelled(request)) {
System.out.println("Cancel Operation Performed");

return mapping.findForward("mainpage");
}

CustomerForm custForm = (CustomerForm) form;
dataSource = getDataSource(request);
myConnection = dataSource.getConnection();

Statement stmt = myConnection.createStatement();
ResultSet rst = stmt.executeQuery("select * from person");

if ("Save".equals(custForm.getStep())) {
//String firstName = custForm.getFirstName();
//String lastName = custForm.getLastName();

//System.out.println("Customer First name is " + firstName);
//System.out.println("Customer Last name is " + lastName);
int i=0;
while (rst.next()) {
System.out.println("First Name is: " + rst.getString("fname"));
System.out.println("Last Name is: " + rst.getString("lname"));
/// custForm.setFirstName(rst.getString("fname"));

arlist.add(i++,rst.getString("fname"));
}


nextPage = mapping.findForward("success");
}

custForm.setCusList(arlist);

myConnection.close();

return nextPage;
}
}


iam getting records from a table person and want to display all the records in the success.jsp. iam using iterate tag is there any other way to do this....
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Here are a few lines from your Action class:



It looks like you are filling cusList with String values but you should be filling it will Customer objects.

- Brent
Ambikesh Khiriya
Greenhorn

Joined: Sep 26, 2007
Posts: 5
<logic:iterate id="customer" name="CustomerForm" property="cusList" indexId="index">
<bean:write name="customer" property="firstName"/>

</logic:iterate>


try specifing the tyype of the object like in

<logic:iterate id="customer" name="CustomerForm" property="cusList" type="java. .. String">

<bean:write name="customer" property="firstName"/>

</logic:iterate>

It sould work
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in accessing a list through a iterate tag.