I. Why using Struts?
Struts is a framework implemented according to the
Model-View-Controller (MVC) design pattern. MVC
architecture divides applications components into three
three different and independent categories: Model, View
and Controller.
Model:
Model, generally represented as a set of JavaBeans,
provides data from the database and saves it into the data store.
All the business logic are implemented in the Model. It checkes
input data before saving into the database. It contains Data access
Like JDBC and EJB, and third-parties packages (like Hibernate),
Data validation and Data saving logic.
View:
View takes the entered user data and dispaches it to
the controller. it contains HTML, JSPs, Custom Tag Libraries
and Resources files.
Controller:
Controller acts as a bridge between the applications'Model
and web View.
Controller receives a request from a client via View and
invokes an action class with the model. The fram work provides
an ActionForm class to help transfer data between Model and View.
It executes then an appropriate business logic from the Model
an produce an output to the user. It contains: ActionServlet,
Action, ActionForm and struts-config.xml.
We will use the Struts framework for the follwing
example. It is about the set of the rare gases that are:
Helium, Neon, Argon, Krypton Xenon, and Radon. Each of the atoms
has its proprties: name, Symbol, charge and mass. We will add the
last one to check the availability of the gas in the set.
In order to build a project, we need Eclipse, MyEclipse, Jboss and
its installer; and Java JDK.
They are free on the internet, except JBoss that is on the test
for a mounth. It is not necessary to have the newest software to
build the project. Eclipse 3.0, MyEclipse 3.8 and JBoss 3.0
do correctly the job:
MyEclipse Version: 3.8.4, File size: 58.18 MB from:
archive.eclipse.org
JBoss 3.0 from:
sourceforge.net
II. Creating a struts project:
Step 1:
File > New > Project...
Select Web Project
> Next
> project Name: NobleGases
> Finish
Step 2.
We have then a J2EE project. We will add the struts
capabilities:
> Right click on the project: NobleGases
> Choose MyEclipse
> add struts Capabilities.
NobleGases in /Webroot
Base package: net.atoms.struts
> Finish
Step 3.
Create a default entry page: greeting
> Right click on the Folder WebRoot
> Choose New >
> JSP
File name :index.jsp
Template touse : Standard JSP using Strusts 1.1
Override in the case its demanded.
> Finish
4.
Choose Webroot > WEB-INF
> Click on index.jsp
> Add
<logic:forward name="greeting" /> after the
last <%@ taglib ... %>
( just write <logic and Ctrl-Space and choose forward)
The entry page is:
greetings
III. Creating Global Action Forward and Action Mappings
Step 1:
> Choose Webroot > Right-Click > New Folder
> Tape jsp
> Choose the directory /WebRoot/jsp
Create a second index.jsp
> Choose jsp > Right-Click > New > file
> Save it as:
index.jsp
Step 2:
> Webroot > WEB_INF > Open the filestruts-config.xml
On the bottom click on the fast view > Choose outline
> click on ouline > Select action mapping
> New Action
> Use case: default
> Click on chech-box: Forward
> Forward path:/jsp/index.jsp
(The Forward Path is /jsp/index.jsp, for geetings page).
> Finish
Step 3:
Always in the filestruts-config.xml file:
> Choose global-forwards
> Right-click > New Forward
Forward name: greetings
Forward path: /default.do
> Click on redirect
> Finish
The content of the filestruts-config.xml file is:
Struts-config.xml
IV. Creating an object class: Atom
Step 1:
> Select the project: NobleGases
> Right-click > New > Class
Package: net.atoms
Name: Atom
> Finish
The Class Atom represents the six noble gas atoms
with the properties name, symbol, charge, mass, and presence
Step 2:
> Right-click on the class name (here: Atom)
> Source > Choose: Generate Getters and Setters
> Choose all
Inset point: Last method
> Click on the check-box Generate method comment.
Step 3:
> Right-click again on the class NobleGases
> Source > Choose: Generate Constructors using Fiels
> Choose all (leave the methods collapsed)
Insertion point: First method
> Erase supper();
> Write the empty constructor: public NobleGases(){}
> Save
Here is the related bean:
JavaBean class
V. Creating ListForm.java and List.jsp
(for the set of the atoms from bean and form jsp files):
Step 1:
> Choose struts-config.xml.
> On the bottom: Right click at left
> Choose outline > Form Bean
> New Forn Action and JSP
Use case: AtomListForm
Superclass: Choose: org.apache.struts.action.ActionForm
> Click on Method > Click on the check-box:
buplic void reset (HttpServletRequest ...)
Choose Optional details
> Click on JSP , we see then:
New JSP Path : /jsp/atomList.jsp
Step 2:
In the net.scientificsentence.struts.form
directory, open the AtomListForm.java
> add the inside collection and ArrayList() classes:
We have then:
atomListForm.java
and
atomList.jsp
VI. Creating a mapping and class action
Step 1:
> Choose NobleGases > File > New > Class
Package net.atoms.struts.action
Name atomListAction
Optional details:
> Forwards
showList
/jsp/atomList.jsp
> Finish
Step 2:
> WEB-INF > Open the struts-config.xml
> Outline > action-mapping
> Right-click > Choose New Action
Use case: atomList
Superclass: org.apache.struts.action.Action
Optional details:
> Browse
Name: atomListForm
Input Source: /jsp/atomList.jsp
> Finish
The atomListAction is :
AtomListAction.java
VII. Database
We will use instead a collection class. Its content
is the following:
AtomsCollection
VIII. Addig Functionalities: Add, Remove data:
Creating new form bean and action form classes
AtomEditForm.java and AtomEditAction.java:
Step 1:AtomEditForm.java:
> Right-Click on the package net.atoma.NobleGases
> File > new > Class
> Generate Getter and Setter methods
Insert point: last method
> OK
The content of this file is:
AtomEditForm.java
Confirm this form in the struts-config.xml
> WEB-INF > struts-config.xml>
> Click on action mapping
> Use case atomEdit
Superclass: org.apache.struts.actions.DispatchAction
> Browse > Form
Name: atomEditForm
Optional Details:
Parameter: do
Add three forwards:
1. To atomEdit.jsp page
Forward Name: showEdit
Forward path /jsp/atomEdit.jsp
> Add
The content of the file is:
atomEdit.jsp
2. To atomAdd.jsp
Forward Name: showAdd
Forward path /jsp/atomAdd.jsp
> Add
The content of the file is:
atomAdd.jsp
3. To atomList.jsp
Forward Name: showList
Forward path /jsp/atomList.jsp
> Check: Redirect
> Add
The content of the file is:
atomList.jsp
Step 2:AtomEditAction.java:
> Right-Click on the package net.atoma.struts.action
> File > new > Class
The content of this file is:
AtomEditAction.java
IX. The result of the application
> Choose Window >Preference > MyEclpse
> Application Servers > JBoss 3
> Check JBoss Home directory (like C:\Program Files\Jboss 3)
> Check JBoss JDK name
In the bare menu
> Click on Deployment
> Check the project name
> Choose JBoss3
> Redeploy
> Start JBoss
> See the result on http://localhost:8080/NobleGases/default.do
as follows:
Greetins page
and
Noble gases table
|