struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p...@shareowner.com
Subject RE: Updating collections of information
Date Fri, 03 Jan 2003 21:32:54 GMT
Greg,

I have to correct my posting.

In my OrderEntryForm, there is an instance variable called lineItems.

In my reset method,

I first instantiate it using lineItems = getOrder(request).getLineItems().
Then I set the checkbox and let struts to populate the line items. You don't
have to keep a copy in session, instead, you can

- keep the size in session.
- in reset method, instantiates a new lineItems(size in session).

I keep two copies (one in request is enough for init values) - because I am
dumb - because I am lazy - because I want to send that much information to
the database - because...... Two-copy will not work for a large multi-row
form (mine is only few rows).

All struts need is how to create a new instance of your collection, I give
it two much.

Regards,
 
 
PQ
 
"This Guy Thinks He Knows Everything"
"This Guy Thinks He Knows What He Is Doing"

-----Original Message-----
From: Greg Bearth [mailto:greg_bearth@fourthgen.com] 
Sent: December 31, 2002 5:48 PM
To: 'Struts Users Mailing List'
Subject: RE: Updating collections of information

If your solution is working for you, it cannot be too dumb, and you are
ahead of me.  I agree with your guesses for struts behavior.  I added a line
in reset() to pull the collection bean out of the session and into the form.
This alleviates the NullPointerExeptions, but I am still missing the magic
that happens in the step where "Struts saves all the values into this
object".  The sample app prints out A B C even though I modified the values
to be D E F.  It may be time to concede defeat.

I appreciate your time and assistance.

-----Original Message-----
From: pqin@shareowner.com [mailto:pqin@shareowner.com]
Sent: Tuesday, December 31, 2002 3:57 PM
To: struts-user@jakarta.apache.org
Subject: RE: Updating collections of information


My solution is pretty dumb because it keeps two copies of ActionForm.

1. In orderEntryLoad action (before I load order entry page)

I retrieve line items from database and save two copies of them. One in the
session (see point 2), the other in the request so I have all the initial
values of this ActionForm.

2. In orderEntry action (after I click Submit button)

My guess for struts's behavior is

- Struts instantiates an instance of ActionForm.
- Struts calls reset method to create a new lineItems object. I override it
so as to make it an exact copy of the lineItems I stored in session.
- Struts saves all the values into this object.

Now I can save this ActionForm to database.




Regards,


PQ

"This Guy Thinks He Knows Everything"
"This Guy Thinks He Knows What He Is Doing"

-----Original Message-----
From: Greg Bearth [mailto:greg_bearth@fourthgen.com]
Sent: December 31, 2002 4:36 PM
To: 'Struts Users Mailing List'
Subject: RE: Updating collections of information

PQ, please bear with me because I think you are doing what I would like to
do.  I sincerely appreciate the hand-holding as this is my first Struts
project.

In your example, the page displays line items and it appears the user could
potentially update stringOrderAmount and mark items for removal.  When the
form is submitted, does your action class simply call getLineItems() and it
returns a collection of line items with the updated stringOrderAmount(s) and
markedForRemoval flags?  I assume so, and this would be analogous to what I
am attempting to do.  I simply have one extra CollectionBean layer because
in reality, this bean has other attributes besides the collection.

Can you elaborate on what getOrder(...) does?  It returns an iterator, so
obviously it has access to a collection, but what collection (lineItems
variable in your form class?) and how did it get populated?  This is the
heart of what I am missing because my form always returns null on the call
to getCollectionBean().  After adding a line, "this.collectionBean = new
CollectionBean()", to the CollectionForm constructor, then
getCollectionBean() returns an object, but calling getElements() on the
object returns null.  Does your form have session scope or request scope?
Is my problem related to putting the collection bean directly into the
session?  If so, how do I wrap it inside the form (the
PopulateCollectionAction doesn't know what Form is expected by the
subsequent UpdateCollectionAction)?

Also, you use a prefix of 'nested'.  To what tag library does this refer,
and is it important to the solution?

Sample Code I am using to test:
---------------------

public class PopulateCollectionAction extends Action {
	public ActionForward perform(ActionMapping actionMapping, ActionForm
actionForm, HttpServletRequest httpServletRequest, HttpServletResponse
httpServletResponse) throws IOException, ServletException {
		Collection c = new ArrayList();
		c.add(new ElementBean("A"));
		c.add(new ElementBean("B"));
		c.add(new ElementBean("C"));
		CollectionBean bean = new CollectionBean();
		bean.setElements(c);

httpServletRequest.getSession().setAttribute("theCollectionBean", bean);
		return(actionMapping.findForward("displayCollection"));
//displayCollection.jsp
	}
}

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

public class UpdateCollectionAction extends Action {
	public ActionForward perform(ActionMapping actionMapping, ActionForm
actionForm, HttpServletRequest httpServletRequest, HttpServletResponse
httpServletResponse) throws IOException, ServletException {
		CollectionForm f = (CollectionForm) actionForm;
		Collection c = f.getCollectionBean().getElements();
//NullPointerException
		for (Iterator i = c.iterator(); i.hasNext();)
//NullPointerException
		{
			ElementBean e = (ElementBean) i.next();
			System.out.println(e.getValue());  //This should
print D E F.
		}
		return(actionMapping.findForward("start"));  //index.jsp
	}

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

public class CollectionForm extends ActionForm {
	CollectionBean collectionBean;

