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

Preview:

Citation preview

Session-02

Index. Jsp in Struts 2

Web.xml File in Struts 2

Action Class in Struts 2

Struts.xml in Struts 2

View in Struts 2

Struts 2 disadvantages

1. Bigger learning curve

2. Poor documentation

3. Less transparent

Core Components

1. Struts 2 Interceptors

2. Struts 2 ValueStack

3. Struts 2 ActionContext

4. Struts 2 ActionInvocation

5. Struts 2 OGNL

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.

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.

Struts 2 default interceptors

Example

Example Explanation

Interceptor Class

How to use Interceptors?

Create Interceptor Class

Create Action Class

Struts.xml

OUTPUT

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.

Which class is the Front Controller in Struts2?

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:

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:

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.

Mostly used Interceptors

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.

Dynamic data handling using Struts 2 ValueStack

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.

How does ValueStack in Struts2 work?

• Struts2 puts in the stack are not the properties, but the objects that hold those

properties.

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).

Static data versus dynamic data

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:

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.

Struts 2 ActionContext Class

4) Struts 2 ActionInvocation• The ActionInvocation represents the execution state of an action. It holds the action

and interceptors objects.

Struts 2 ActionInvocation Class

Example

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.

Role of OGNL 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:

Continue…

Continue…

Summary!!

Recommended