>> Delivering Value

Struts 2.0
Session - 9
June, 2009 Presentation By Training & Development

Introduction
Good Evening everybody! Welcome to the Day 9 of the Struts 2 training course

2

Introduction
 Day 1 – History of Web applications
It will be another exciting day of learning. Let us now do a quick recap on what we have learned so far:

 Day 2 – Concepts to be known before

learning Struts 2

 Day 3 – Struts 2 environment setup, Hello

World Project, PhD Project Stack, OGNL

 Day 4 – Dissection of Hello World, Value  Day 5 – Action Interface, Action Support

class, Validation, Resource Bundle and Internationalization Struts 2

 Day 6 – Validation Framework provided by  Day 7 – Writing your own interceptors  Day 8 – Action Context, accessing Session

and SessionAware interface

3

Ms. Natasha
Let us now let Thinker, Dumbo and Jumbo refresh your memory by providing reviews of important points over the past week

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
To which interface does the interceptor’s doIntercept method type cast the action class to invoke the validate method? Validateable

8

Mr. Thinker
What is the name of the file that provides details about the resource bundle file etc struts.propertie s

9

Dumbo Vs. Jumbo
Where are field errors displayed in the page?

Above the corresponding field

10

Mr. Thinker
What are parameters addFieldError? the for Field Name, Error message

11

Mr. Thinker
What is the method to retrieve an error message from the resource bundle? this.getText

12

Dumbo Vs. Jumbo
if there is a validation error in the XML then will the validate 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

13

Dumbo Vs. Jumbo
How do you enclose OGNL expression inside validation xml?

${ }

14

Mr. Thinker
What should be the name of the validation xml if I have just one business method inside the action class? <action class>validation.xml

15

Mr. Thinker
What is the validator type to be used for complex validations? Expression

16

Dumbo Vs. Jumbo
When is the error message displayed for a validator type of expression?

If the expression mentioned inside param returns false

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?

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

18

Mr. Thinker
If I have the attribute with the same name in multiple locations inside the value stack, which one will be returned?

The first occurrence’s value will be returned from the top.

19

Mr. Thinker
Why don’t we give the ognl expression as actionObject.person.name instead of person.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

20

Mr. Thinker
What is the scope of Value Stack? Value stack is created for each request

21

Dumbo Vs. Jumbo
Value Stack is placed under which top level object?

Action Context 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

Value Stack, parameters, application, session, attr, request

24

Mr. Thinker
What are parameters? Parameters are nothing but request.getParameters. In other words all form fields are stored in parameters

25

Mr. Thinker
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

What is attr? What is the difference between attr and parameters?

26

Mr. Thinker
Does Value Stack contain Infrastructure data? No, value stack does not contain infrastructure data?

27

Mr. Thinker
What kind of data does value stack contain then? Value stack contains only application data (or domain data)

28

Dumbo Vs. Jumbo
What is the scope of action context?

Action Context is also created per request like value stack (i.e. per action).

29

Dumbo Vs. Jumbo
If I am requesting a static HTML page will the action context be created? Provide reasoning 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

30

Dumbo Vs. Jumbo
What is the advantage of action context?

Clean MVC implementation. We do not have traces of servlet API inside action class

31

Mr. Thinker
Which class creates Action Context? Action Context and in fact value stack are created by Default Action Invocation class which implements Action Invocation interface. The method is init method

32

Mr. Thinker
What are the parameters for execute method in struts 2? None

33

Mr. Thinker
What is the method to get the session from action context? ActionContext.getContex t( ).getSession( )

34

Mr. Thinker
What does it return? It returns a Map

35

Dumbo Vs. Jumbo
Why does it return a Map and not HttpSession?

Because of the same reason explained previously. i.e. clean mvc implementation. We do not want traces of servlet api inside action class

36

Dumbo Vs. Jumbo
How do we avoid even performing the method invocation of ActionContext.getContext( ).getSession( )?

You use SessionAware interface

37

Mr. Thinker
What are the methods in SessionAware interface? There is just one method called setSession(Map)

38

Mr. Thinker
What are you supposed to inside setSession method? Have a class level variable and set it from setSession method

39

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

40

Preparable Interface

41

Mr. Thinker
When are action objects created? (i.e. at what scope?) Action objects are created for each request (i.e. for each action)

42

Mr. Thinker
Since action objects are created for each request, the variables inside action objects are also created every time

43

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

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 Then start storing the next 5 fields in the object. Then store the data from the object into the database

 Step 3: On click of next in the third page, retrieve the object from the session.

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( )”  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

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
What are the contents of the Value Stack? Action Object, Model Object and temporary object in the order of bottom to top

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?

Yes, there is a way. There is an interface called ModelDriven interface. Let the trainer explain more on this

50

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

51

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

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 Thanks it was a nice explanation.

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
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

61

Session 9
Thank You !!

62

Sign up to vote on this title
UsefulNot useful