	public CollectionForm() {
		this.collectionBean = new CollectionBean();
	}

	public CollectionBean getCollectionBean() {
		return collectionBean;
	}

	public void setCollectionBean(CollectionBean collectionBean) {
		this.collectionBean = collectionBean;
	}
}

---------------------
//index.jsp

<html:html>
<body>
	<html:form action="start.do" method="post">
		<center><input type="submit" value="Submit" /></center>
	</html:form>

</body>
</html:html>

---------------------
//displayCollection.jsp

<html:html>
<body>
	<html:form action="update.do" method="post" focus="value">
		<logic:iterate name="theCollectionBean" id="element"
property="elements"
type="com.fourthgen.sample.ElementBean">
			<html:text name="element" property="value"/>
			</br>
		</logic:iterate>
		<br/>
		<center><input type="submit" value="Submit" /></center>
	</html:form>

</body>
</html:html>

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

-----Original Message-----
From: pqin@shareowner.com [mailto:pqin@shareowner.com]
Sent: Tuesday, December 31, 2002 12:39 PM
To: struts-user@jakarta.apache.org
Subject: RE: Updating collections of information

On the page:

<nested:iterate property="lineItems" id="orderEntryLineItemForm">
    <TR>
        <TD nowrap><nested:hidden property="assetID"/><nested:write
property="assetName"/></TD>
        <TD align=center><nested:text property="stringOrderAmount"/></TD>
        <TD align=center>
            <nested:select property="nextTransactionDate">
                <nested:options property="nextTransactionDateValues"
labelProperty="nextTransactionDateLabels"/>
            </nested:select>
        </TD>
            <TD align=center><nested:checkbox property="markedForRemoval"
value="true"/></TD>
    </TR>
</nested:iterate>


public class OrderEntryForm extends ActionForm {

    /**
     * list of line items.
     */
    private Collection lineItems ;
    /**
     * next withdraw from line item.
     */
    private String nextWithdrawalDate ;
    /**
     * instantiates an empty collection of line items.
     */
    public OrderEntryForm() {
        lineItems = new ArrayList() ;
    }

    /**
     * sets line items.
     *
     * @param   c   collection of line items.
     */
    public void setLineItems(Collection c)
    {
        lineItems.addAll(c) ;
    }

    /**
     * returns line items.
     *
     * @return  lineItems   line items.
     */
    public Collection getLineItems()
    {
        return lineItems ;
    }

    /**
     * add one line item to order entry form.
     *
     * @param   o   line item.
     */
    public void addLineItem(OrderEntryLineItemForm o)
    {
        lineItems.add(o) ;
    }

    /**
     * I have to initialize the form otherwise got
IndexOutOfBoundsException. I also need to reset markedForRemoval.
     *
     * @param   mapping action mapping, no use.
     * @param   request request to find the numLineItems in session.
     */
    public void reset(ActionMapping mapping, HttpServletRequest request)
    {
        Iterator iterator = getOrder(......) ;
        while (iterator.hasNext())
        {
            ((OrderEntryLineItemForm)
iterator.next()).setMarkedForRemoval(Boolean.FALSE) ;
        }
    }


}

Regards,


PQ

"This Guy Thinks He Knows Everything"
"This Guy Thinks He Knows What He Is Doing"

-----Original Message-----
From: Greg Bearth [mailto:greg_bearth@fourthgen.com]
Sent: December 31, 2002 1:36 PM
To: 'Struts Users Mailing List'
Subject: RE: Updating collections of information

I'm not sure I understand how I can override reset to solve this issue.  Can
you elaborate?  Do I have to create an empty collection or something?

To be clear, this is not the situation where checkboxes must be initialized
to false in the reset method.  I've already fallen for that one.  My issue
could simply be a collection of three beans which have String property
values "A", "B", "C" respectively (of course in real life the size of the
collection is not predetermined and the bean would have more properties).
These values are displayed using the logic:iterate tag and the html:text tag
(sample, untested code below).  My form bean would have getBeanCollection()
and setBeanCollection(Collection c) methods. Each bean in the collection has
a getString() and setString(String s) methods.  Assuming the user changes
the three values to "D", "E", "F", how should my action class retrieve the
new values?  I'd like to call getBeanCollection() on the form bean and have
it return a collection of three beans in which the getString method returns
"D", "E", "F" respectively, but so far getBeanCollection() returns null.

<logic:iterate name="selectedBean" id="element" property="beanCollection"
type="xxx.yyy.ElementBean">
  <tr>
    <td align="left"><html:text name="element" property="string"/></td>
  </tr>
</logic:iterate>

I appreciate your time and patience.

-----Original Message-----
From: pqin@shareowner.com [mailto:pqin@shareowner.com]
Sent: Tuesday, December 31, 2002 10:20 AM
To: struts-user@jakarta.apache.org
Subject: RE: Updating collections of information


This is a classic question. You have to override reset in your form to
correctly populate all the elements.

Regards,


PQ

"This Guy Thinks He Knows Everything"
"This Guy Thinks He Knows What He Is Doing"


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


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


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

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