44
Session-02

Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Embed Size (px)

Citation preview

Page 1: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Session-02

Page 2: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Index. Jsp in Struts 2

Page 3: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Web.xml File in Struts 2

Page 4: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Action Class in Struts 2

Page 5: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Struts.xml in Struts 2

Page 6: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

View in Struts 2

Page 7: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

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

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

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

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

Struts 2 default interceptors

Page 12: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Example

Page 13: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Example Explanation

Page 14: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Interceptor Class

Page 15: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

How to use Interceptors?

Page 16: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Create Interceptor Class

Page 17: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Create Action Class

Page 18: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Struts.xml

Page 19: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

OUTPUT

Page 20: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

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

Which class is the Front Controller in Struts2?

Page 22: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

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

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

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

Mostly used Interceptors

Page 26: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2
Page 27: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

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

Dynamic data handling using Struts 2 ValueStack

Page 29: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

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

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

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

Static data versus dynamic data

Page 33: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

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

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

Struts 2 ActionContext Class

Page 36: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

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

Struts 2 ActionInvocation Class

Page 38: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Example

Page 39: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

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

Role of OGNL in Struts 2

Page 41: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

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

Continue…

Page 43: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Continue…

Page 44: Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2

Summary!!