What is Java Struts?
Struts is a free and open source framework for developing Java EE web applications. Struts2 is the combination of Webwork and struts1 frameworks. Struts2 is a popular and mature web application framework based on the MVC design pattern.
Explain Architecture of struts2?
Struts 2 architecture is divided in 4 major categories.
Servlet filters: Servlet filters – Servlet filter comprises of 3 major components, those are ActionContextCleanUp, SiteMesh and FilterDispatcher.
Struts core: This is the actual Model part of Struts 2. It contains the main business logic. Struts Core category consists of Action, Interceptor,ActionProxy, ActionMapper, Result and Tag library systems.
Interceptors: In Struts 2, almost every action is associated with an interceptor or a set of interceptors. It can be called before or after the request processing. It can also be configured for each action basis individually. Interceptors define common cross cutting tasks in clean and reusable architecture.
User defined classes/files: Action classes, JSP or any other view and struts.xml etc are part of user defined classes or files.
What are the features of struts 2?
- Easy Integration and easy to modify Tags
- POJO forms and POJO Actions
- Tag support and strong validation support
- Check boxes do not require any kind of special application for false values.
- Any class can be used as an action class and one can input properties by using any JavaBean directly to the action class.
- Strut 2 actions are spring friendly and so easy to Spring integration.
- AJAX theme enables to make the application more dynamic.
- Portal and servlet deployment are easy due to automatic portlet support without altering code.
- The request handling in every action makes it easy to customize, when required.
What is interceptor? And life cycle methods of interceptor?
Interceptor is an object i.e. invoked at preprocessing and postprocessing of a request. It is pluggable
- public void init()
- public void intercept(ActionInvocation ai)
- public void destroy()
What are Struts2 core components?
Struts2 core components are:
- Action Classes
- Interceptors
- Result Pages, JSP of FreeMarker templates
- ValueStack, OGNL and Tag Libraries
What are the some useful annotations introduced in Struts2?
- Action Annotation(Result and Namespace)
- @ParentPackage annotation
- Interceptor Annotation
- Validation Annotation
- Type Conversion Annotation
- @ResultPath
Can you explain Value Stack?
The value stack is a set of several objects which keeps the following objects in the provided order −
Temporary Objects: There are various temporary objects which are created during execution of a page. For example the current iteration value for a collection being looped over in a JSP tag.
The Model Object: If you are using model objects in your struts application, the current model object is placed before the action on the value stack.
The Action Object: This will be the current action object which is being executed.
Named Objects: These objects include #application, #session, #request, #attr and #parameters and refer to the corresponding servlet scope
What is OGNL?
OGNL stands for Object Graph Navigation Language. It is an expression language of Struts2.
What is use of i18n interceptor?
The i18n interceptor is used to provide multi lingual support for struts application. com.opensymphony.xwork2.interceptor.I18nInterceptor interceptor is responsible for i18n support in Struts2 applications.
Can you explain aware interfaces in struts2?
Aware interfaces are used to store information in request, session, application and response objects. The 4 aware interfaces are given below:
- ServletRequestAware
- ServletResponseAware
- SessionAware
- ServletContextAware
What types of validations are available in xml based validation in struts2?
Following is the list of various types of field level and non-field level validation available in Struts2
- date validator
- double validator
- email validator
- expression validator
- int validator
- regex validator
- required validator
- requiredstring validator
- stringlength validator
- url validator
What are the classes used in Struts?
Struts Framework consists of following classes:
Action Servlets:It is  used to control the response for each incoming request.
Action Class: Used to handle the request.
Action Form: It is java bean, used to refer to forms and associated with action mapping.
Action Mapping: It is used for mapping between object and action.
Action Forward: It is used to forward the result from controller to destination.
What is Action Class? What are the types of Action Class?
Action Class in Struts framework defines the business logic. An action class handles the client request and prepares the response. It also decides where the response should be forwarded.
 We have following types of action classes in struts:
