session-02. index. jsp in struts 2 web.xml file in struts 2
TRANSCRIPT
![Page 1: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/1.jpg)
Session-02
![Page 2: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/2.jpg)
Index. Jsp in Struts 2
![Page 3: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/3.jpg)
Web.xml File in Struts 2
![Page 4: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/4.jpg)
Action Class in Struts 2
![Page 5: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/5.jpg)
Struts.xml in Struts 2
![Page 6: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/6.jpg)
View in Struts 2
![Page 7: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/7.jpg)
Struts 2 disadvantages
1. Bigger learning curve
2. Poor documentation
3. Less transparent
![Page 8: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/8.jpg)
Core Components
1. Struts 2 Interceptors
2. Struts 2 ValueStack
3. Struts 2 ActionContext
4. Struts 2 ActionInvocation
5. Struts 2 OGNL
![Page 9: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/9.jpg)
1) Struts 2 Interceptors• Interceptor is an object that is invoked at the preprocessing and postprocessing of a
request.
• Interceptors are the backbone of Struts2 Framework.
• Struts2 interceptors are responsible for most of the processing done by the
framework, such as passing request params to action classes, making Servlet API
request, response, session available to Action classes, validation, i18n support, etc.
• In Struts 2, interceptor is used to perform operations such as validation, exception
handling, internationalization, displaying intermediate result etc.
• ActionInvocation is responsible to encapsulate Action classes and interceptors and
to fire them in order.
• The most important method for use in ActionInvocation is invoke() method that keeps
track of the interceptor chain and invokes the next interceptor or action.
![Page 10: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/10.jpg)
Advantage of interceptors
• Pluggable If we need to remove any concern such as validation, exception handling,
logging etc. from the application, we don't need to redeploy the application.
• We only need to remove the entry from the struts.xml file.
![Page 11: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/11.jpg)
Struts 2 default interceptors
![Page 12: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/12.jpg)
Example
![Page 13: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/13.jpg)
Example Explanation
![Page 14: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/14.jpg)
Interceptor Class
![Page 15: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/15.jpg)
How to use Interceptors?
![Page 16: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/16.jpg)
Create Interceptor Class
![Page 17: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/17.jpg)
Create Action Class
![Page 18: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/18.jpg)
Struts.xml
![Page 19: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/19.jpg)
OUTPUT
![Page 20: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/20.jpg)
Does interceptors is thread safe?• Struts2 interceptors are singleton classes and a new thread is created to handle the
request,
• So it’s not thread safe and we need to implement them carefully to avoid any issues
with shared data.
![Page 21: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/21.jpg)
Which class is the Front Controller in Struts2?
![Page 22: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/22.jpg)
Stacking multiple Interceptors• As you can imagine, having to configure multiple interceptor for each action would
quickly become extremely unmanageable.
• For this reason, interceptors are managed with interceptor stacks.
• Here is an example, directly from the struts-default . xml file:
![Page 23: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/23.jpg)
How to Use basicStack ?• We have already seen how to apply interceptor to the action, applying interceptor
stacks is no different.
• In fact, we use exactly the same tag:
![Page 24: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/24.jpg)
What is the use of execAndWait interceptor?
• Sometimes we have long running actions where user will have to wait for final result.
• In this case, user can get annoyed with no response and may refresh the page
causing issues in application.
• Struts2 provide execAndWait interceptor that we can use for long running action
classes to return an intermediate result to user while the processing is happening at
server side.
• Once the processing is finished, user will be presented with the final result page.
• Struts2 execAndWait interceptor is already defined in the struts-default package
and we just need to configure it for our action classes.
• The implementation is present in ExecuteAndWaitInterceptor class that returns “wait”
result page until the processing of action class is finished.
![Page 25: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/25.jpg)
Mostly used Interceptors
![Page 26: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/26.jpg)
![Page 27: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/27.jpg)
2) Struts 2 ValueStack • A valueStack is simply a stack that contains application specific objects such as
action objects and other model object.
• At the execution time, action is placed on the top of the stack.
• We can put objects in the valuestack, query it and delete it.
![Page 28: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/28.jpg)
Dynamic data handling using Struts 2 ValueStack
![Page 29: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/29.jpg)
Continue…• ValueStack, one of the powerful features of Struts 2, holds all the data during
processing of a request.
• Action classes also store data in the form of beans. (In Struts 2 you use getter and
setter methods.)
• Once the data is stored in the action class objects, Struts 2 puts that data onto the
ValueStack in the form of the Action object.
• ValueStack stores a stack of objects, and this data will be exposed to the other parts
of the framework.
![Page 30: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/30.jpg)
How does ValueStack in Struts2 work?
• Struts2 puts in the stack are not the properties, but the objects that hold those
properties.
![Page 31: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/31.jpg)
Static data versus dynamic data• If data that will be displayed on the UI is known in advance, it can be called static
data.
• Static data can be handled easily by coding it directly in JavaServer Pages (JSP).
![Page 32: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/32.jpg)
Static data versus dynamic data
![Page 33: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/33.jpg)
Continue…• The value stack is a set of several objects which keeps the following objects in the
provided order:
you can get valueStack object inside your action as follows:
![Page 34: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/34.jpg)
3) Struts 2 ActionContext• The ActionContext is a container of objects in which action is executed.
• The values stored in the ActionContext are unique per thread (i.e. Thread Local).
• So we don't need to make our action thread safe.
• The struts framework places other objects in ActionContext also e.g. map
representing the request, session, application scopes.
![Page 35: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/35.jpg)
Struts 2 ActionContext Class
![Page 36: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/36.jpg)
4) Struts 2 ActionInvocation• The ActionInvocation represents the execution state of an action. It holds the action
and interceptors objects.
![Page 37: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/37.jpg)
Struts 2 ActionInvocation Class
![Page 38: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/38.jpg)
Example
![Page 39: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/39.jpg)
5) Struts 2 OGNL• The Object Graph Navigation Language (OGNL) is an expression language.
• It simplifies the accessibility of data stored in the ActionContext.
• OGNL also helps in data transfer and type conversion.
• The struts framework sets the ValueStack as the root object of OGNL.
• Notice that action object is pushed into the ValueStack.
• We can direct access the action property.
![Page 40: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/40.jpg)
Role of OGNL in Struts 2
![Page 41: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/41.jpg)
Continue…• The struts framework places other objects in ActionContext also e.g. map
representing the request, session, application scopes.
• To get these values i.e. not the action property, we need to use # notation.
• For example to get the data from session scope, we need to use #session as given
in the following example:
![Page 42: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/42.jpg)
Continue…
![Page 43: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/43.jpg)
Continue…
![Page 44: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2](https://reader037.vdocuments.site/reader037/viewer/2022110207/56649d8d5503460f94a75156/html5/thumbnails/44.jpg)
Summary!!