Академический Документы
Профессиональный Документы
Культура Документы
Struts 2.0
Session - 9
June, 2009
Presentation By
Training & Development
Introduction
2
Introduction
Day 1 – History of Web applications
It will be another Day 2 – Concepts to be known before
exciting day of learning. learning Struts 2
Let us now do a quick
recap on what we have Day 3 – Struts 2 environment setup, Hello
learned so far: World Project, PhD Project
Day 4 – Dissection of Hello World, Value
Stack, OGNL
Day 5 – Action Interface, Action Support
class, Validation, Resource Bundle and
Internationalization
Day 6 – Validation Framework provided by
Struts 2
Day 7 – Writing your own interceptors
4
Dumbo Vs. Jumbo
What should be the name of the result (or the
return string) in case of validation errors?
Input
5
Mr. Thinker
params
What is the name of the
interceptor that transfers
data from the screen fields
to the action fields?
6
Dumbo Vs. Jumbo
To which interface does the interceptor’s doIntercept
method type cast the action class to check for any
errors?
ValidationAware
7
Mr. Thinker
8
Mr. Thinker
9
Dumbo Vs. Jumbo
Where are field errors displayed in the page?
10
Mr. Thinker
11
Mr. Thinker
12
Dumbo Vs. Jumbo
if there is a validation error in the XML then will the
validate method of action class be executed?
13
Dumbo Vs. Jumbo
How do you enclose OGNL expression inside
validation xml?
${ }
14
Mr. Thinker
15
Mr. Thinker
Expression
16
Dumbo Vs. Jumbo
When is the error message displayed for a validator
type of expression?
17
Dumbo Vs. Jumbo
What are the two re-alignments that we need to make
as far as our understanding on the value stack is
concerned?
18
Mr. Thinker
19
Mr. Thinker
20
Mr. Thinker
21
Dumbo Vs. Jumbo
Value Stack is placed under which top level object?
22
Dumbo Vs. Jumbo
How many kinds of objects are placed inside action
context object
6 kinds of objects
23
Dumbo Vs. Jumbo
What are the 6 kinds of objects
24
Mr. Thinker
What are parameters?
25
Mr. Thinker
26
Mr. Thinker
27
Mr. Thinker
28
Dumbo Vs. Jumbo
What is the scope of action
context?
29
Dumbo Vs. Jumbo
If I am requesting a static HTML page will
the action context be created? Provide
reasoning for your answer
30
Dumbo Vs. Jumbo
What is the advantage of action
context?
31
Mr. Thinker
32
Mr. Thinker
None
33
Mr. Thinker
ActionContext.getContex
t( ).getSession( )
34
Mr. Thinker
It returns a Map
35
Dumbo Vs. Jumbo
Why does it return a Map and
not HttpSession?
36
Dumbo Vs. Jumbo
How do we avoid even performing the method
invocation of ActionContext.getContext(
).getSession( )?
37
Mr. Thinker
38
Mr. Thinker
39
Ms. Natasha
40
Preparable Interface
41
Mr. Thinker
42
Mr. Thinker
43
A Very Typical Scenario
An object contains 20 fields
At the end of the third page, the data from the object is stored into
the database
44
How is this implemented traditionally?
Step 1: On click of next in the first page, store all the fields in the object and
store the object in the session
Step 2: On click of next in the second page, retrieve the object from the
session. Then start storing the next 5 fields in the object. Put the object back
into the session
Step 3: On click of next in the third page, retrieve the object from the session.
Then start storing the next 5 fields in the object. Then store the data from the
object into the database
45
The Preparable Interface
As the name suggests, the Preparable Interface is used to implement any
preparatory work before invoking the action’s actual business method (i.e.
execute or corresponding method)
Preparable interface provides one method called “void prepare( )”
So, if your stack contains PrepareInterceptor, then the prepare method would
be called
defaultStack already contains PrepareInterceptor
Let us now look into the source code of this interceptor and also check how
defaultStack has configured this interceptor
46
The Preparable Interface (Contd.)
Using the Preparable Interface to implement the above scenario:
– Make your action implement the Preparable interface
– In the prepare method prepare the corresponding object by retrieving it
from session and storing it in the object
– Because of outsourcing these preparatory logic to the prepare method, the
action method now will focus only on pure business logic
47
ModelDriven Interface
48
Mr. Thinker
49
Dumbo Vs. Jumbo
In all the examples we have been seeing, the person object contains,
name, age etc and also other details like address etc. In the JSP I
have been referring these fields as person.firstName, person.age,
person.address.street etc. Is there a way we can avoid the person and
just firstName, age, address.street etc?
50
ModelDriven Interface
This interface contains one method called getModel( )
If your stack contains this interceptor then from the JSP you can just refer as
firstName instead of person.firstName
51
Dumbo Vs. Jumbo
May I know what happens in the background
52
Block Diagram
TEMPORARY OBJECT
MODEL OBJECT
ACTION OBJECT
53
Dumbo Vs. Jumbo
:I understand that it is responsible for pushing the data
into value stack and now I have model object. But how
does it ensure that I can just refer them as firstName
instead of Person.firstName
I thought you should be knowing the answer for this. We have been telling
time and again that in the OGNL expression you can ignore all top level
objects. That is why we have been referring as person.firstName instead of
actionObject.person.firstName. In the case of our discussion, the Person object
would be returned from getModel method. So, the Person object itself would be
pushed into the stack. The Person object would be a top level object in the
stack. So, I can ignore person and start from next level. Is it clear?
54
Model Driven Interface (Contd.)
Let us now look into the source code of this interface and
ModelDriveInterceptor
Let us also check how the defaultStack also implements this interface
55
Exercise
My application contains the following screens:
Screen 1:
– Heading – Borrower Details
– First Name
– Last Name
– Age
– No of dependents
– Next button
– Validations
Age should be greater than 18
First Name, Age and no of dependents are mandatory
56
Exercise
Screen 2:
– Heading – Contact Details
– Address 1
– Address 2
– City
– State
– Country
– Next Button
– Validations
Address 1, city, state and country are mandatory
57
Exercise
Screen 3:
– Heading – Employment Details
– Employer Name
– Employer Address1
– Employer Address2
– Employer City
– Employer State
– Employer Country
– No of years
– Submit Button
– Validations
Employer Name, address 1, city, state, country and no of years
are mandatory
58
Exercise
Screen 4:
– Heading – Consolidated View
– Provide a message, your application has been successfully submitted.
The following are the details submitted by you:
– <Here provide all the details of the first three screens under the
corresponding headings in one page>
Object Design
– Person is the top level object and inside that Address and employer
are child objects. Respective variables should be stored accordingly in
the correct object
59
Exercise
Assignment:
– Implement the above screens using Preparable and ModelDriven
interface
– Hint: getModel should return Person Object
– Also modoify the already developed session interceptor such that if I
type the second, third or fourth page directly, it should lead me to the
first page
60
Session 8
61
Session 9
Thank You !!
62