struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Aaron Ravenberg" <aravenb...@benefitsxml.com>
Subject RE: Struts Design Question
Date Wed, 08 Aug 2001 14:00:59 GMT
Yes you can do the add/edit all on the same form.  If have a database
generated code or key you can put that in a hidden field.  When in the
action examine the form or request obj to see if that key is there, if it is
you have a modify, if not add the record.

Have you page that lists the songs pass a the code/key as a parameter to the
SongForm.jsp. Then you can use the jsp:useBean as follows:

<jsp:useBean id="SongForm" class="com.mypackage.SongForm" scope="request" >
 <jsp:setProperty name="SongForm" property="songKey" param="songKey" />
</jsp:useBean>

Then to pre populate the form you can make a DataBase call in your form
setSongKey() method and use the result to set the other values in the form.

For a delete you can have a second form on the same page (with its own
submit button) that has the songKey set into the SongDeleteForm and the
submit is mapped to a DeleteAction that just uses the key to delete the
record.

I am new at the struts stuff myself, so these may not be the best ways to
accomplish what you are trying to do, but I know it works.  So anyone else
please feel free to critique.

Aaron


-----Original Message-----
From: Shriver, Ryan [mailto:Ryan.Shriver@whitlockebs.com]
Sent: Wednesday, August 08, 2001 9:30 AM
To: 'struts-user@jakarta.apache.org'
Subject: Struts Design Question


Hello all,
I downloaded Struts last week and have enjoyed playing with it. I've come
upon a design problem and I was hoping some folks on this list could help
solve it. I've had no luck finding an answer in the mail archives.

I want to maintain a list of songs. Through a web interface, I want to add a
new song, edit information about an existing song (composer, artist, etc)
and also delete a song. Under the covers this will do some sort of database
insert, update and delete, respectively.

The forms for adding a new song and editing an existing song are practically
identical. They contain the same field names with the difference being edit
pre-populates the fields with the existing values of a song whereas the
fields are blank for add. Question #1: Can one ActionForm suffice for both
add and edit form pages? My gut says yes, but I want to make sure.

Actions. In the simplest scenario, I could create separate actions for all
operations. I'd have AddSongAction, EditSongAction and DeleteSongAction
classes each with their own logic in perform(). This would work, but there's
something that bothers me about having three different classes performing
operations on one entity. It seems like overkill.

DispatchActions. I saw the post about using DispatchAction to have multiple
methods in a single Action class. I really like this approach, as I could
have insert, update and delete methods in my Action class. However, the
input="" attribute in the action mapping definition (in struts-config.xml)
is shared for all of them. So if I have separate addsong.jsp and
editsong.jsp form pages sharing the same Action, and a validation error
occurs, Struts can only return to one (whatever input="" is). So this won't
work.

Question #2: Is there a happy medium between these two approaches?

One idea that I haven't worked through is using one songform.jsp page for
add/edit/delete. Calling songform.jsp?method=add would produce a blank page
for entering a new song. Songform.jsp?method=edit&id=1234 would populate the
form with values from a song whose id = 1234. From here one could edit or
delete the selected song. In Struts I'd use the DispatchAction to pass all
add/edit/delete requests to one Action class. Because all were using the
same form, the input="" attribute problem above would be avoided.

Question #3: Will this approach work? Can anyone offer a better solution for
this problem?

Thanks for the feedback.

-ryan


Mime
View raw message