What's with all the <% %> business? Things to watch out for, method names and the object cast to the jsp need to match names (e.g. foo.getEmployee() and ${foo.employee}). The form must be scoped to session if you are dynamically changing the size of the indexed property.

<html:text name="bean" property="property" indexed="true" />

A better example would be a form bean with a getEmployees() method and a setEmployee rather than getBeanList or whatever it was.


public Object[] getEmployees() { return emplyeeList.toArray(); }

public void setEmployees(ArrayList employeeList) {
        this.employeeList = employeeList;
}

public Employee getEmployee(int i) {
        return (Employee) employeeList.get(i);
}

public void setEmployee(int i,Employee employee) {
        this.employeeList.add(i,employee);
}


..


public class Employee {
        private String name;

        public String getName() {
                return name;
        }
        etc
}

..

<logic:iterate id="employee" name="employeeForm" property="employees">

<html:"text name="employee" property="name" indexed="true" />

</logic:iterate>

or

<c:forEach var="employee" items="${employeeForm.employees}">
        ..



On 20 Jan 2004, at 08:56, <[EMAIL PROTECTED]> wrote:

I am resending my earlier mail on this user list..Go through the sample code
and
ask me if u don't understand something.


The important portions are commented.Especially look at the jsps property
how
it is set and also the form bean.The property syntax I have used was for struts 1.0 ..But with struts 1.1 , you can have a better cleaner syntax using nested tags.But I have not used it...This works for 1.1 as well..


********************************************************

//Form Class

import java.util.ArrayList;

import java.util.List;

import org.apache.struts.action.ActionForm;

public class ExampleListForm extends ActionForm {

//A list of Emp beans

private List beanList = new ArrayList();

public List getBeanList(){

return beanList;

}

public void setBeanList(List list){

beanList = list;

}

//very imp.

//give indexed access to the beans

public Employee getEmployee(int index){

//very imp

//when a jsp is submited , then while auto populating the form,this will
ensure
that


// the form is populated properly.

while(index >= beanList.size()){

beanList.add(new Employee());

}

return (Employee)beanList.get(index);

}

public void setEmployee(int index,Employee emp){

beanList.set(index,emp);

}

}

***********************************************

Bean class

public class Employee {

private String name;

private String salary;

/**

* Returns the name.

* @return String

*/

public String getName() {

return name;

}

/**

* Returns the salary.

* @return String

*/

public String getSalary() {

return salary;

}

/**

* Sets the name.

* @param name The name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* Sets the salary.

* @param salary The salary to set

*/

public void setSalary(String salary) {

this.salary = salary;

}

}

****************************************

JSP

<%@ page language="java"%>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<html:html>

<html:form action="/" >

<logic:iterate id="bean" name="exampleListForm" property="beanList"
indexId="i"/>

<html:text name="exampleListForm"  property="<%=\"employee[\" + i
\"].name\"%>">

<html:text name="exampleListForm"  property="<%=\"employee[\" + i
\"].salary\"%>">

</logic:iterate>

</html:form>

</html:html>

Explanation:

See how the property is constructed.

<html:text name="exampleListForm"  property="<%=\"employee[\" + i
\"].name\"%>">

So this will result in a parameter name employee[i].name in the http
request.So
when the jsp is rendered, this will be interpreted as

getEmployee[i].getName().And when the jsp is submitted , the same will be
interpreted as getEmployee[i].setName().And this will result in auto
population
of data in the form as u can see.


So check the indexed properties on the form as well.(Especially the
getEmployee(index i) proeprty with while loop.)

Hope this helps.



regards,

Shirish.


-----Original Message----- From: Mark Lowe [mailto:[EMAIL PROTECTED] Sent: Monday, January 19, 2004 6:18 PM To: Struts Users Mailing List Subject: Re: getting data from form with nested beans


Dunno what that thread on lazy lists was but nesting beans should work fine, but you will need to scope any property that you want a dynamic size to session.

Shouldn't be anymore complicated than that.


On 19 Jan 2004, at 16:01, Martin Sturzenegger wrote:


hi,
concerning nested properties, i found so many questions, hints etc. in
the archive but nothing that really helped all the way...i'm still
confused (or even more now...)
i still don't understand how struts handles input from a form that
holds an iteration of nested beans.
is the following correct?
as soon as the user submits the form, the actionform-bean, holding the
nested beans with the user's changes, gets transmitted.
is it so, that before the action-class is called, the form-bean's
reset() method is called, and all nested beans are set to null by
default?
so do i have to override the reset() method?
what do i iterate over in the reset() method to get the user's inputs?
how do i limit the iteration?
does the validate() method gets called before the reset method?.

i've seen examples, where a dto-class is instanciated within the
reset() method.
is this the way to do it?
do i have to access these dto-beans in the action class?

could somebody give me a little example of a reset()-method, just to
show how the user's input can be gathered and then stored away?

and.. what are lazy lists? i wasn't able to find a definition....

sorry about it but

regards from an utterly confused martin




---------- Urspruengliche Nachricht ------------------------------ Von: <[EMAIL PROTECTED]> Datum: Mon, 19 Jan 2004 10:52:10 +0100

You ahve a fixed length or Empty list in the form.So when the auto
population tries to populate the nested bean for the list which is
empty/fixed size,you get this exception.
Try to use lazy list or search the archive for nested property
usage...There are many examples which will demonatrate how to use it.

HTH.
regards,
Shirish

-----Original Message-----
From: Martin Sturzenegger [mailto:[EMAIL PROTECTED]
Sent: Monday, January 19, 2004 10:46 AM
To: Struts Users Mailing List; [EMAIL PROTECTED]; Struts
Users
Mailing List
Subject: Re: Including one JSP in another


i try to receive user-input from a form using a list of nested beans. after hitting submit i get an ArrayIndexOutOfBoundsException can somebody give me a hint? many thanks martin




stacktrace:


java.lang.ArrayIndexOutOfBoundsException
java.lang.reflect.Array.get(Native Method)

org.apache.commons.beanutils.PropertyUtils.getIndexedProperty(Propert y
Utils.java:525)

org.apache.commons.beanutils.PropertyUtils.getIndexedProperty(Propert y
Utils.java:428)

org.apache.commons.beanutils.PropertyUtils.getNestedProperty(Property U
tils.java:770)

org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils. j
ava:801)
org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:
881)
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:808)
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:1252)

org.apache.struts.action.RequestProcessor.processPopulate(RequestProc e
ssor.java:821)

org.apache.struts.action.RequestProcessor.process(RequestProcessor.ja v
a:254)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:
1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java: 525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)






Confidentiality Notice


The information contained in this electronic message and any
attachments
to this message are intended
for the exclusive use of the addressee(s) and may contain
confidential
or privileged information. If
you are not the intended recipient, please notify the sender at
Wipro
or
[EMAIL PROTECTED] immediately
and destroy all copies of this message and any attachments.

----------------------------------------------------------------- -
---
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail:
[EMAIL PROTECTED]



Confidentiality Notice


The information contained in this electronic message and any
attachments
to this message are intended
for the exclusive use of the addressee(s) and may contain
confidential
or
privileged information. If
you are not the intended recipient, please notify the sender at
Wipro
or
[EMAIL PROTECTED] immediately
and destroy all copies of this message and any attachments.

----------------------------------------------------------------- -
---
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail:
[EMAIL PROTECTED]




------------------------------------------------------------------ -
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail:
[EMAIL PROTECTED]




------------------------------------------------------------------- -
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




-------------------------------------------------------------------- -
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]






--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]





--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to