Struts 2 Interview Questions and Answers
This section covers frequently asked Struts 2 interview questions.
1. What is Struts?
Struts is a popular open-source framework for developing Java web applications using the Model-View-Controller (MVC) design pattern. Struts 2 is based on the WebWork framework.
2. Struts 1 vs. Struts 2.
Feature | Struts 1 | Struts 2 |
---|---|---|
Action Class | Must extend an abstract class | Plain Old Java Object (POJO) |
Front Controller | ActionServlet |
StrutsPrepareAndExecuteFilter |
Request Processing | RequestProcessor |
Interceptors |
View Technology | Primarily JSP | JSP, FreeMarker, Velocity |
Configuration File | [anyname].xml in WEB-INF |
struts.xml in the classpath |
3. Features of Struts 2.
- MVC architecture
- POJO-based actions
- AJAX support
- Rich tag library
- Various result types
- Integration with other frameworks
4. What is MVC? (Repeated from earlier section).
MVC (Model-View-Controller) is a design pattern separating application logic into three parts: Model (data), View (presentation), and Controller (handling user input).
5. What is an Interceptor?
An interceptor is a pluggable component invoked before and/or after a request is processed. It's used for cross-cutting concerns (like logging, security, etc.).
6. Interceptor Lifecycle Methods.
init()
intercept(ActionInvocation invocation)
destroy()
7. ValueStack.
The ValueStack is a data structure holding the action and other model objects.
8. ActionContext.
ActionContext
is a container for request-specific data.
9. ActionInvocation.
ActionInvocation
invokes the action and manages interceptors.
10. OGNL (Object-Graph Navigation Language).
OGNL is Struts 2's expression language for accessing and manipulating objects.
11. Constants of the Action
Interface.
SUCCESS
ERROR
INPUT
LOGIN
NONE
12. params
Interceptor.
The params
interceptor pushes request parameters onto the ValueStack.
13. execAndWait
Interceptor.
execAndWait
handles asynchronous operations and waits for the result.
14. modelDriven
Interceptor.
modelDriven
makes a model object the default object on the ValueStack.
15. validation
Interceptor.
validation
performs input validation.
16. Bundled Validators.
(This section lists built-in Struts 2 validators.)
17. Plain Validator vs. Field Validator.
A plain validator can be applied to multiple fields. A field validator applies to a specific field.
18. jsonValidation
Interceptor.
jsonValidation
supports asynchronous validation using JSON.
19. Aware Interfaces.
Aware interfaces (ServletRequestAware
, ServletResponseAware
, SessionAware
, ServletContextAware
) provide access to servlet-related objects.
20. i18n
Interceptor.
The i18n
interceptor handles internationalization (localization).