Beruflich Dokumente
Kultur Dokumente
Struts 2.0
Session - 1
May, 2009
Presentation By
Training & Development
Struts Training
2
Agenda and Course Design
Day 1
This is the Agenda – What is Software Framework
for today – Struts Background
Hyper Text Transfer Protocol
Servlet
Ready for the training JSP
! Now Mr. Trainer MVC Design Pattern
will take over and I
– Struts
will see you after
sometime..
3
Training Objective
After completing today’s session, you should have learned the following:
– Software Framework
– Struts Background
4
Introduction to Struts
5
What is Software Framework?
Definition: A Framework is a piece of structural software that attempts to
provide a platform upon which applications of that domain can be more
quickly built.
Some facts about Frameworks:
– Frameworks provide a base code library above / using which
applications can be built
– Frameworks by themselves are not end products. They just provide
starting points. We still need to develop applications using Frameworks
– Frameworks automate common tasks
– Frameworks provide architectural solutions
6
Rapid Fire Round
7
Rapid Fire Round
8
Why was Struts Born?
HTTP – Hyper Text Transfer Protocol
Server
Give me the HTML
containing the event
details
Request 1
9
Important Attributes of HTTP
• HTTP is Stateless
Hi Guy! Nice to contact you again. Server
Give me the participant details of
the event that you just gave me
10
Important Attributes of HTTP
• HTTP treats all messages as text messages
Server
Hey Heavy Weight Server! I am passing you
the salary of the customer as a Float Value.
Please post it to the database ultimately!
11
Mr. Thinker
12
Enter Servlet
Servlets alleviate the shortcomings of HTTP
13
How does Servlet alleviate the problems?
HTTP is stateless, but servlets will help to overcome this shortcoming as
mentioned in the cartoon below
14
Servlet
Hi Guy! Nice to contact you again. Take it Client
Give me the participant details of
the event that you just gave me
Wow ! Thanks !
Servlet
How many times I can tell this guy that I
am stateless !
Servlet! Can you help this client? Here is Server
his request
15
Servlet
• HTTP treats all messages as text messages, but servlets will help overcome
this shortcoming
16
Servlet
Hey Heavy Weight Server! I am passing Hey, done man!
you the salary of the customer as a Float
Value. Please post it to the database
ultimately!
Wow ! Thanks !
HTTP Client
Let me pass this to the Servlet guy as
Server
usual
Servlet
Done !
17
Servlet
But servlets have got their own set of problems:
STORY
SCREENPLAY
DIALOGUES
MUSIC
LYRICS
DIRECTION
T. RAJENDER M.A
19
Problem 1 – Separation of Concern
“Separation of Concern” is totally missing both from the above cartoon as
well as the above servlet code.
In other words, the servlet takes care of:
Getting request parameters
Converting request parameters to appropriate Java Types
Performing Business Logic
Inserting data into database
Building HTML output to be sent to the client
Ideally this is a bad idea to have one person do everything
20
Problem 2 – HTML
HTML is built from within the servlet
– In the above code, HTML is built from within the servlet code
– Each time a simple user interface change was needed, the Java developer
needs to modify the Servlet code, recompile the source and then to
deploy the application into the server environment.
– This is a maintenance nightmare
21
Mr. Thinker
22
Enter JSP
• Once people started realizing the complications that servlet introduced, a
new concept called JSP was introduced
An important
announcement to Server
Side Developers !
Here is the end to all your
sufferings
Your lives will be much
simpler from now on
No more HTML in servlet
No more Servlet
No more Servlet
Long live JSP
Long live
23
Mr. Thinker
24
Enter JSP
No, it is not the case
25
JSP
What problems have been resolved with using JSP?
– No more HTML code in Java
With JSPs, the maintenance nightmare of having HTML inside java is
totally eliminated. So, making a HTML UI change is pretty simple
New problems introduced
– In JSPs we need to write Java code in JSPs which is more like writing
java code in HTML. This is equally if not more confusing and difficult
Old problems that remain
– The separation of concern that we talked about still remains because
now JSP takes care of everything rather than Servlet
26
Mr. Thinker
27
Enter MVC Design Pattern
MVC design pattern came into existence in order to resolve the issues that we
have been looking into, in the past few slides
This pattern also satisfies the wishes of Mr. Thinker, i.e., using Servlet and
JSPs together
Let us have a detailed look into this design pattern
28
MVC Dissected
– M Model
Data and Logic
User Interface
– V View
– C Controller Coordinator
29
Enter MVC Design Pattern
As evident from the above, the MVC design pattern suggests splitting the
whole architecture into three layers.
– The Model Layer takes care of Business Logic and beyond and hence is
the owner of application/business data
– The View Layer takes care of rendering the screens to the users (Screens)
– The Controller Layer coordinates between the Model and the View and
also selects appropriate views for the given user action
30
MVC Structure Generic
31
Dumbo Vs. Jumbo
Is this MVC just theoretical? If I want to
implement MVC design pattern, which
technology should I use as Model, View and
Controller
32
What puzzle does MVC solve?
It solves the important problem of separation of concern
33
What puzzle does MVC solve?
Direction: Shankar
Hero: Rajinikanth
Lyrics: Vairamuthu
Music: A.R.Rahman
34
What puzzle does MVC solve?
As you can see in this case, there is an expert in each area who completely
owns that particular area. Hence we have clear separation of concerns
35
Dumbo Vs. Jumbo
OK, now I have decided to use MVC design
pattern in my application. Is there any
reduction of coding effort by using this
pattern?
36
Mr. Thinker
37
Enter Struts
We initially talked about frameworks, what they provide us and what
problem they solve
Mr. Thinker is totally spot on. It would be great if we have somebody or
something helping us with boring coding
– Eg. Getting request parameters and setting them into Model Object
parameters
– Converting Java type to strings and vice versa
– Error handling and validation
38
Struts
Struts 1.0 implements MVC Design Pattern
The Struts 1.0 takes care in helping the developer with few of the rudimentary code
39
Mr. Thinker
40
Enter Struts 2.0
Struts 2.0 makes Struts even better
Struts 2.0 also implements MVC framework and has lot of improvements
over Struts 1.x
We will be looking through this interesting technology in much detail in
sessions to come
41
Metamorphosis of Struts
42
Ms. Natasha
43