Struts 2

1. What is Struts 2 ?

Apache Struts 2 is a brand-new, state-of-the-art, open source web application framework. Struts 2 is not just a new release of the older Struts 1 framework. It is completely a new framework, based on the esteemed OpenSymphony WebWork framework.

Struts 2 is a second-generation web application framework that implements the Model-View-Controller (MVC) design pattern.

2. Why was Struts 2 built ?

At some point, the Struts community became aware of the limitations in the Struts 1 framework. The new framework, Struts 2 introduces several new architectural features that make the framework cleaner and more flexible. These new features include interceptors for layering cross-cutting concerns away from action logic; annotation-based configuration to reduce or eliminate XML-based configuration; a powerful expression language (EL), Object-Graph Navigation Language (OGNL), that transverses the entire framework; and a mini-MVC–based tag API that supports modifiable and reusable UI components.

3. What is the purpose of MVC ?

The MVC pattern provides a separation of concerns that applies well to web applications. Separation of concerns allows us to manage the complexity of large software systems by dividing them into high-level components. The MVC design pattern identifies three distinct concerns: model, view, and controller.

4. What is FilterDispatcher in Struts 2 ?

The role of a front controller is played by FilterDispatcher and is the first component to act in the processing. This important object is a servlet filter that inspects each incoming request to determine which Struts 2 action should handle the request. The framework handles all of the controller work for us. We just need to inform the framework which request URLs map to which of our actions. We can do this with XML-based configuration files or Java annotations.

5. How does Struts 2 become a zero-configuration web application ?

We can derive all of application’s metadata, such as which URL maps to which action, from convention rather than configuration. The use of Java annotations plays an important role
in this zero-configuration scheme. While zero-configuration has not quite been achieved in Struts 2, we can currently use annotations and conventions to drastically reduce XML-based configuration.

6. What are interceptors in Struts 2 ?

Interceptors are components that execute both before and after the rest of the request processing, i.e., an action. Some interceptors only do work before the action has been executed, and others only do work afterward. The important thing about interceptor is that the interceptor allows common, cross-cutting tasks to be defined in clean, reusable components that can be kept separated from your action code.

7. What kinds of work should be done in interceptors ?

Logging is a good example of interceptor. Logging should be done with the invocation of every action, but it probably should not be put in the action itself.

8. Why is interceptor not put in Struts 2 action ?

It is not a part of the action’s own unit of work. Earlier, Struts 1 framework had the responsibility of providing built-in functional solutions to common domain tasks such as data validation, type conversion, and file uploads. Struts 2 uses interceptors to do this type of work. While these tasks are important, they are not specifically related to the action logic of the request. Struts 2 uses interceptors to both separate and reuse these cross-cutting concerns.

9. What is ValueStack in Struts 2 ?

ValueStack is a storage area that holds all of data associated with the processing of a request. Data is moved to the ValueStack in preparation for request processing, it is manipulated there during action execution, and it is read from there when the results render their response pages.

10. What is OGNL in Struts 2 ?

OGNL (Object-Graph Navigation Language) is a powerful expression language or more that is used to reference and manipulate properties on the ValueStack.

11. What does an ActionContext contain in Struts 2 ?

The ActionContext contains all of the data that makes up the context in which an action occurs. This includes the ValueStack but also includes stuff the framework itself will use internally, such as the request, session, and application maps from the Servlet API.

12. How to declare FilterDispatcher in web.xml in Struts 2 ?

Make the following entry in deployment descriptor file, web.xml

13. How to tell Struts 2 FilterDispatcher to find the annotation-based Java actions ?

Make the following entry in deployment descriptor file, web.xml

14. How to run Struts 2 application in development mode ?

Use the below constant attribute in struts.xml file

15. What taglib is used for Struts 2 UI tags ?

Use below tag at top of the JSP page.

16. What is default package in Struts 2 ?

The struts-default is the default package, defined in the system’s struts-default.xml file, declares a huge set of commonly needed Struts 2 components ranging from complete interceptor stacks to all the common result types. The struts-default package that it defines contains common architectural components that all developers can reuse simply by having their own packages extend it.

17. Does action class require to implement Action interface in Struts 2 ?

Struts 2 actions do not have to implement the Action interface. Any object can informally honor the contract with the framework by simply implementing an execute() method that returns a control string.

18. What does an Action interface contain in Struts 2 ?

The com.opensymphony.xwork2.Action interface contains only one method, i.e., String execute() throws Exception.

