• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Advice on persisting complex state across multiple server trips

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the session.
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the feedback. I'll give the Session a try first.

Cheers,
Mike.
 
Michael Thiessen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic