Академический Документы
Профессиональный Документы
Культура Документы
Struts 2.0
Session - 4
May, 2009
Presentation By
Training & Development
Introduction
2
Mr. Thinker
3
Mr. Thinker
4
Dumbo Vs. Jumbo
Jumbo! Does JSPs completely replace Servlets?
5
Mr. Thinker
6
Mr. Thinker
View Layer is generally
What is the View layer implemented using JSPs
generally made of?
7
Dumbo Vs. Jumbo
What puzzle does MVC solve?
8
Mr. Thinker
9
Mr. Thinker
10
Dumbo Vs. Jumbo
Command Pattern is basis of what framework?
11
Mr. Thinker
12
Mr. Thinker
13
Dumbo Vs. Jumbo
Web Work is based on what Framework?
X Work
14
Mr. Thinker
15
Dumbo Vs. Jumbo
What is the minimum java requirement to use
Struts 2?
16
Mr. Thinker
17
Ms. Natasha
18
The Internals of the Ph.D.
Project
19
Using an MVC Design pattern without Struts
20
Using Struts 2
Result A MAGIC
Struts.xml Action Class
JSP BOX
(Execute business logic)
21
Dumbo Vs. Jumbo
Now I know why he talked about Filters
yesterday
22
Web.xml
As with any web application, the request first goes to web.xml
23
Filter Dispatcher
This is a Filter
24
Struts.xml
The Filter Dispatcher reads the Struts.xml to find out which action class /
method needs to be invoked
Once this information is available, the Filter Dispatcher initiates the request
processing life cycle
25
The Magic Box
The magic box does a lot of things
We entered our name, first number and second number in the screen and
suddenly and automatically these values are available in the Action Class
without our intervention
We never wrote request.getparamter!! we never did explicit type conversion
to float values
That is why we called it a Magic Box
We will explain in more detail about this Magic Box and its actual name very
shortly
26
Action Class
We already saw this. This is a normal java class which need not even extend
any other class
This is just a POJO
27
The Return Journey
After the successful execution of business logic in action class, the
framework checks the struts.xml to see which page should be displayed
The framework now displays the result jsp
28
Dumbo Vs. Jumbo
In the above explanation, I saw Filters but not
even a single servlet. Struts 2 does not support
servlets?
Cool Man !
29
Exploration of the Magic Box - Interceptors
You are my villain.
I am going to
change your
Hi! Are you request….. #@$@#$@#
available for a $???
Mr. Interceptor
date tonight?
30
Dumbo Vs. Jumbo
I cannot imagine that Struts 2 can do such destructive
things. So, interceptors are basically like viruses that
mutate your request and make it look different. Am I
correct?
Not exactly. You have got it totally wrong. Only the example
taken was like that. Interceptors are the friends of Struts 2
developers. Interceptors are used for constructive purposes.
Our trainer will explain more
31
Interceptor Pictorially
As you can see from the above picture, interceptors are part of the
controller layer
The filter invokes the various interceptors
32
What are interceptors?
Interceptors are normal Java classes
34
What are interceptors? (Contd.)
Interceptors are concepts of XWork framework on top of which Sturts2 has
been built
Interceptors are of two types
– Interceptors that come built in with the framework
– Custom Interceptors – Interceptors that can be written totally by you
35
Dumbo Vs. Jumbo
I am slowly coming to terms. But I have a
question.
<SAMPLE>
36
Configuration of Interceptor
Interceptors are generally per action
37
Exercise
In the Hello World Project, add the interceptor in struts.xml as
mentioned above and see the behavior after running it in browser
– Observe the log (Tomcat 6.0) in the Net Beans window at the
bottom – you would observe that the name of the action is
displayed in the logs
– Run the HelloWorld.jsp and you would observe that the name
does not get displayed
38
Dumbo Vs. Jumbo
Till I did the exercise it was totally clear to me. I was assuming
that the framework did a lot of things like setting screen
variables to the action variables etc. But now I am so confused.
I just introduced an interceptor element in struts.xml and it
immediately introduced a bug that whatever name I enter, it does
not get displayed in the results page. What a shame?
39
Explanation For This Behavior
Of course it is clear that the purpose of logger interceptor is to record log
messages in the console and hence you see details regarding the action name
etc in the log files
But how does introducing the logger interceptor introduce a bug like
behavior?
It should now be clear to you that configuring logger interceptor overrode a
setting that was already present
That is nothing but the “default” behavior. We just overrode the default
behavior. So the default behavior was no more available
40
Dumbo Vs. Jumbo
I understood but where do I set the default behavior? Is the
setting built in? if I want to reset the default behavior to some
other settings is there no way to do it?
41
Dumbo Vs. Jumbo
You are unbelievable man! It is so crystal clear for me now.
However, I see the term Stack being mentioned. Is there any
significance attached to it?
42
Interceptor Stack
One action can be associated with multiple interceptors
Example:
<interceptor-ref name="logger"/>
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
43
Dumbo Vs. Jumbo
That’s fine. But in the above case, what is the order of
execution? I hope the order of execution might matter in many
cases
Yes the order matters. The order of execution is the order in which the
interceptors are declared in the struts.xml under the action element.
So, in the above example, first logger interceptor will be executed,
then exception, alias, servlet config and prepare. Am I clear?
44
Dumbo Vs. Jumbo
Yes, you are. Thank you. But my original question of what is the
significance of the term Stack still remains unanswered.
45
Interceptor Stack (Contd.)
OK, now that there is a way to configure multiple interceptors for a given
action, the concept of stack is just an extension of this.
Instead of defining the interceptors one by one in the action element, an
interceptor stack can be defined
The interceptor stack can then be used in action definitions
46
Interceptor Stack (Contd.)
<action name="helloWorld"
class="com.aigss.training.struts.action.HelloWorldAction">
<interceptor-ref name="myStack"/>
<result name="success">/HelloWorldSuccess.jsp</result>
</action>
47
Dumbo Vs. Jumbo
Its all fine. But I heard that interceptors are normal Java
Classes. But where I see there is just the interceptor name. But
where is the reference to the actual java class. How does the
framework know which java class to execute for a particular
interceptor?
WoW!
Today is your day. You have been asking all gem of questions. The
answer is again struts-default.xml. There is an element called
interceptors under which the following is a typical interceptor
definition:
<interceptor name="logger"
class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"
/>
In the above, class is the java class that will be executed whenever
a logger interceptor is referred
48
Mr. Thinker
49
Mr. Thinker
The interceptor-stack
element can in turn contain
other stacks as well
50
Exercise
Dumbo has been jumping all over about a bug
Your goal is now to make the Hello World project work well again
51
Exploration of the Magic Box – OGNL / Value
Stack
52
Exploration of the Magic Box – OGNL / Value
Stack
PAGE DEVELOPER
It is just a centralized data store which can be referred by the various layers
of the framework
54
Value Stack Constituents
The value stack consists of the following objects in the provided order
– Temporary Objects
– Model Object
– Action Object
TEMPORARY OBJECT
– Named Objects
MODEL OBJECT
Application
Session ACTION OBJECT
55
How does Value Stack work?
If you want the attribute “name”, then you query the value stack for this
attribute.
Each stack element, in the provided order, is asked whether it has the
property.
If it does, then the value is returned and we are done.
56
Advantages of Value Stack
• As evident from the above discussion, the main advantage of value stack is
that you don’t care where the value is – the action, the model, or the HTTP
request – you just know that if the value exists it will be returned.
57
Accessing Value Stack – OGNL
The objects in the value stack are accessed by what is called OGNL
58
A Very Simple Example
Assume that we have an action class called “MyAction”
Inside that we have an object called “person” with getter and setter methods
Inside Person class there is an object called “address” with getter and setter
in Person Class
Inside Address class we have attributes called city, state, zipCode etc and
corresponding getters and setter methods
59
A Very Simple Example
CITY
ADDRESS
PERSON
MY ACTION
VALUE STACK
60
A Very Simple Example (Contd.)
Now I want to retrieve and display the city attribute in the JSP
61
Dumbo Vs. Jumbo
Wow!! Man. This is really interesting. I didn’t know that it would be
that easy. If not for these value stack, ognl etc my life would have been
tough. I should have retrieved the Person object either from request or
session object and then manually invoked the gettters inside these
objects. Now I am really beginning to appreciate the magic of Struts 2
62
Dumbo Vs. Jumbo
In one of the previous sessions we talked
about Romeo and Juliet. I think it was about
a concept called Dependency Injection. Was it
just for fun?
63
Interceptor
ParamsInterceptor
setMyName
(String myName)
My Action
64
Interceptor (Contd.)
Is this not dependency injection? This is called Setter Injection.
The action class just provides a setter method but it expects somebody else
to set the value to this variable called myName
Promptly the Interceptor does exactly that. The ParamsInterceptor sets the
myName variable by invoking the setter method of the action class
In one of the future sessions we will also see about another type of
dependency injection
65
Exercise
• The objective is to capture Address Details of a Person and display
them back
Street City
My
Name Address
Zip
Street Code
T hat’s a good question. You are not alone. Many developers feel a little bored
to have the same name. More importantly if I need to use the same action class
for two business logic methods then I have a problem because I cannot name
both of them as execute
We have a solution for this. You can name the business methods as
doSomething( ) or even something( ).
68
Session 4
69
Session 4
Thank You !
70