>> Delivering Value
Struts 2.0 Session - 8 June, 2009 Presentation By Training & Development
Introduction Good Evening everybody! Welcome to the Day 8 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
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 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
Ms. Natasha Thanks. Let us quickly move on to today’s session. We will cover a very interesting concept today. i.e. writing your own interceptors. Sounds interesting? Keep listening.
5
Mr. Thinker Interceptors are similar to what?
Interceptors are similar to Filters
6
Mr. Thinker Where is the default setting of interceptor set?
The default interceptor to be used is set in a file called “strutsdefault.xml”
7
Dumbo Vs. Jumbo What is the order in which the interceptors get executed in an interceptor stack?
The interceptors in the interceptor stack are executed in the order in which they are defined in the Interceptor stack
8
Mr. Thinker What is value stack?
Value stack is a stack of objects
9
Dumbo Vs. Jumbo I have an object called PurchaseOrder inside which I have Asset inside which I have Invoice. Inside Invoice I have invoiceNo. How can I access invoiceNo using OGNL?
purchaseOrder.asset.invoice.invoiceNo
10
Mr. Thinker What is the name of the interceptor that sets the variables of action class from the screen fields?
ParamsInterceptor
11
Dumbo Vs. Jumbo What is the tag that should be added for the errors to be displayed in the JSP
<s:actionerror />
12
Mr. Thinker Workflow What is the name of the interceptor that invokes the validate method of the action class and acts according to the result of the validate method? (either pass the control back to the page or invoke the action method)
13
Mr. Thinker What is the name of the method in any interceptor that always gets invoked during the request processing chain?
doIntercept
14
Dumbo Vs. Jumbo To which interface does the interceptor’s doIntercept method type cast the action class to check for any errors?
ValidationAware
15
Mr. Thinker What is the name of the file that provides details about the resource bundle file etc
struts.propertie s
16
Mr. Thinker What are the three ways of creating your own interface?
Implement Interceptor, extend AbstractInterceptor or extend MethodFilterInterceptor
17
Mr. Thinker How many methods are there in the Interceptor interface and what are they?
init, destroy and intercept
18
Dumbo Vs. Jumbo What is the parameter for intercept method?
ActionInvocation
19
Dumbo Vs. Jumbo In the intercept method what should you do not break the chain of invocations?
You should call the method actionInvocation.invoke( )
20
Mr. Thinker In turn which method does ActionInvocation.invoke call?
It internally calls interceptor.intercept
21
Mr. Thinker What is the name of the method exposed by MethodFilterInterceptor?
doIntercept
22
Dumbo Vs. Jumbo What is the name of the parameter which is used to specify the name of the methods for which the interceptors should not be invoked?
excludeMethods
23
Mr. Thinker What is the name of the parameter which is used to specify the name of the methods for which the interceptors should be invoked?
includeMethods
24
Mr. Thinker How do you pass parameters to an interceptor?
Using the param element
25
Mr. Thinker How can I pass different values for the same parameter in different actions?
Use param name=”interceptor.para mname” instead of just parameter rname
26
Dumbo Vs. Jumbo If I want my interceptor to accept parameters, which of the three approach I will take? Whether implement Interceptor, extend AbstractInterceptor or extend MethodFilterInterceptor?
No Bar. Any one can be used
27
Ms. Natasha Thanks tireless threesome. Let us continue with today’s session. Today we will be looking into the following topics: – Action Context – Accessing Servlet objects from action class – SessionAware
Let us start our discussion with the first topic that is Action Context:
28
Mr. Thinker What are the objects available in Value Stack?
Temporary Objects, Model Obejct, Action Object, named objects in that order
29
Dumbo Vs. Jumbo I have a very good question here. I have been taught about the above in Day 4 itself. The order is also clear. But what is not clear is that, whether the order is from top to bottom or from bottom to top? Since it is a stack, this is very important. Yeah! I now remember
I totally appreciate your question. The order is from top to bottom. I thought you should not have asked this question. We have repeatedly heard from the trainer that the first thing the Action Invocation does is to create the action class object and put it in the value stack. So, obviously that should be at the bottom of the stack. 30
Mr. Thinker I have a person object in my action class. This action class object in turn is put on top of value stack. How do we access the name parameter inside the person object using ognl?
I say person.firstName
31
Dumbo Vs. Jumbo I have another question. If I have the same attribute (i.e. with same name) available in multiple locations inside the value stack, then which one of them will be returned?
It is now clear
Once again this was also discussed. Since it is a stack, the lookup is from top to bottom. So, whenever an object is found, it would be returned and the object with same name under these will be ignored. Let us see an example in the next slide:
32
Dumbo Vs. Jumbo Hey Jumbo! I forgot something. I know action object is put inside value stack and ognl refers to value object for any expression. But why don’t we give the expression as actionObject.person.firstName instead of person.firstName
This was also covered. The OGNL expression starts looking inside the top level objects of value stack and it does not start from the top level object itself. In other words you can just ignore the top level objects of value stack in the ognl. Since action is a top level object you can just ignore it and start from person.firstName
33
Example
34
Dumbo Vs. Jumbo !!!!!
In the above diagram, when I refer name, the name inside myModel is returned and the name inside myAction is ignored. One more point to note: see how we are ignoring the top level object in the OGNL expression. We do not say myModel.name, we just say name 35
Important Point about Value Stack Value Stack is created for each request. When we referred value stack as a global container for data, it does not
mean that it is just one object for all users.
The word global is not from the number of users perspective but from a per
request perspective. Any data required to serve the request / action will be available in the value stack
36
Dumbo Vs. Jumbo Wow!! Now lots of my questions about value stack are gone. But however, tell me, where is value stack? Is it just the end of the world? Is there nobody above value stack under which value stack is placed? Is value stack the top most data container?
It looks like the syllabus is leaked? Otherwise, how could you correctly preempt our next topic! To answer your question No, Value Stack is not the top most data container. There is something beyond that. That is called Action Context. Let us now look into more details on Action Context:
37
Action Context Do you remember how we represented Value Stack a few sessions ago? Let
us relook that.
38
Action Context • Well as above, we just stopped with value stack • That was just for the purpose of over-simplification. The correct representation should be like this
39
Dumbo Vs. Jumbo hmm!! Things are changing now. It is now clear that value stack is itself placed in another top level object called Action Context. This leads me to an assumption that value stack is not the only object in action context. There should be other objects in action context also. Otherwise, what is the need for action context?
You are perfectly right Dumbo!! There are other objects in action context apart from value stack. But it goes without saying that value stack is the hero!! Let us drill down more on action context
40
Action Context Objects Objects in Action Context: The following are the objects inside Action Context – Value Stack – parameters – application – session – attr – Request
41
Action Context Objects • It is represented in the diagram below
42
Dumbo Vs. Jumbo Wait, before even you move further, I am irritated to say the least. I still remember the trainer talking about the type of objects in value stack. It was spelled out as Temporary Objects, Model Object, Action Object and Named Objects. Under named objects, he talked about things like application, session etc. Was he wrong?
I understand why you feel the way you do. But believe me, it was again a case of over-simplification. If we had talked about Action Context on that particular day, it would have been a bit tougher for you to understand things and you would have in fact lost interest. Let us now see what the trainer has to say.
43
Objects in Value Stack TEMPORARY OBJECT MODEL OBJECT Action Object Application, SESSION
Action Context
So the contents of value stack are: – Action Object – Model Object – Temporary Object
TEMPORARY OBJECT MODEL OBJECT ACTION OBJECT
44
Dumbo Vs. Jumbo I am a bit confused now. What kind of objects are stored in value stack and what kind of objects are stored in action context
Let us summarize this once and for all
45
Value Stack Vs. Action Context Value Stack
Action Context
Contains only domain data (i.e. application data)
This contains infrastructure data + Value Stack
Does not contain infrastructure data (i.e. servlet related objects like request, session etc)
Action Stack is the top most container
Value Stack is inside Action Context
46
Dumbo Vs. Jumbo I heard the trainer saying that Value Stack is per request. It is created for each request. How about action context then?
Obviously action context is also per request. Action context is also created for each request or atleast refreshed for each request. Dumbo: Who created action context?
47
Dumbo Vs. Jumbo All is well. But tell me, why do I need Action Context at all. I initially thought Value Stack itself is a burden. Why one more action context class? Whatever it provides are already available from the servlet specification? Struts 2 could have given me a handle to request, session etc directly. Why Action Context? It has complicated my life.
It has definitely not complicated your life. Struts 2 strives for clean MVC implementation. Let the trainer explain more.
Action Context is created by Default Action Invocation class. Refer the init method and you should come to know of this. You can also let the trainer show you the source code of Action Context class.
48
Mr. Thinker What are the parameters for execute method in Struts 1.x?
Form Bean, request and response
49
Clean MVC implementation – Struts 1.x Vs Struts 2 In Struts 1.x, we need to pass around the form bean, request and response
object all around from page to action class
But Struts 2 is an advocator of clean MVC implementation. You do not need
these extra over head parameters in the execute method.
All data is stored in one location called Action Context. Whether it is
infrastructure data or domain data, you surrender to Action Context. It will save you unlike Struts 1.x where you need to refer multiple places to get data
50
Dumbo Vs. Jumbo Now I appreciate the use of action context object. Long live action context
Cool
51
Natasha Hope you clearly understood this concept of action context Now let us see how we use this action context in action
Let us move on to the next topic which is “Using Action Context in Action”
52
Exercise It is easy that we learn this via an exercise Your Address Capture Application currently has two pages one for
input and the other for output
Add a Login Page (User Name, Password and Login Button). On click of Login, invoke the LoginAction Login Action will put the user name into session. No need to lookup
against any authentication schema. Just consider all users to be valid. Only check for blank user name and password in which case you given an error
On successful login, the Address capture input page will be displayed. Create an interceptor which will not allow access of the address
capture input page / output page directly. Users should always go via Login Page. If they directly type the URL of inner pages, route them back to the Login Page 53
Exercise Big Hints: – To get a session anywhere in the framewok, do this: – ActionContext.getContext( ).getSession( ); – This returns a Map – Then you can put any attribute in the session by Map.put(“name”, value); – You can get any attribute in the session by Map.get(“name”);
54
Dumbo Vs. Jumbo It was a nice way of explaining the usage of session in action. But can I avoid this boring code of getting session from action context? Is there somebody who can help
Yes, there is. Refresh your memory on Dependency Injection. Then listen to the trainer
55
Session Aware Interface As evident already, this is for lazy guys If you do not want to take the trouble of getting the reference to the session
yourself, then use this
The interface contains just one method called setSession(Map) If I am lazy, I just make my action implement sessionAware interface and then
implement setSession method
As per dependency injection principle, somebody else will invoke the setSession
method for you and you do not need to take the trouble
56
Dumbo Vs. Jumbo But what should I do within the setSession method
Typically you have a class level variable called session and this setSession method will just assign the input parameter to the class level variable. So simple!!
57
Dumbo Vs. Jumbo And then right from the word go in my action I can start using the session
Yes because the setSession method was invoked by somebody else and was invoked long before the action’s execute method was invoked.
58
Exercise Change the previous exercise Instead of taking session yourself, make it implement session aware
interface
59
Session 8 I hope you enjoyed today’s session. It introduced you to the new concept of Action Context and then we also talked about how we can practically use action context by accessing session object. We will see you tomorrow with more interesting topics. Till then, have fun!!
60
Session 8 Thank You !!
61