Date post: | 18-Nov-2014 |
Category: |
Documents |
Upload: | api-26580974 |
View: | 186 times |
Download: | 1 times |
>> Delivering Value
Struts 2.0Session - 9
June, 2009
Presentation ByTraining & Development
2
Introduction
Good Evening everybody! Welcome to the Day 9 of the
Struts 2 training course
3
Introduction Day 1 – History of Web applications
Day 2 – Concepts to be known before learning Struts 2
Day 3 – Struts 2 environment setup, Hello 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
Day 8 – Action Context, accessing Session and SessionAware interface
It will be another exciting day of learning. Let us now do a quick recap on what we have
learned so far:
4
Ms. Natasha
Let us now let Thinker, Dumbo and Jumbo refresh your memory by
providing reviews of important points over the past week
5
Dumbo Vs. Jumbo
What should be the name of the result (or thereturn string) in case of validation errors?
Input
6
Mr. Thinker
What is the name of theinterceptor that transfersdata from the screen fieldsto the action fields?
params
7
Dumbo Vs. Jumbo
To which interface does the interceptor’s doInterceptmethod type cast the action class to check for anyerrors?
ValidationAware
8
Mr. Thinker
To which interface does theinterceptor’s doInterceptmethod type cast the actionclass to invoke the validatemethod?
Validateable
9
Mr. Thinker
What is the name of the filethat provides details aboutthe resource bundle file etc
struts.properties
10
Dumbo Vs. Jumbo
Where are field errors displayed in the page?
Above the corresponding field
11
Mr. Thinker
What are theparameters foraddFieldError?
Field Name, Error message
12
Mr. Thinker
What is the method to retrievean error message from theresource bundle?
this.getText
13
Dumbo Vs. Jumbo
if there is a validation error in the XML then will thevalidate method of action class be executed?
Yes, it will be executed because it is always better to display the errors at one go to the user
14
Dumbo Vs. Jumbo
How do you enclose OGNL expression insidevalidation xml?
${ }
15
Mr. Thinker
What should be the name of thevalidation xml if I have justone business method inside theaction class?
<action class>-validation.xml
16
Mr. Thinker
What is the validator type to beused for complex validations?
Expression
17
Dumbo Vs. Jumbo
When is the error message displayed for a validatortype of expression?
If the expression mentioned inside param returns false
18
Dumbo Vs. Jumbo
What are the two re-alignments that we need to makeas far as our understanding on the value stack isconcerned?
Value Stack is not the top level object. This itself is placed on Action Context object. 2. Value stack itself does not contain named objects like application, session etc. it is contained by Action Context
19
Mr. Thinker
If I have the attribute with the samename in multiple locations inside thevalue stack, which one will be returned?
The first occurrence’s value will be returned from the top.
20
Mr. Thinker
Why don’t we give the ognlexpression asactionObject.person.name instead ofperson.name?
The OGNL expression starts looking from inside each top level object. Any OGNL expression is hence considered to start from inside the top level object
21
Mr. Thinker
What is the scope of ValueStack?
Value stack is created for each request
22
Dumbo Vs. Jumbo
Value Stack is placed under which top level object?
Action Context Object
23
Dumbo Vs. Jumbo
How many kinds of objects are placed inside actioncontext object
6 kinds of objects
24
Dumbo Vs. Jumbo
What are the 6 kinds of objects
Value Stack, parameters, application, session, attr, request
25
Mr. Thinker
What are parameters?
Parameters are nothing but request.getParameters. In other words all form fields are stored in parameters
26
Mr. Thinker
What is attr? What is thedifference between attr andparameters?
attr contains all the attributes set in any scope starting from page, request, session, servlet and application. Parameters are just form field. The advantage with attr is that you can get the attribute in any scope without actually specifying the scope. Whichever scope is encountered first, it will be automatically returned. The order of search would be in the same order mentioned above
27
Mr. Thinker
Does Value Stack containInfrastructure data?
No, value stack does not contain infrastructure data?
28
Mr. Thinker
What kind of data does valuestack contain then?
Value stack contains only application data (or domain data)
29
Dumbo Vs. Jumbo
What is the scope of actioncontext?
Action Context is also created per request like value stack (i.e. per action).
30
Dumbo Vs. Jumbo
If I am requesting a static HTML page willthe action context be created? Providereasoning for your answer
If it is static html page, then ideally the request will never pass beyond Filter. Hence action context will not be created
31
Dumbo Vs. Jumbo
What is the advantage of actioncontext?
Clean MVC implementation. We do not have traces of servlet API inside action class
32
Mr. Thinker
Which class creates ActionContext?
Action Context and in fact value stack are created by Default Action Invocation class which implements Action Invocation interface. The method is init method
33
Mr. Thinker
What are the parameters forexecute method in struts 2?
None
34
Mr. Thinker
What is the method to get thesession from action context?
ActionContext.getContext( ).getSession( )
35
Mr. Thinker
What does it return?
It returns a Map
36
Dumbo Vs. Jumbo
Why does it return a Map andnot HttpSession?
Because of the same reason explained previously. i.e. clean mvc implementation. We do not want traces of servlet api inside action class
37
Dumbo Vs. Jumbo
How do we avoid even performing the methodinvocation of ActionContext.getContext().getSession( )?
You use SessionAware interface
38
Mr. Thinker
What are the methods inSessionAware interface?
There is just one method called setSession(Map)
39
Mr. Thinker
What are you supposed toinside setSession method?
Have a class level variable and set it from setSession method
40
Ms. Natasha
Thanks a lot. Let us now continue with today’s lessons. Today we will be discussing
about two very important interfaces and their usages.
The Preparable interface and ModelDriven interface
41
Preparable Interface
42
Mr. Thinker
When are action objectscreated? (i.e. at what scope?) Action objects are
created for each request (i.e. for each action)
43
Mr. Thinker
Since action objects are created for eachrequest, the variables inside actionobjects are also created every time
44
A Very Typical Scenario
An object contains 20 fields
The first 10 fields are data entered in the first page
The next 5 fields are data entered in the second page
The next 5 fields are data entered in the third page
At the end of the third page, the data from the object is stored into the database
45
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
46
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( )”
prepare is a call back method
The method is called by an interceptor called “PrepareInterceptor”
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
47
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
48
ModelDriven Interface
49
Mr. Thinker
What are the contents of theValue Stack? Action Object, Model
Object and temporary object in the order of bottom to top
50
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 Ihave been referring these fields as person.firstName, person.age,person.address.street etc. Is there a way we can avoid the person andjust firstName, age, address.street etc?
Yes, there is a way. There is an interface called ModelDriven interface. Let the trainer explain more on this
51
ModelDriven Interface
This interface contains one method called getModel( )
If your action implements ModelDriven interface, then you should implement getModel( ) method and in this method just return the Person object (or whichever you feel is the model object)
The getModel method is called by an interceptor called ModelDrivenInterceptor
If your stack contains this interceptor then from the JSP you can just refer as firstName instead of person.firstName
52
Dumbo Vs. Jumbo
May I know what happens in the background
Yes, the ModelDriven Interceptor invokes the getModel method and whatever be the return it just pushes it into the Value Stack. This is what we have been referring as Model Object above Action Object in the value stack. Please see the diagram once again in the next slide
53
Block Diagram
TEMPORARY OBJECT
MODEL OBJECT
ACTION OBJECT
54
Dumbo Vs. Jumbo
:I understand that it is responsible for pushing the datainto value stack and now I have model object. But howdoes it ensure that I can just refer them as firstNameinstead 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?
Thanks it was a nice explanation.
55
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
56
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
57
Exercise Screen 2:
– Heading – Contact Details– Address 1– Address 2– City– State– Country– Next Button– Validations Address 1, city, state and country are mandatory
58
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
59
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
60
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
61
Session 8
I hope you had a very good exercise as it covers many of the concepts that we have
learned till now.You have a very good night and we will see
you back soon
62
Session 9
Thank You !!