Apart from the above method Action interface also provides some useful String constants that can be used as return values for selecting the appropriate result. The constants defined by the Action interface are

These constants can conveniently be used as the control string values returned by our execute() method. The true benefit is that these constants are also used internally by the framework.

19. What is ActionSupport class in Struts 2 ?

The ActionSupport class, a convenience class that provides default implementations of the Action interface and several other useful interfaces, giving us such things as data validation and localization of error messages.

20. What is DefaultWorkflowInterceptor in Struts 2 ?

ActionSupport implements two interfaces that coordinate with one of the interceptors from the default stack, the DefaultWorkflowInterceptor, to provide basic validation.

21. Where to put validation in Struts 2 ?

Validation logic should be placed in validate() method. This method is exposed via the com.opensymphony.xwork2.Validateable interface. Actually, ActionSupport implements the validate() method, but we have to override its empty implementation with our own specific validation logic.

22. How to create and store errors in Struts 2 ?

Use below example

23. What is FileUpload-Interceptor in Struts 2 ?

Struts 2 provides built-in help for file uploading. The default interceptor stack includes the FileUpload-Interceptor.

24. What does FileUpload-Interceptor do in Struts 2 ?

FileUpload-Interceptor processes a multipart request and transforms the file itself, along with some metadata, into request parameters. The request parameters exposed by FileUpload-Interceptor are

File—the uploaded file itself
String—the content type of the file
String—the name of the uploaded file, as stored on the server

25. What are execution cycle of an Interceptor in Struts 2 ?

An interceptor has a three-stage, conditional execution cycle:

Do some preprocessing.

Pass control on to successive interceptors, and ultimately the action, by calling invoke(), or divert execution by itself returning a control string.

Do some postprocessing.

26. Give a code example of an Interceptor in Struts 2 ?

27. What does happen in preprocessing phase in Struts 2 ?

During the preprocessing phase, the interceptor can be used to prepare, filter, alter, or otherwise manipulate any of the important data available to it. This data includes all of the key objects and data, including the action itself, that pertain to the current request.

28. What does happen when invoke() method is called on interceptor in Struts 2 ?

When an interceptor determines that the request processing should not continue, it can return a control string rather than call the invoke() method on the ActionInvocation. In this manner, it can stop execution and determine itself which result will render.

29. What does happen in postprocessing phase in Struts 2 ?

Even after the invoke() method returns a control string, any of the returning interceptors can arbitrarily decide to alter any of the objects or data available to them as part of their postprocessing.

30. What is an ActionContext in Struts 2 ?

The ActionContext helps clean things up by providing the notion of a context for the execution of an action. By context we mean a simple container for all the important data and resources that surround the execution of a given action.

31. What does an ActionContext hold in Struts 2 ?

parameters – Map of request parameters for this request
request – Map of request-scoped attributes
session – Map of session-scoped attributes
application – Map of application-scoped attributes
attr – Returns first occurrence of attribute occurring in page, request, session, or application scope, in that order
ValueStack – Contains all the application-domain–specific data for the request

32. Which operator is used in OGNL expression in Struts 2 ?

# operator is used in OGNL expression for evaluation. For example, if you want to retrieve a username from session in JSP page then use #session[‘username’].

33. How does data move through Struts 2 framework ?

When Struts 2 receives a request, it immediately creates an ActionContext, a ValueStack, and an action object. As a carrier of application data, the action object is quickly placed on the ValueStack so that its properties will be accessible via OGNL.

34. How many categories of tags are there in Struts 2 ?

There are mainly four categories of tags in Struts 2. They are data tags, control-flow tags, UI tags, and miscellaneous tags.

35. What is the purpose of data tags in Struts 2 ?

Data tags focus on ways to extract data from the ValueStack and/or set values in the ValueStack. Data tags are property, set, push, bean, and action tags.

36. What is the purpose of control-flow tags in Struts 2 ?

Control-flow tags give you the tools to conditionally alter the flow of the rendering process; they can even test values on the ValueStack. Control-flow tags are iterator, if, else, elseif.

37. What is the purpose of UI tags in Struts 2 ?

UI tags are used to design User Interface. As most of the work of an HTML user interface is accomplished by forms and form elements, many of the UI component tags correlate to HTML elements.

38. What is the purpose of miscellaneous tags in Struts 2 ?

Miscellaneous tags include useful functionality as managing URL rendering and internationalization of text. Miscellaneous tags are include, URL, i18n, text, param.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.