Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Session-02. Index. Jsp in Struts 2 Web.xml File in Struts 2."— Presentation transcript:

1 Session-02

2 Index. Jsp in Struts 2

3 Web.xml File in Struts 2

4 Action Class in Struts 2

5 Struts.xml in Struts 2

6 View in Struts 2

7 Struts 2 disadvantages 1.Bigger learning curve 2.Poor documentation 3.Less transparent

8 Core Components 1.Struts 2 Interceptors 2.Struts 2 ValueStack 3.Struts 2 ActionContext 4.Struts 2 ActionInvocation 5.Struts 2 OGNL

9 1) Struts 2 Interceptors InterceptorInterceptor is an object that is invoked at the preprocessing and postprocessing of a request. InterceptorsInterceptors are the backbone of Struts2 Framework. Struts2 interceptorsStruts2 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. ActionInvocationActionInvocation 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.

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

11 Struts 2 default interceptors

12 Example

13 Example Explanation

14 Interceptor Class

15 How to use Interceptors?

16 Create Interceptor Class

17 Create Action Class

18 Struts.xml

19 OUTPUT

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

21 Which class is the Front Controller in Struts2?

22 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:

23 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:

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

25 Mostly used Interceptors

26

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

28 Dynamic data handling using Struts 2 ValueStack

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

30 How does ValueStack in Struts2 work? Struts2 puts in the stack are not the properties, but the objects that hold those properties.

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

32 Static data versus dynamic data

33 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:

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

35 Struts 2 ActionContext Class

36 4) Struts 2 ActionInvocation The ActionInvocation represents the execution state of an action. It holds the action and interceptors objects.

37 Struts 2 ActionInvocation Class

38 Example

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

40 Role of OGNL in Struts 2

41 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. #sessionFor example to get the data from session scope, we need to use #session as given in the following example:

42 Continue…

43

44 Summary!!


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

Similar presentations


Ads by Google