struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject ActionForm initialization
Date Mon, 01 Jul 2002 20:01:29 GMT
I have a simple ActionForm with one property: a reference to a value object. I'd like to initialize
that reference to point to the value object previously fetched by the Action. 

An Action typically takes form data passed to it in order to fetch (and perhaps update) an
object from the model. For example, in my case the ActionForm passed to the Action contains
an employee ID. My Action used the ID to fetch the corresponding employee value object and
associate it with the session under the key "EmployeeValueObject". 

After returning from the Action, Struts forwards the user to some viewing JSP. In my case,
it's a JSP that has a form used to view and edit the value object's fields. Since the form
has a single property, I use references like:
<html:text property="valueObject.firstName"/>. In other words, Struts will use this
to generate a call to "getValueObject().setFirstName".

Question 1: Where is the standard place to do this initialization? In other words, this implied
call to "getValueObject()" should return the value object fetched by the Action. 

(I've seen people do similar initializations in the Action, but I'm very uncomfortable with
the Action needing to "know" the view the user will be taken to. And even if you know where
the user is going, how would you know how many forms are referenced within the view?)

Question 2: What are you thoughts on doing something like the following:

Normally, it's not until one gets to an "html:form" within a JSP that Struts creates the ActionForm.
(The tag's doStartTag fetches the form and saves it with the pageContext. It removes it from
the pageContext in the doEndTag.)

Therefore, I think a logical place to assign the value object to the ActionForm would be within
the body of the "html:form" tag. I can imagine creating a simple custom tag used something
<mytag:setFormProperty property="valueObject" name="EmployeeValueObject"/>
The tag would use the ActionForm associated with the encompassing "html:form" and assign the
named EmployeeValueObject reference to the ActionForm's "valueObject" property. 

Modifying the value object directly from the form using statements like "valueObject.firstName"
saves me a lot of code over having to code and maintain an ActionForm with the same properties
as the value object. The form's associated Action still has responsibility for actually posting
the value object to the model.

Sent via

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message