You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Cruz, Edward J." <e....@radium.ncsc.mil> on 2003/04/01 19:39:51 UTC

How does Struts handle A list attribute in an AcitonForm

Hi All, 
	
	I have a question regarding an ActionForm.  

I have an ActionForm 

public class OrderForm extends ActionForm {
	String orderID;
	Header header;
	List items;
	
}

I have looked at the struts-exercise-taglib example and see how to display
the items list via the logic-iterate tag.  My question is if I use this form
for a multi-page form where header information is filled in on one page and
submitted.  This forwards to a page which displays all the header
information and a form which allows the user to add an item.  The form to
add an item has several fields which are stored in an item which I need to
store in the list as shown above.  

Do I need to have a single item which I access through the form as I access
the header then in an appropriate place add that item to the list for use
later in a display or transfer to the business logic?  

For instance, 

public class OrderForm extends ActionForm {
	String orderID;
	Header header;
	Item tempItem;
	List items;

}

Then in my form to add an item to the order  I have the appropriate html
text boxes... etc.  They will be access through the tempItem.id method.
Then once the item passes validation I can add it to the list of items I
will later display to the user?

Jamie Cruz
Software Engineering 
e.cruz@radium.ncsc.mil 
(301)688-1430 




Re: How does Struts handle A list attribute in an AcitonForm

Posted by Ian Hunter <ih...@hunterweb.net>.
What I've done in the past is something like this -- first, I try to always
use DynaActionForms.  In my struts-config file I'd declare something like

    <form-bean name="familyForm"
type="org.apache.struts.validator.DynaValidatorForm">
      <form-property name="address1" type="java.lang.String"/>
      <form-property name="address2" type="java.lang.String"/>
      <form-property name="city" type="java.lang.String"/>
      <form-property name="state" type="java.lang.String"/>
      <form-property name="zip" type="java.lang.String"/>
      <form-property name="phoneHome" type="java.lang.String"/>
      <form-property name="family" type="PersonBean[]"/>
    </form-bean>

elsewhere I have PersonBean defined to have firstName, lastName, etc. as
strings;

Then in my JSP it looks like:

<html:form action="addfamily">
<table>  <!-- "header" info in your case -->
<tr><td>Address:</td><td><html:text property="address1"
size="30"/></td></tr>
<tr><td>&nbsp;</td><td><html:text property="address2" size="30"/></td></tr>
<tr><td>&nbsp;</td><td><html:text property="city" size="25"/>, <html:text
property="state" size="2" maxlength="2"/>  <html:text property="zip"
size="8" maxlength="10"/></td></tr>
<tr><td>Home Phone: <html:text property="phoneHome" size="10"/></td></tr>
</table>
<table>
<tr><th>First Name</th><th>Last Name</th><th>Cell</th><th>Work</th></tr>
<logic:iterate id="family" name="familyForm" property="family">
<tr>
<td align="center"><html:text name="family" property="firstName"
indexed="true"/></td>
<td align="center"><html:text name="family" property="lastName"
indexed="true"/></td>
<td align="center"><html:text name="family" property="cellPhone" size="10"
indexed="true"/></td>
<td align="center"><html:text name="family" property="workPhone" size="10"
indexed="true"/></td>
</tr>
</logic:iterate>
</table>
<html:submit value="Add Family"/>
</html:form>

And in the corresponding Action it looks like

        try {
            HouseholdView hv = DataStore.getNewHouseholdView();
            hv.setAddress1 (dynaForm.get("address1").toString());
            hv.setAddress2 (dynaForm.get("address2").toString());
            hv.setCity (dynaForm.get("city").toString());
            hv.setState (dynaForm.get("state").toString());
            hv.setZip (dynaForm.get("zip").toString());
            hv.setPhoneHome (dynaForm.get("phoneHome").toString());
            Integer fkHousehold = hv.add();

            for (int i = 0; i < Constants.MAXFAMILYMEMBERSFORADD; i++) {
                if (!((PersonBean)dynaForm.get("family",
i)).getFirstName().equals("")) { // is this entry filled in?
                    MemberView mv = DataStore.getNewMemberView();
                    mv.setFirstName (((PersonBean)dynaForm.get("family",
i)).getFirstName());
                    mv.setLastName (((PersonBean)dynaForm.get("family",
i)).getLastName());
                    mv.setPhoneCell(((PersonBean)dynaForm.get("family",
i)).getCellPhone());
                    mv.setPhoneWork (((PersonBean)dynaForm.get("family",
i)).getWorkPhone());
                    mv.setFkHousehold (fkHousehold);
                    Integer fkMember = DataStore.addMemberView (mv);
                }
            }
            servlet.log(user.getName() + " added family");

Does that help?

----- Original Message -----
From: "Cruz, Edward J." <e....@radium.ncsc.mil>
To: "Struts-User List" <st...@jakarta.apache.org>
Sent: Tuesday, April 01, 2003 12:39 PM
Subject: How does Struts handle A list attribute in an AcitonForm


> Hi All,
>
> I have a question regarding an ActionForm.
>
> I have an ActionForm
>
> public class OrderForm extends ActionForm {
> String orderID;
> Header header;
> List items;
>
> }
>
> I have looked at the struts-exercise-taglib example and see how to display
> the items list via the logic-iterate tag.  My question is if I use this
form
> for a multi-page form where header information is filled in on one page
and
> submitted.  This forwards to a page which displays all the header
> information and a form which allows the user to add an item.  The form to
> add an item has several fields which are stored in an item which I need to
> store in the list as shown above.
>
> Do I need to have a single item which I access through the form as I
access
> the header then in an appropriate place add that item to the list for use
> later in a display or transfer to the business logic?
>
> For instance,
>
> public class OrderForm extends ActionForm {
> String orderID;
> Header header;
> Item tempItem;
> List items;
>
> }
>
> Then in my form to add an item to the order  I have the appropriate html
> text boxes... etc.  They will be access through the tempItem.id method.
> Then once the item passes validation I can add it to the list of items I
> will later display to the user?
>
> Jamie Cruz
> Software Engineering
> e.cruz@radium.ncsc.mil
> (301)688-1430
>
>
>
>


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


> ---------------------------------------------------------------------
> 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