This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Struts and the fly likes Advice on persisting complex state across multiple server trips Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Advice on persisting complex state across multiple server trips" Watch "Advice on persisting complex state across multiple server trips" New topic
Author

Advice on persisting complex state across multiple server trips

Michael Thiessen
Greenhorn

Joined: Feb 26, 2005
Posts: 5
Hello,

I'm building an editable parent / child JSP page. I have a single object derived from ActionForm backing the JSP page. The form object contains an ArrayList of JavaBean objects - one for each child row. The ArrayList is populated from a database on initial display of the page. I'm using the <logic:iterate> tag etc. to display the child info. No problems so far.

The page allows the user to add new child objects to the parent object (e.g. these will wind up being new rows in a database). What I'm trying to do is simulate a single save (ACID transaction) for the whole page. I want to do trips to the server whenever the user adds a new child row. I'd like to add a child object to the form's ArrayList to keep the population logic in the JSP page simple. I *don't* want to save the new object out to the database right away.

The problem is the ActionForm doesn't retain any "object" state. On the post, Struts calls getChildObject(int index) on the ActionForm. The backing ArrayList is null at this point. In the past, I've just repopulated the ArrayList by going back to the database "just in time." In this case, not so nice since some of the objects aren't represented in the database.

I'm sure many people have run into this as it would be the same issue for the classic "shopping cart" example.

Options I can think of are:

- storing intermediate, temporary rows in the database
- using the Session
- some Struts feature to serialize object state and send it on the request

None of these are particularly appealing.

Any help would be greatly appreciated.

Thanks,
Mike.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

You really shouldn't hit the DB every time. It can get expensive.

Session scope is not as evil as you might think.

The other option is to load up your JSP with hidden fields.


A good workman is known by his tools.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Use the session.
boyet silverio
Ranch Hand

Joined: Aug 28, 2002
Posts: 173
Hello Mike,

You can also look at the "caching" capabilities of the Apache iBatis Data (SQL) Mapper. It has the capability of caching database-loaded objects which the iBatis framework can retrieve in case it encounters the same "select" statement again.
[ February 26, 2005: Message edited by: boyet silverio ]
Michael Thiessen
Greenhorn

Joined: Feb 26, 2005
Posts: 5
Thanks for the feedback. I'll give the Session a try first.

Cheers,
Mike.
Michael Thiessen
Greenhorn

Joined: Feb 26, 2005
Posts: 5
A quick follow-up question.

This JSP:

<logic:iterate id="item" name="editForm" property="items">
<tr> <td nowrap align="center"><html:checkbox name="item" property="myProp" indexed="true" /></td>
</tr>
</logic:iterate>

generates a call to this method of my ActionForm-derived class:

public MyItem getItem(int i)
{
return (MyItem)items.get(i);
}

The items ArrayList is empty. I need to get it out of the Session, but I know of no way to get a handle to the Session (or the Request) from this point. I know it's accessible from the ActionForm's validate or reset methods. Can I somehow get it via a servlet reference?

Thanks in advance,
Mike.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Populate your ActionForm in the Action, so that when the JSP accesses it, it already has all the data it needs. If it helps, you can think of your ActionForms as DTOs between your control layer and your presentation layer. Your Action calling your view of the edit form might look like this (assume your action-mapping specifies that it uses editForm):



If your domain object was in session, you could have just as easily gotten it from session instead of using the facade. It doesn't really matter. The main points to illustrate are that your domain object and your ActionForm are completely separate, and that your ActionForm is pre-populated in the Action before the JSP gets its hands on it.
Michael Thiessen
Greenhorn

Joined: Feb 26, 2005
Posts: 5
I should have been more specific. I am pre-populating the form from the action. The calls in the form that I was referring to in my last post are occuring after the submit, but before the form's validate method has been called ... or the call into the action object.

Thanks,
Mike.
 
wood burning stoves
 
subject: Advice on persisting complex state across multiple server trips