struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Heligon Sandra <sandra.heli...@thomson.net>
Subject RE: How define a DynaActionForm to display a collection ?
Date Wed, 19 Mar 2003 09:48:48 GMT
Thanks a lot for your example, it is exactly what I wish to do. 
The JSP code with the tag logic:iterate helped me much.
I still have nevertheless a problem. 

When I run the application I have the error:
[ServletException in: /pages/MyPage.jsp]I do not know how to iterate over
'class java.lang.String'.' 

It is probably the code of the action that populates the form
that is bad.
For your example I will write:

public ActionForward executeAction(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception 
{
    Collection beans = new ArrayList();
    ItemBean bean1 = new ItemBean();
    beans.add(bean1);
    bean1.setId(1);
    bean1.setName("Toto");

    ItemBean bean2 = new ItemBean();
    beans.add(bean2);
    bean1.setId(2);
    bean1.setName("Tata");
   
    // Populate the form from the business data values
    BeanUtils.copyProperties(form,beans);
} 

The action that populates the form is not the action view2Submit.do.
The action view2Submit.do process the form, isn't it ?

Thanks a lot in advance for your help.
Sandra


----------------------------------------------------------------------------

As of February 12th, 2003 Thomson unifies its email addresses on a worldwide
basis. 
Please note my new email address: sandra.heligon@thomson.net 

http://www.thomson.net/ 

----Original Message-----
From: harish krishnaswamy [mailto:whonts@yahoo.com]
Sent: 18 March 2003 19:30
To: Struts Users Mailing List
Subject: RE: How define a DynaActionForm to display a collection ?



After spending quite sometime on the collection-backed
form properties solution, I figured its worth
mentioning the solution I dug out. I have to say that
the documentation on this one is pretty sparse. Ok
here we go...

I have a jsp that needs to accept an order for items.
This jsp has single-occurence fields like customer
name and address and multi-occurence fields like item
id, item name and item price. The later is the focus
of this message. I have created a plain java bean for
the multi-occurence fileds called ItemBean.

public class ItemBean {
  public ItemBean() {
  }
  public void setId(int id) {
    this.id = id;
  }
  public int getId() {
    return this.itemId;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getName() {
    return this.name;
  }
  public void setPrice(double price) {
    this.price = price;
  }
  public double getPrice() {
    return this.price;
  }
}

I have created a DynaActionForm bean as follows:

<form-bean name="orderForm" dynamic="true"   
  type="org.apache.struts.action.DynaActionForm">
  <form-property name="name" type="java.lang.String"/>
  <form-property name="address" 
    type="java.lang.String"/>
  <form-property name="itemBeans" 
    type="java.util.ArrayList"/>
  <form-property name="event"
type="java.lang.String"/>
</form-bean>

And I have a jsp like shown below:

<html:form action="view2Submit.do">

Name: <html:text property="name"/>&nbsp;
Address: <html:text property="address"/>

<table>

<tr>
	<td>Id:</td>
	<td>Name:</td>
	<td>Price:</td>
</tr>

<logic:iterate id="itemBeans" indexId="idx" 
  name="orderForm" property="itemBeans" 
  type="com.xyz.ItemBean">

<tr>
<td><html:text name="itemBeans" property="id" 
  indexed="true"/></td>
<td><html:text name="itemBeans" property="name" 
  size="50" indexed="true"/></td>
<td><html:text name="itemBeans" property="price" 
  indexed="true"/></td>
</tr>

</logic:iterate>

<tr>
<td><html:submit value="Save" 
  onclick="setSubmitEvent('saveActivity');"/>
</td>
<td><html:submit value="Add" 
  onclick="setSubmitEvent('addActivityProperty');"/>
</td>
</tr>
</table>

<html:hidden property="event"/>

</html:form>

Notice that the values of "id" and "property"
attributes of the <logic:iterate> tag are the same.
This is what makes the name of the html text fields to
evaluate to the right format, which is:
itemBeans[n].id for the id field. This "itemBeans" is
the name of the indexed-property in the form bean. The
"event" field is used by DispatchAction.

Hope this was helpful to atleast someone.

-Harish

 Heligon Sandra <sandra.heligon@thomson.net> wrote:
I have an other question if I use a sub-class of DynaValidatorForm 
and define the form-bean in the struts-config file with:

dynamic="true" type="foo.bar.YourDyanForm">




How do I write my JSP page ?

type="foo.bar.YourDyanForm">











Is this the good manners to make? 


----------------------------------------------------------------------------

As of February 12th, 2003 Thomson unifies its email addresses on a worldwide
basis. 
Please note my new email address: sandra.heligon@thomson.net 

http://www.thomson.net/ 

----Original Message-----
From: Heligon Sandra 
Sent: 18 March 2003 13:43
To: 'Struts Users Mailing List'
Subject: RE: How define a DynaActionForm to display a collection ?
Importance: High



If I define a sub-class of DynaValidatorForm I just have to add
two methods to access to the list parameter, isn't it ?

public final class MyDynaForm extends DynaValidatorForm {

private ArrayList list;

public MyDynaForm () 
{ 
list = new ArrayList();
ConfProp props = new ConfProp();
Vector v = props.getMeterHosts();
for (int i=0; i list.add(v.elementAt(i));
setList(list);
}

public ArrayList getList() {
return list;
}

public void setList(ArrayList list) {
this.list = list;
}
}

I will wish to be certain that it is the best way to display a
collection in DynaValidatorForm 
because I have a doubt in a message of the archives I read: 
The only reason you would ever need to create a real class that extends
DynaActionForm or DynaValidatorForm would be if you need a custom
reset()
or validate() method. 

What do you think about it? 

-----Original Message-----
From: Rick Reumann [mailto:r@reumann.net]
Sent: 11 March 2003 18:18
To: Struts Users Mailing List
Subject: Re: How define a DynaActionForm to display a collection ?


On Tue, 11 Mar 2003 17:36:09 +0100
Heligon Sandra wrote:

> Has someone an example of struts-config.xml file and
> a such SetUpAction with DynaActionForm and Collection ?

In a form-bean declarations, where YourDyanForm is a subclass of a
DynaActionForm(DynaValidatorForm):

dynamic="true" type="foo.bar.YourDyanForm">

{ snip }

{ snip }



Then in you Action class...


execute(..) {
ArrayList someList = someBusinessClass.getMeListOfObjectsIneed();
formBean.set("myCollection" someList );

//forward to jsp page
}


The reason you'll want to subclass DynaActionForm is if you do some
validation you probably will have to override the reset method. 

-- 
Rick Reumann

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



---------------------------------
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message