Which design pattern the Interceptors in Struts2 is based on

Interceptors in Struts2 are based on Intercepting Filters

What is Pull-MVC and push-MVC based architecture? Which architecture does Struts2 follow??

Pull-MVC and Push-MVC are better understood with how the view layer is getting data i.e. Model to render. In case of Push-MVC the data (Model) is constructed and given to the view layer by the Controllers by putting it in the scoped variables like request or session. Typical example is Spring MVC and Struts1. Pull-MVC on the other hand puts the model data typically constructed in Controllers are kept in a common place i.e. in actions, which then gets rendered by view layer. Struts2 is a Pull-MVC based architecture, in which all data is stored in Value Stack and retrieved by view layer for rendering.

Are Interceptors in Struts2 thread safe?

No, Unlike Struts2 action, Interceptors are shared between requests, so thread issues will come if not taken care of.

Are Interceptors and Filters different?

Apart from the fact that both Interceptors and filters are based on intercepting filter, there are few differences when it comes to Struts2. i. Filters: 1. Based on Servlet Specification 2. Executes on the pattern matches on the request. 3. Not configurable method calls ii. Interceptors: 1. Based on Struts2. 2. Executes for all the request qualifies for a front controller (A Servlet filter).And can be configured to execute additional interceptor for a particular action execution. 3. Methods in the Interceptors can be configured whether to execute or not by means of exclude methods or include Methods. ( see tutorial on

In Struts1, the front-controller was a Servlet but in Struts2, it is a filter. What is the possible reason to change it to a filter?

There are two possibilities why filter is designated as front controller in Struts 2 i. Servlet made as front controller needs developer to provide a right value in which lets the framework to initialize many important aspects (viz. struts configuration file) as the container starts. In absence of which the framework gets initialized only as the first request hits.Struts2 makes our life easy by providing front-controller as a filter and by nature the filters in web.xml gets initialized automatically as the container starts. There is no need of such load-on-start-up tag. ii. The second but important one is the introduction of Interceptors in Struts2 framework. It not just reduce our coding effort, but helps us write any code which we would have used filters for coding and necessary change in the web.xml as opposed to Struts1.So now any code that fits better in Filter can now move to interceptors (which is more controllable than filters), all configuration can be controlled in struts.xml file, no need to touch the web.xml file. iii. The front controller being a filter also helps towards the new feature of Struts i.e. UI Themes. All the static resources in the Themes now served through the filter

Which class is the front-controller in Struts 2?

The class "org.apache.struts2.dispatcher.FilterDispatcher? is the front controller in Struts2. In recent time Struts 2.1.3 this class is deprecated and new classes are introduced to do the job.

What is the role of Action/ Model?

Actions in Struts are POJO, is also considered as a Model. The role of Action are to execute business logic or delegate call to business logic by the means of action methods which is mapped to request and contains business data to be used by the view layer by means of setters and getters inside the Action class and finally helps the framework decide which result to render

What is the relation between Value-Stack and OGNL?

Value-Stack is a place where all the data related to action and the action itself is stored. OGNL is a mean through which the data in the Value-Stack is manipulated.

What is struts.devMode and why it is used?

Struts.devMode is a key used in struts.properties file (Can also be configured in struts.xml file as) , to represent whether the framework is running in development mode or production mode by setting true or false. If set to development mode, it gives the following benefits : i. Resource bundle reload on every request; i.e. all localization properties file can be modified and the change will be reflected without restarting the server. ii. struts.xml or any configuration files can be modified without restarting or redeploying the application iii. The error occurs in the application will be reported, as oppose to production mode. Also remember that struts.devMode should be marked as false in production environment to reduce impact of performance. By default it is "false".

What is the difference between empty default namespace and root name space?

If the namespace attribute is not defined in the package tag or assigned "" value then it is called empty default namespace. While if "/" is assigned as value to the namespace attribute then it is called as root namespace. The root namespace is treated as all other explicit namespaces and must be matched. It?s important to distinguish between the empty default namespace, which can catch all request patterns as long as the action name matches, and the root namespace, which is an actual namespace that must be matched.

What is the difference between Action and ActionSupport?

Action is interface defines some string like SUCCESS, ERROR etc. and an execute () method. For convenience Developer implement this interface to have access to String field in action methods. ActionSupport on other hand implements Action and some other interfaces and provides some feature like data validation and localized error messaging when extended in the action classes by developers.

What is execute and wait interceptor?

The ExecuteAndWaitInterceptor is great interceptor provided out of box in Struts2 for running long-lived actions in the background while showing the user a nice progress meter or a progress bar. For example while uploading a large file to the server we can use this interceptor to display a nice running progress bar instead of leaving the user in confusion that the application is not responding. This also prevents the HTTP request from timing out when the action takes more than 5 or 10 minutes.

Does the order in which interceptors execute matters? If yes then why?

Well, the answer is yes and no. Some Interceptors are designed to be independent so the order doesnot matter, but some interceptors are totally dependent on the previous interceptor?s execution. For example the validation and workflow interceptors, the validation interceptors checks if there is any error in the form being submitted to the action, then comes the workflow interceptor who checks if validation (occurred in the last) has any error, in presence of error it will not let the rest of the interceptors ( if any ) in the stack to execute. So this is very important that the validation interceptors execute first before the workflow. On the other hand let?s say you wrote an interceptors who is doing the authentication and you have the user credential provided ( by the time this executes) it doesnot matter where this interceptors is placed( It is a different fact that you would like to keep it in the top ).

Who loads the struts.xml file? Which Struts2 API loads the struts.xml file?

In Struts2 FilterServlet is the responsible class for loading struts.xml file as we deploy the application on the container. Unlike Servlet (as with Struts1) needs the load-on-start-up tag to load the front controller, a filter doesnot need to have load on start-up tag to be loaded as the application is deployed. As with servlet specification a filter is loaded/ executed as the application starts up.

What is the Dispatch Action (Struts1) equivalent in Strtus2?

Struts1 provided the facility of having related action methods in a Single Action class, depending on the method parameter, the mapped methods were executed. To achieve this we have to extend the DispatchAction class in Struts1.But this comes as default in Struts2, We need to provide the qualified methods in the action class( no need to inherit any class), and provide the mapping of action path/name to the method attribute in action tag(struts.xml) and proper code in view layer.

What is abstract package in Struts2, and what is its use?

An abstract package usually defines inheritable components such as interceptor, different interceptor stacks, result types etc. But doesnot contain any actions. The way we declare a package as abstract is through the package elements attribute as abstract and setting the value to "true". By default (if abstract attribute is not mentioned) it is false.Struts-default.xml is an example of abstract package.

What is the advantage of having a POJO class as an action?

As mentioned in the first question, the POJO class is light weight and easy to test with frameworks like JUnit. Moreover, there is no need to create separate ActionForms to hold the values from the source web page.

What is the advantage of extending the action class from ActionSupport class?

The use of ActionSupport class provides the features like validation, locale support and serialization to an action

How can one integrate Spring IoC with Struts 2?

Struts 2 come with support for Spring and Spring can be used to inject beans to various classes. It can also be used to inject properties to the action class of struts 2. For configuring Spring, contextLoaderListener can be configured.

Link
Eng. Ravi Kumar Verma

Simple and dept learning technology is very important part in software development. Same story has been implemented at this site, so learn and implement the inovative things, Thanks

Ravi Kumar Verma