?
Solved

Basic Struts work flow question

Posted on 2007-11-15
3
Medium Priority
?
3,412 Views
Last Modified: 2013-11-24
Hi there,

I am new to Struts (1.2.x). 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!

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.

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?)

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

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)

4. Create a db connection by using Singleton pattern

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

6. Create 3 javabeans to map three db tables

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

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

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?

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?

Thanks a lot.
0
Comment
Question by:tiger0516
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
boonleng earned 1500 total points
ID: 20296021
> 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?)
There's no right or wrong doing in this way, but I prefer putting all actions for the same group in 1 class using DispatchAction, for easy manage.

Your flow seem fine. However I would suggest that you place all the database activity in DAO. To clearly define each layer.  JSP <-> Struts Action <-> DAO <- Datasource

DAO flow
---------------
   1. get connection from datasource
   2. execute SQL
   3. populate resultset to javabean.
   4. close connection on finally
   5. return javabean.

ps: suggest reading http://java.sun.com/blueprints/corej2eepatterns/Patterns/index.html You don't need to implement the full pattern, but will give you an idea how each component works.
0
 
LVL 1

Author Comment

by:tiger0516
ID: 20296924
Do you have some working example code?

Thanks
0
 
LVL 14

Expert Comment

by:boonleng
ID: 20297755
You can find the sample code for DAO on Java Pet Store.
https://blueprints.dev.java.net/petstore/
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
A solution for Fortify Path Manipulation.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question