Re: Basic Struts work flow question

2007-11-15 Thread Gary Affonso

Minghui Yu wrote:

Hi there,

I am new to Struts (version 1.2.7). I am developing a small app to
learn Struts. Could
you please give me some suggestions?


Respectfully, my first suggestion would be to use Struts 2.  If you're 
just getting started and you need to climb a learning-curve anyway, why 
not climb the curve with the latest release?


The differences between S1 and S2 are significant.  You don't want to 
learn a bunch of stuff in S1 that just doesn't apply in S2.


Unless, of course, your motivation for learning is to help with existing 
S1 projects.  In that case you've gotta learn S1.


But if you're learning so you can code *new* webapps, use S2.

- Gary

P.S. And learn Spring, too. :-)

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Basic Struts work flow question

2007-11-15 Thread Givler, Eric
Responses inline...

-Original Message- 
From: Minghui Yu [mailto:[EMAIL PROTECTED] 
Sent: Thu 11/15/2007 7:16 PM 
To: Struts Users Mailing List 
Cc: 
Subject: Basic Struts work flow question



Hi there,

I am new to Struts (version 1.2.7). I am developing a small app to
learn Struts. Could you please give me some suggestions? Many thanks!!

Scenario:

A very small web app. I have 3 tables: student, course, and student-
course. Student and course are 1:N relationship. student-course is a
bridge table, which takes user_id (from Student) and course_id (from
Course) as FKs. Anonymous users can view, registered users can edit,
admin can add,delete and edit. That's it!

EGDecide how you separate anonymous pages vs registered user pages.  
EGAre  you protecting them via a role and constraints in web.xml?

As I am brand new to Struts, I do not want to use Hibernate or another
framework. I just want basic JDBC for db work.

EGProbably a bad idea.  At the very least, look at iBATIS.  It will 
save you time.
EGLook at the Struts CRUD demo at www.learntechnology.net

My ideas:

1. For each action group (instead of individual action), create an
Action class. For example, UserAction (add,edit,delete users) instead
of UserAddAction, UserDeleteAction, UserEditAction. Is this a good
practice or not? Or shall I create an Action class for each individual
action (will that bring a lot repetitive codes?)

EGI'd stick with one action, and make use of a flavor of dispatch 
action, or
EGuse EventDispatchAction.

2. Like 1, for each action group, create an ActionForm class, like
UserActionForm

EGYou may not need to if you look into FormDef or use DynaActionForms.

3. Create a JSP page for each individual action (not group) as the
input/entry page. For example, addstudent.jsp, deletestudent.jsp,
editstudent,jsp. Each JSP page use corresponding ActionForm bean. For
example, the above three JSP pages all use UserActionForm. However,
different JSP pages use different element of the ActionForm bean. For
example, user_id is not used in addstudent.jsp but is used in
deletestudent.jsp, as an id is required to delete a record but not
required to add one (I set id, the PK for Student table as auto-
increasement)

EGJust don't ever let anyone see the id#.  It should never appear as a 
hidden field.

4. Create a db connection by using Singleton pattern

EGIbatis will handle all the connection stuff for you, just create the 
singleton 
EGfor getting the SQLMAP Client instance.

5. For each Action class, depending on different inputs, prepare sql
statement and pass the statement to the db connection created in 4.
Get a RecordSet as returned value

EGYou should not be passing SQL statements around at all.
EGRead about DAOs, or just create a service/business related class that
EGperforms these interactions with the database rather than coding them
EGdirectly into the actions.  An action need not establish a 
connection at all.

6. Create 3 javabeans to map three db tables

EGSounds ok.

7. Construct javabean objects based on the RecordSet returned from 5.
For example, select * from
Student will construct a SetStudent

EGThese can be properties of the forms, so you need not have 
additional objects.
EGIbatis can automatically handle returning a resultset as an 
ArrayList.
EGYou can also look into the RowSetDynaBean (or disconnected rowset).

8. Use a loop in JSP page to get properties from the bean

EGUse Struts Logic or JSTL c:foreach.

That's my basic view on Struts. I have no idea if my view is okay or
bad. Could you please give me some help?

EG That's all I have for now.  It sounds like you are starting ok.

Additionally, I am not sure how to close the db connection,.as I need
to call the db many times. Within the Singleton patten db connection
class, as I need to return the RecordSet, how shall I close the db?
What shall be the order/sequence to do that?

EGThat's why you use a framework.  The iBATIS tutorial (quickstart) is 
only
EGabout 10 pages, so it's well worth looking into.  If you know SQL, 
which
EGyou'd have to if you were going to use JDBC, then look at iBATIS.
EGThe above is opinion only - take it for what it's worth, and good 
luck.