struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nicolas De Loof" <nicolas.del...@cgey.com>
Subject Re: Array of Beans used in an ActionForm
Date Wed, 02 Apr 2003 10:13:52 GMT

For your indexed properties, use arrayForm(x).name instead of arrayForm[x].name

Try to use JSP tags instead of scriptlets, your JSP will be more readeable to other developers.

Nico.

----- Original Message -----
From: "Clauss, Arne" <Arne.Clauss@gedas.de>
To: "'Struts Users Mailing List'" <struts-user@jakarta.apache.org>
Sent: Wednesday, April 02, 2003 12:02 PM
Subject: AW: Array of Beans used in an ActionForm


Hello Nico!

Thanks for your help. I now have got indexed getter and setters. Showing the
data in the JSP works fine, but getting the data back formn the JSP in the
UpdateAction didn't work.

Please notice: I dont't want to handle an array of Strings in the
ActionForm, I would like to hadle an array of Beans ( with own getters and
setters) in the AtionForm.

Maybee it make sence to post some code here for a better understanding of
the problem.

This ist the ActionForm used in the JSP:

public class TestForm extends ActionForm {


/**
* Log4j Category for this class
*/
protected final static Category cat =
Category.getInstance(TestForm.class.getName());

public TestForm (int size)
{
arrayForm = new DataBean[size];
}

public TestForm ()
{
if (cat.isDebugEnabled())
{
cat.debug("Konstruktor Start");
}
int size=3;
arrayForm = new DataBean[size];
for (int i = 0; i<=(size-1);i++)
{
arrayForm[i]= new DataBean();
}
if (cat.isDebugEnabled())
{
cat.debug("Konstruktor Ende");
}
}

private DataBean[] arrayForm = null;

/**
* Gets the arrayForm
* @return Returns a ArrayForm[]
*/
public DataBean getArrayForm(int i) {
return arrayForm[i];
}
/**
* Sets the arrayForm
* @param arrayForm The arrayForm to set
*/
public void setArrayForm(int i, DataBean arrayForm) {
this.arrayForm[i] = arrayForm;
}

}

This is the Bean which is included in the array of the TestForm:

public class DataBean extends ActionForm {

/**
* Log4j Category for this class
*/
protected final static Category cat =
Category.getInstance(TestForm.class.getName());

/**
* Konstruktor
*/
public DataBean(String myName, int myId) {
this.setName(myName);
this.setID(myId);
}

/**
* Konstruktor
*/
public DataBean() {
this.setName("");
this.setID(Integer.MIN_VALUE);
}

private String name = "";
private int ID = Integer.MIN_VALUE;

/**
* Gets the name
* @return Returns a String
*/
public String getName() {
return name;
}
/**
* Sets the name
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* Gets the iD
* @return Returns a int
*/
public int getID() {
return ID;
}
/**
* Sets the iD
* @param iD The iD to set
*/
public void setID(int iD) {
ID = iD;
}
}

And last but not least my JSP code:

This works fine for showing the data, but not for updating:
<%
for (int i=0; i<=2; i++)
{
%>
<br><input type="text" name="testForm.arrayForm[<%= i%>].name" value="<%=
testForm.getArrayForm(i).getName()%>">

<%
}
%>

I have also tried this code int eh JSP. Than I get the following error: No
getter method for property testForm.arrayForm[0].name

<html:text property="testForm.arrayForm[0].name">
<html:text property="testForm.arrayForm[1].name">
<html:text property="testForm.arrayForm[2].name">

Any ideas?

Regards,

Arne


> -----Urspr√ľngliche Nachricht-----
> Von: Nicolas De Loof [mailto:nicolas.deloof@cgey.com]
> Gesendet: Mittwoch, 2. April 2003 10:39
> An: Struts Users Mailing List
> Betreff: Re: Array of Beans used in an ActionForm
>
>
> If you want to use an array for a property that your JSP uses
> as indexed (i.e. property="data(n)") you will have to define
> indexed getters and setters :
>
> public void setData(index i, String value) {
>     this.data[i] = value;
> }
>
> public String getData(index i) {
>     return this.data[i];
> }
>
> Struts will not create an instace of the array for you when
> it creates or populates the formBean. Your form-bean has to
> create the array in the "good" size before population occurs.
>
> Nico.
>
>
> > Hello!
> >
> > Is is possible to deal with an Array of Beans in an AtionForm?
> >
> > It should look like this:
> >
> > public class TestForm extends ActionForm {
> > private DataBean[] dataBeans = DataBean[size_of_array];
> >
> > getter and setter methods
> > }
> >
> > public class DataBean {
> > private String name="";
> > private int id = 0;
> >
> > getter and setter methods
> > }
> >
> > Searching the list I have found the following important facts:
> >
> > - It is important to have an default construktor in the ActionForm,
> > which creates an instance of the Array.
> >
> > Have somebody some code example to help me?
> >
> > Kind regards,
> >
> > Arne
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Mime
View raw message