Sie sind auf Seite 1von 43

>> Delivering Value

Struts 2.0
Session - 1

May, 2009

Presentation By
Training & Development
Struts Training

Hi, I am Natasha ! Mr. Thinker


I will be your
Once in a while, he thinks loud. Needless to
Virtual Trainer for say, he makes you think as well!
this session.
Mr. Dumbo
Unlike you guys, he is pathetic. He is
Let me introduce the “Last Bench” guy. But he wants to
my friends who learn !
will assist me in
this training ! Mr. Jumbo
Wow! He resembles each of you. Very
sharp! His full-time job is to cut the nose of
Mr. Dumbo
&
Look how he is standing! I hate this guy. We
Mr. Trainer four do all the job and he takes away the
credit. Just crush him with your questions.

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

Name few frameworks available in Java

7
Rapid Fire Round

On top of what Protocol do most of the Web


Applications run?

8
Why was Struts Born?
 HTTP – Hyper Text Transfer Protocol
Server
Give me the HTML
containing the event
details

I got it. Here it is.


HTTP Client Have a good day”

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

Oh God. What can I do now? My


life is going to be tough to keep
reminding him

Who are you? I do not remember


you. So, obviously I do not
remember about the event that
you are talking about.

I communicate through HTTP.


HTTP Client Hence I am stateless. So, I do not
remember the client. ‘Get it, Serve it, Request 2
Forget it’ is my way of working

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!

I am dead man. In the database this field is


Numeric. But this guy says that he will
treat it as text only. Is there anybody to
save me?

Sorry Mate. I treat every request as text.


Even if you pass number, decimal or
HTTP Client whatever, for me everything is Text.
Have fun
Request 3

11
Mr. Thinker

Oh Wait! How can I build


complex web applications like
shopping cart if HTTP is so
primitive?

12
Enter Servlet
 Servlets alleviate the shortcomings of HTTP

 Servlets sit on top 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

Of course yes. I am here to suppress and hide your


shortcomings. I can remember clients’ previous
requests with the help of HttpSession. So, here are
HTTP Client’ the participant details of the event that you sent
out previously
Request 2 from Client 1

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

OK, I need to convert this to a Float and then


save it in the database.

Done !

Request 3 from Client 1

17
Servlet
 But servlets have got their own set of problems:

 Let us look into the contents of a typical age old Servlet


Service( ) {
If (userAction.equals(“Submit”)) {
String custNo = request.getParameter(“custNo”);
//convert the above to Integer
//Keep doing this for all the fields in the screen
//Perform some business logic
//Insert data into database
Out.println(“<HTML>”);
Out.println(“<head>”);
…..
….
Out.println(“</HTML>”);
} else if (userAction.equals(“somethingelse”)) {
//Do the same stuff again for this action
}
}
 Let us now talk about the problems with the above servlet code
18
Problem 1 – No Separation of Concern

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

How nice it would be if there is a


way not to embed HTML in servlet
but still make use of the advantages
provided by servlet!!

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

I am even more confused now. “No


more Servlets”. Then all the
advantages of servlet would be gone!!

24
Enter JSP
 No, it is not the case

 JSPs are still converted to Servlets in the background. So the advantages of


servlets are still intact even if you use JSP
 Example of an age old JSP
<%
String custNo = request.getParameter(“custNo”);
//convert the above to Integer
//Keep doing this for all the fields in the screen
//Perform some business logic
//Insert data into database
%>
<HTML>
<Head>

</Head>
<Body>

</Body>
</HTML>

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

Servlet is great and JSPs are great in


their own context. How nice it
would be if both Servlets and JSPs
can be used together to solve the
problem of separation of concern!!

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

That depends on what technology your


application is. If it is Java based, then
probably Model would be a set of Java
classes, View will be built in JSPs and
controller would be a Servlet

32
What puzzle does MVC solve?
 It solves the important problem of separation of concern

 So, how does it look like now

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?

Design patterns are just suggestions on best practices.


Design Patterns do not provide any Code Library or
something that reduces your work. So, you should still do
everything, but only advantage is that it will now look
neater and maintenance would be easy because of the
separation of concern

36
Mr. Thinker

How nice it would be if my coding


effort is reduced and somebody
helps me with coding rudimentary
and monotonous code? How nice
it would be if I do not have to
waste my energy on boiler plate
code!

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

 Many developers had a thought processes similar to Mr. Thinker and as a


result Struts 1.0 was born

38
Struts
 Struts 1.0 implements MVC Design Pattern

VIEW CONTROLLER MODEL


JSPs Action Servlet Java Classes

 The Struts 1.0 takes care in helping the developer with few of the rudimentary code

 Struts 1.x was very popular for quite some time

39
Mr. Thinker

Struts 1.x is great. Widely used, very


large developer network. Everything is
fine. But the help I get in terms of
reducing my burden is not that great.
There are some areas of improvement
still. Will somebody resolve these and
make Struts even better!!

40
Enter Struts 2.0
 Struts 2.0 makes Struts even better

 Struts 2.0 is a brand new framework

 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

Today was just meant to be an overview session. I am


sure you did not learn anything new because all of you
are already experts in Servlets/JSP etc. There will be a
lot of more interesting sessions coming up. Stay tuned!!

43

Das könnte Ihnen auch gefallen