struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From struts-u...@japache.org
Subject Referencing value objects via the ActionForm
Date Fri, 31 May 2002 19:09:57 GMT
I know this has come up before, but I couldn't find a succinct answer when I searched through
old notes. Here's my situation, which is probably typical of many Struts users.

Let's say you have a page used to display and edit employee data. We get to this page using
a path like: "/do/viewEditEmployee?id=55" 

Normally, the Action processing this request would fetch a value object for employee record
55 and leave it in (request) scope for the viewing JSP to reference. The ActionForm associated
with path "/do/viewEditEmployee" simply holds the id. 

The "display and edit employee data" form has a submit button on it titled "Save". Pressing
the button goes to path: "/do/saveEmployeeData". The ActionForm associated with this path
has properties for an employee, such as name, ssn, dob, etc.

When someone gets to the "display and edit employee data" page the first time, the form's
input fields should be initialized to the values for employee 55.

Here's my question: 

I could code an ActionForm containing setters and getters matching employee properties. However,
the ActionForm will end up having the same properties as the employee value object. It seems
redundant having to code such a similar class. (In other words, having to code a ActionForm
to be in sync with my employee value object's properties will be a coding and maintenance
issue.)

What I'd rather do is code the ActionForm to have a single property called "vo" for "value
object". The ActionForm#getVo() method would return a value object for the employee. I'm hoping
this will allow me to use Struts form tags with references like
<html:text property="vo.name"/> and <html:text property="vo.ssn"/> to reference
the getters and setters of the value object.

Now, remember that we originally got to the "display and edit employee" form with the path
"do/viewEditEmployee?id=55". I'd like the Action associated with that path to fetch the employee
value object and put it in the proper scope for use by the ensuing JSP. In other words, I'd
like to associate the value object with the ActionForm that will later on be used by the path
"/do/saveEmployee". My problem is, when I'm in the Action for "/do/viewEditEmployee" I don't
know (and it seems like I shouldn't know) the name of the ActionForm class that will be used
by the ensuing JSP. Where I should place the value object reference?

I'm assuming that <html:text property="vo.name"/> references properties of the ActionForm
accociated with the encompassing "<html:form ...>". That's not the ActionForm passed
into the initial Action (the one associated with "/do/viewEditEmployee").

What should I do?

(By the way, when I first considered doing this I was a little concerned by the form updating
the value object directly, instead of updating normal ActionForm properties, and having the
Action class move data to a value object as needed. The reasons I'd like to go with the "vo.name"
approach are: It looks like much less coding and maintenance. Furthermore, since the Action
class will always be responsible for fetching the value objects and will judge when and how
to apply their values to the server application, this approach seems to consistent with MVC
principles.)
--
Sent via jApache.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
View raw message