- Action: The basic action class in which we implement our business logic.
- Include Action: Similar as include page directive in jsp.
- Forward Action: It is used in case we need to forward the request from one JSP to another. If we directly forward the request from one jsp it violates the MVC architecture. Hence an action class is used to do this job.
- Dispatch Action: Handles multiple operations in multiple methods. It is better to have one method per operation instead of merging the entire business logic in a single execute method of an action class.
- Look up Dispatch Action: It is same as dispatch action but recommended not to use.
- Switch Action: It is used to switch between different modules in struts application.
What is Dispatch Action class?
The dispatch action class also extends the basic action class. This class has an advantage over the action class that it does not has to explicitly override the execute method rather the developers are free to write their own methods. It enables the developers to use the same action class for multiple flows.
List some Struts tag libraries?
Some Struts tag libraries are:
- HTML Tags: used to create the struts input forms and GUI of web page.
- Bean Tags: used to access bean and their properties.
- Logic Tags: used to perform the logical operation like comparison.
- Template Tags: used to changes the layout and view.
- Nested Tags: used to perform the nested functionality.
- Tiles Tags: used to manage the tiles of the application.
Can you explain custom tag?
Custom Tags can be described as Java classes written by developers which can be used in the JSP using XML mark-up. Custom tags can be considered as class which acts as view helper beans that can be put into use without script lets which are Java code snippets mixed with JSP mark-up. JSP pages are developed and tweaked by page authors and cannot interpret the script lets. This blurs the separation of different duties to be performed in a project.
Explain how to work with error tags?
Working with error tags is not as complex as it looks. Error tags can be put in place by adding <html:errors/> in to JSP. Unlike others the error tag does not have a body it also does not consist of any attributes. The only function the error tag performs is to display the error in place where the error tag is placed using three features – the header, the body and the footer. The list of the error text to be displayed or written is represented by the error body.
What design patterns are used in struts?
Struts is based on model 2 MVC (Model View Controller) architecture. Struts controller uses the command design pattern and the action classes use the adapter design pattern. The process () method of the RequestProcessor uses the template method design pattern. Struts also implement the following J2EE design patterns.
- Service to Worker
- Dispatcher View
- Composite View (Struts Tiles)
- Front Controller
- View Helper
- Synchronizer Token
Explain struts.devMode?
The struts.devMode is used to make sure that framework is running in development mode or production mode by setting true or false. struts.devMode is set to false in production phase to reduce impact of performance. By default it is “false”. It is used because of the following reasons:
What is HTTP Forward?
HTTP Forward is the process used for displaying a page when requested by the end user. The user requests for a resource by clicking on a hyperlink or submitting a form and the next page is displayed to the user as a response.
What is HTTP Redirect?
HTTP Redirect is more complex as compared to HTTP direct. In this a user requests a resource and the responses are first sent to the user, which is not the requested resource.
What is purpose of execAndWait interceptor?
Struts2 provides execAndWait interceptor for long running action classes. We can use this interceptor to return an intermediate response page to the client and once the processing is finished, final response is returned to the client. This interceptor is defined in the struts-default package and implementation is present in ExecuteAndWaitInterceptor class
What is ActionMapping?
In action mapping is the mapping of the action performed by the user or client on the application.
What is DispatchAction?
The DispatchAction enable the programmer to combine together related function or class. It is using programmer can combine the user related action into a single UserAction. Like add user, delete user and update user.
What is the default suffix for Struts2 action URI ?
The default URI suffix for Struts2 action is .action, in Struts1 default suffix was .do. We can change this suffix by defining struts.action.extension constant value in our Struts2 configuration file as:
<constant name=”struts.action.extension” value=”action,do”></constant>
What are the pros of Struts 2?
- Simplified Design: Code is not tightly coupled to Struts framework or Servlet API.
- Easy plug-in: Developers can use other technologies plug-in easily. It includes SiteMesh, Spring, Tiles, etc.
- Simplified ActionForm: ActionForms are POJOs, we do not need to implement any interface or extend from any class.
- Annotations introduced: Use of annotation results in reduction in length and complexity of code. It is also used in configuration file for simplicity.
- Better tag features: It includes theme based tags and Ajax enabled tags.
- Simplified Testability: Unit testing of Struts 2 Action class is very easy because it doesn’t need complex HttpServletRequest and HttpServletResponse objects.
- Simplified Action: Similar to ActionForms, Actions are also simple POJOs and they do not need to implement any interface or extend any class.
- OGNL integration: It uses OGNL to fetch data from ValueStack and type conversion which reduces code.
- Ajax support: Struts2 tags are Ajax enabled.
- Multiple View options: View is not restricted to JSP. Freemarker, velocity templates can also be used as a view.
Can you explain Struts.properties in Struts2?
This configuration file provides a mechanism to change the default behavior of the framework. Actually all of the properties contained within the struts.properties configuration file can also be configured in the web.xml using the init-param, as well using the constant tag in the struts.xml configuration file. But if you like to keep the things separate and more struts specific then you can create this file under the folder WEB-INF/classes. The values configured in this file will override the default values configured in default.properties which is contained in the struts2-core-x.y.z.jar distribution.
What are the cons of struts 2?
Compatibility: Struts 2 is completely different from the Struts 1. So it’s difficult to perform migration of applications from Struts 1 to Struts 2.
Limited Documentation: Limited documentation is available for Struts 2. In addition to this, new users find it difficultly to understand its concepts due to poorly managed documentation by Apache.
Can you explain struts validator framework?
Struts Validator Framework enables us to validate the data of both client side and server side.
There are two types of validation supported in the struts framework:-
- Clint side validation (Using java script support and enabling java script in ur browser).
- Server site validation (using Validator method or ActionServlet class and).
There is another way which is defining the rules of validation in validation-rule.xml file.
When some data validation is not present in the Validator framework, then programmer can generate own validation logic, this User Defined Validation logic can be bind with Validation Framework.
Validation Framework consist of two XML configuration Files:
- Validator-Rules.xml file
- Validation.xml file
Explain how can we upload files in Struts2 application?
File Upload is one of the common tasks in a web application. Struts2 provides built in support for file upload through FileUploadInterceptor. This interceptor is configured in struts-default package and provides options to set the maximum size of a file and file types that can be uploaded to the server.
Explain integrate log4j in Struts2 application?
Struts2 provides easy integration of log4j API for logging purpose, all we need to have is log4j configuration file in the WEB-INF/classes directory.
List some bundled validators?
- requiredstring
- stringlength
- email
- date
- int
- double
- url
- regex