struts - day9

62
>> Delivering Value Struts 2.0 Session - 9 June, 2009 Presentation By Training & Development

Upload: api-26580974

Post on 18-Nov-2014

201 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Struts - Day9

>> Delivering Value

Struts 2.0Session - 9

June, 2009

Presentation ByTraining & Development

Presenter
Presentation Notes
Employee Name: Trainer Name: Date of Training:
Page 2: Struts - Day9

2

Introduction

Good Evening everybody! Welcome to the Day 9 of the

Struts 2 training course

Page 3: Struts - Day9

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:

Page 4: Struts - Day9

4

Ms. Natasha

Let us now let Thinker, Dumbo and Jumbo refresh your memory by

providing reviews of important points over the past week

Page 5: Struts - Day9

5

Dumbo Vs. Jumbo

What should be the name of the result (or thereturn string) in case of validation errors?

Input

Page 6: Struts - Day9

6

Mr. Thinker

What is the name of theinterceptor that transfersdata from the screen fieldsto the action fields?

params

Page 7: Struts - Day9

7

Dumbo Vs. Jumbo

To which interface does the interceptor’s doInterceptmethod type cast the action class to check for anyerrors?

ValidationAware

Page 8: Struts - Day9

8

Mr. Thinker

To which interface does theinterceptor’s doInterceptmethod type cast the actionclass to invoke the validatemethod?

Validateable

Page 9: Struts - Day9

9

Mr. Thinker

What is the name of the filethat provides details aboutthe resource bundle file etc

struts.properties

Page 10: Struts - Day9

10

Dumbo Vs. Jumbo

Where are field errors displayed in the page?

Above the corresponding field

Page 11: Struts - Day9

11

Mr. Thinker

What are theparameters foraddFieldError?

Field Name, Error message

Page 12: Struts - Day9

12

Mr. Thinker

What is the method to retrievean error message from theresource bundle?

this.getText

Page 13: Struts - Day9

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

Page 14: Struts - Day9

14

Dumbo Vs. Jumbo

How do you enclose OGNL expression insidevalidation xml?

${ }

Page 15: Struts - Day9

15

Mr. Thinker

What should be the name of thevalidation xml if I have justone business method inside theaction class?

<action class>-validation.xml

Page 16: Struts - Day9

16

Mr. Thinker

What is the validator type to beused for complex validations?

Expression

Page 17: Struts - Day9

17

Dumbo Vs. Jumbo

When is the error message displayed for a validatortype of expression?

If the expression mentioned inside param returns false

Page 18: Struts - Day9

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

Page 19: Struts - Day9

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.

Page 20: Struts - Day9

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

Page 21: Struts - Day9

21

Mr. Thinker

What is the scope of ValueStack?

Value stack is created for each request

Page 22: Struts - Day9

22

Dumbo Vs. Jumbo

Value Stack is placed under which top level object?

Action Context Object

Page 23: Struts - Day9

23

Dumbo Vs. Jumbo

How many kinds of objects are placed inside actioncontext object

6 kinds of objects

Page 24: Struts - Day9

24

Dumbo Vs. Jumbo

What are the 6 kinds of objects

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

Page 25: Struts - Day9

25

Mr. Thinker

What are parameters?

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

Page 26: Struts - Day9

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

Page 27: Struts - Day9

27

Mr. Thinker

Does Value Stack containInfrastructure data?

No, value stack does not contain infrastructure data?

Page 28: Struts - Day9

28

Mr. Thinker

What kind of data does valuestack contain then?

Value stack contains only application data (or domain data)

Page 29: Struts - Day9

29

Dumbo Vs. Jumbo

What is the scope of actioncontext?

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

Page 30: Struts - Day9

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

Page 31: Struts - Day9

31

Dumbo Vs. Jumbo

What is the advantage of actioncontext?

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

Page 32: Struts - Day9

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

Page 33: Struts - Day9

33

Mr. Thinker

What are the parameters forexecute method in struts 2?

None

Page 34: Struts - Day9

34

Mr. Thinker

What is the method to get thesession from action context?

ActionContext.getContext( ).getSession( )

Page 35: Struts - Day9

35

Mr. Thinker

What does it return?

It returns a Map

Page 36: Struts - Day9

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

Page 37: Struts - Day9

37

Dumbo Vs. Jumbo

How do we avoid even performing the methodinvocation of ActionContext.getContext().getSession( )?

You use SessionAware interface

Page 38: Struts - Day9

38

Mr. Thinker

What are the methods inSessionAware interface?

There is just one method called setSession(Map)

Page 39: Struts - Day9

39

Mr. Thinker

What are you supposed toinside setSession method?

Have a class level variable and set it from setSession method

Page 40: Struts - Day9

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

Page 41: Struts - Day9

41

Preparable Interface

Page 42: Struts - Day9

42

Mr. Thinker

When are action objectscreated? (i.e. at what scope?) Action objects are

created for each request (i.e. for each action)

Page 43: Struts - Day9

43

Mr. Thinker

Since action objects are created for eachrequest, the variables inside actionobjects are also created every time

Page 44: Struts - Day9

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

Page 45: Struts - Day9

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

Page 46: Struts - Day9

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

Page 47: Struts - Day9

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

Page 48: Struts - Day9

48

ModelDriven Interface

Page 49: Struts - Day9

49

Mr. Thinker

What are the contents of theValue Stack? Action Object, Model

Object and temporary object in the order of bottom to top

Page 50: Struts - Day9

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

Page 51: Struts - Day9

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

Page 52: Struts - Day9

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

Page 53: Struts - Day9

53

Block Diagram

TEMPORARY OBJECT

MODEL OBJECT

ACTION OBJECT

Page 54: Struts - Day9

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.

Page 55: Struts - Day9

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

Page 56: Struts - Day9

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

Page 57: Struts - Day9

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

Page 58: Struts - Day9

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

Page 59: Struts - Day9

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

Page 60: Struts - Day9

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

Page 61: Struts - Day9

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

Page 62: Struts - Day9

62

Session 9

Thank You !!