>> Delivering Value

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

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


 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


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


Dumbo Vs. Jumbo
What should be the name of the result (or the return string) in case of validation errors?



Mr. Thinker
params What is the name of the interceptor that transfers data from the screen fields to the action fields?


Dumbo Vs. Jumbo
To which interface does the interceptor’s doIntercept method type cast the action class to check for any errors?



Mr. Thinker
To which interface does the interceptor’s doIntercept method type cast the action class to invoke the validate method? Validateable


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


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

Above the corresponding field


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


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


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


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

${ }


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


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


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

If the expression mentioned inside param returns false


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


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.


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


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


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

Action Context Object


Dumbo Vs. Jumbo
How many kinds of objects are placed inside action context object

6 kinds of objects


Dumbo Vs. Jumbo
What are the 6 kinds of objects

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


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


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?


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


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


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

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


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


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

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


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


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


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


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


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


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

You use SessionAware interface


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


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


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


Preparable Interface


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


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


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


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.


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


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


ModelDriven Interface


Mr. Thinker
What are the contents of the Value Stack? Action Object, Model Object and temporary object in the order of bottom to top


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


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


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


Block Diagram


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?

Model Driven Interface (Contd.)
 Let us now look into the source code of this interface and


 Let us also check how the defaultStack also implements this interface


 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


 Screen 2: – Heading – Contact Details – Address 1 – Address 2 – City – State – Country – Next Button – Validations

 Address 1, city, state and country are mandatory


 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


 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


 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


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


Session 9
Thank You !!


Sign up to vote on this title
UsefulNot useful