Beruflich Dokumente
Kultur Dokumente
Struts 2.0
Session - 5
May, 2009
Presentation By
Training & Development
Introduction
2
Introduction
On Day 1 we learned the history of Web
Let us do a quick Applications starting from HTTP till Struts 2
overview on what we On Day 2 we learned about a few important
have learned so far concepts which form the basis of Struts 2
namely Filters, Command pattern,
Annotations and Dependency Injection
On Day 3 we learned the background of how
Struts 2 was formed. We also setup the
environment and did two great projects
namely Hello World and Ph.D in
Mathematics
On Day 4 we dissected the Hello World
Project and Ph.D Project to know the
internals. We also got introduced to
important concepts called Interceptors,
Value Stack and OGNL
Let us now do a random recap on the first 4
days
3
Mr. Thinker
4
Dumbo Vs. Jumbo
Jumbo! Does JSPs completely replace Servlets?
5
Mr. Thinker
6
Mr. Thinker
7
Mr. Thinker
8
Dumbo Vs. Jumbo
Command Pattern is basis of what framework?
9
Mr. Thinker
10
Dumbo Vs. Jumbo
Web Work is based on what Framework?
X Work
11
Mr. Thinker
12
Mr. Thinker
FilterDispatcher. It
What is the first
is a Filter class
component in Struts 2
that receives the
request from the client.
13
Dumbo Vs. Jumbo
What is the name of the file that contains all the
configuration details related to the framework?
struts.xml
14
Mr. Thinker
15
Mr. Thinker
16
Dumbo Vs. Jumbo
With what is the return of action class checked?
17
Mr. Thinker
18
Mr. Thinker
19
Mr. Thinker
20
Dumbo Vs. Jumbo
What is the difference between an Interceptor and
an Interceptor Stack?
21
Dumbo Vs. Jumbo
What is the order in which the interceptors get
executed in an interceptor stack?
22
Mr. Thinker
What is OGNL?
Object Graph Notation
Language
23
Mr. Thinker
24
Dumbo Vs. Jumbo
I have an object called PurchaseOrder inside which I
have Asset inside which I have Invoice. Inside
Invoice I have invoiceNo. How can I access
invoiceNo using OGNL?
purchaseOrder.asset.invoice.invoiceNo
25
Mr. Thinker
26
Mr. Thinker
27
Mr. Thinker
28
Ms. Natasha
29
Primary responsibilities of
Action
30
Primary responsibilities of Action
Actions encapsulate the unit of work
31
Encapsulating the unit of work
Struts 2 is an action based framework
The primary purpose of the action then is to fulfill the business logic. (i.e.
serve as the starting point of model layer)
The primary purpose of the execute method is exactly this. Executing
business logic
32
Dumbo Vs. Jumbo
Just one quick question. I know that execute method should
purely only contain the business logic. But before executing the
business logic there might be a need for other things. One of
them is populating the business objects which I know is handled
by the params interceptor. But how about any other
validations that need to happen before executing the business
logic methods?
33
Locus for data transfer
Being the model component, action is responsible for taking around the
model data all around
As we have already seen, action class has model variables which are class
level variables
Hence it is so easy to access these model data from within the action class
We have also already seen that throughout the framework it is easy to access
these data because of the magic called “Value Stack”
34
Returning control string for result routing
After executing the business logic it is the action’s responsibility to inform
which page should be displayed
For this, the action method uses the return string
The return string is checked against the struts.xml’s action element’s result
element and the corresponding page is displayed to the user
35
Dumbo Vs. Jumbo
That was good explanation about action. Tell me something.
You had mentioned that Struts 2 is an action based framework.
Hence Action class is the hero even though there are other
character artists like value stack, OGNL, interceptors etc.
Being the Hero, I was expecting more help from the framework
in this area. But I do not see any help being rendered by the
framework in the action class. Action class is a java class
entirely written by the developer. So, what is the fun!!!
36
The Action Interface
The Action Interface is the first answer to Dumbo’s question
Your actions can implement this Action Interface in order to tap the
benefits provided by this interface
37
Constituents of Action Interface
The Action Interface provides just one method as follows
– String execute( ) throws Exception
38
Dumbo Vs. Jumbo
Well!! Are you fooling me? What is so great about this
interface? It just provides one method which anyway I am
using and few string constants. First tell me what are these
string constants and then tell me something better than this
action interface.
39
The ActionSupport class
Dumbo is very true in stating that Action interface is not so great.
Fortunately we have an answer
ActionSupport is the best example of how flexible and easy the framework
is
ActionSupport provides implementations of various important interfaces
including Action
If your actions extend this class, you automatically gain access to these
important default behaviors of various interfaces
Interceptors combine with ActionSupport class to provide us with a great
channel to solve complex problems
40
The ActionSupport class (Contd.)
Interceptor
+ Action Support Great Combination to solve complex problems
• The following provides the relationship between the components that we saw
now.
«interface»
Action
ActionSupport
YourAction
41
Exercise
• Change the Address Capture project such that it uses ActionSupport
class
42
The ActionSupport class
The following are the interfaces that the ActionSupport class implements:
– Action
– Validateable
– ValidationAware
– TextProvider
– LocaleProvider
43
Exercise
We already have a Person class
In the screen where you captured the address, capture the age of the
person also
Now run this program and display the age in the result screen apart
from the addresses that you already displaying
44
Dumbo Vs. Jumbo
We have seen the details of Action Support class. But how do
we use it in practice? What are the advantages of using this
class?
There are lots of advantages. Yes that would be the next topic
for the day
45
Validation
The action support class implements an interface called Validateable which
has a method called validate( )
If your action extends ActionSupport you can override this validate method
to include any validations
46
Exercise
In the Address Capture Application, include the following
in the action class:
public void validate()
{
if (this.getPerson().getAge() < 18)
{
this.addActionError("The Age should be greater
than 18. Sorry!!");
}
}
Now run the Address Capture application. Enter the age
as a value less than 18
Observe the behavior 47
Exercise
Now add the following element to the struts action
element for the address capture action
<result name="input">/AddressCapture.jsp</result>
Now run the address capture application. Enter the age
as a value less than 18
Observe the behavior
49
The Internals of the Validation flow
Let us take a close look at struts-default.xml
...
<interceptor name="workflow“
class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/><
interceptor-stack name="defaultStack">
...
<interceptor-ref name="params"/>
...
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
...
<interceptor-stack name="defaultStack">
50
The Internals of the Validation flow (Contd.)
When the user submits the request from the browser, struts-default.xml is referred
When the params interceptor is reached, the screen values are copied to Action
class variables
After params interceptor, somewhere down the line, there is an interceptor called
DefaultWorkflowInterceptor with a reference called “workflow”
So, the doIntercept method of DefaultWorkflowInterceptor is called
Please note that the validate method does not return any value
The validate method just puts an error message using addActionError method
51
The Internals of the Validation flow (Contd.)
The doIntercept method after invoking validate method checks if there any errors
raised, by using hasErrors method
If there are errors then the workflow is cut short. The action method is not
executed. Instead the workflow is returned back to the page by passing the return
string as “input”
If there are no errors, then the next interceptor is invoked
52
Dumbo Vs. Jumbo
Things are beginning to fall in place but I have so many
questions. What is the importance of the two interfaces
Validateable and ValidationAware? Are these used anywhere
apart from the ActionSupport class
Yes of course. The trainer will show you the source code of
DefaultWorkflowInterceptor where you can see that the
interfaces Validateable and ValidationAware are used to typecast
the Action class to these interface types and then invoke the
validate and hasErrors methods respectively. Now you know the
power of interfaces and type casting!!
53
Dumbo Vs. Jumbo
Yes. Cool man. My next question. You remember when we
did our exercise, there were a few errors which gradually got
corrected by themselves. As soon as I just added the validate
method and ran the application I got some exception that there
is no result definition blah blah blah. What was that?
54
Dumbo Vs. Jumbo
But then we faced the next problem. The system just displayed
the input page but did not show any error message or nothing.
It just showed whatever I entered. Is not a bug in struts?
No, definitely not. Since we have mentioned this jsp in our result
element the control is passed back to this page. However we did
not specify to the struts framework that the error should be
displayed. We then added the code <s:actionerror /> which tells
the framework that the error should be displayed on the page. So,
the error was promptly displayed after we included this code in the
action
55
Dumbo Vs. Jumbo
Wow man. That was great explanation. One last question. I
was surprised to see that whatever I entered originally was
still there even after the error was raised. How are the values
maintained over requests.
56
Dumbo Vs. Jumbo
Technology has improved so much aaa!! Now what next!!
BTW, the error message is hard coded in the action class. It’s a
bad practice you know. Is there a better way?
57
Resource Bundles and Internationalization
As already seen, the ActionSupport class implements TextProvider and
LocaleProvider interfaces
These are the interfaces used for internationalization and resource bundling
Let us now see how we can take advantage of this provision from Struts
58
Exercise
Create a properties file called struts.properties under default package
In the Action class instead of hardcoding the error use the following:
– this.addActionError(this.getText("address.age.error");
Any error messages can be added in future by just adding a set of key
value pairs in the guest.properties and correspondingly referring them
from the action class
59
Dumbo Vs. Jumbo
All is well. But is it Good Man. One more question. I
mandatory that the resource see that the error message is
bundle should be named as displayed at the top of the page.
guest. Can I name it as Is there a way I can have error
whatever I want? messages displayed above each
field instead of at the top
60
Exercise
Note down the name of the age field in the Address Capture JSP
You will find that the error message is displayed above the Age field
instead of at the top
61
Exercise
Add a new field called salary
If there are no validation errors, the next page should display all
details including Salary
62
Session 5
63
Session 5
Thank You !
64