0% found this document useful (0 votes)
113 views4 pages

Struts 2

Apache Struts 2 is a framework for building Java web applications based on the MVC pattern. It implements the MVC roles with actions representing the model, results representing the view, and a filter dispatcher representing the controller. Actions are executed through interceptors that can run logic before and after the action. The framework uses OGNL and a value stack to transfer data between the model and view.

Uploaded by

nizax91
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views4 pages

Struts 2

Apache Struts 2 is a framework for building Java web applications based on the MVC pattern. It implements the MVC roles with actions representing the model, results representing the view, and a filter dispatcher representing the controller. Actions are executed through interceptors that can run logic before and after the action. The framework uses OGNL and a value stack to transfer data between the model and view.

Uploaded by

nizax91
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Apache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications.

The framework is designed to streamline the full development cycle, from building, to deploying, to maintaining applications over time. Apache Struts 2 was originally known as WebWork 2.

Basic MVC Architecture


Model View Controller or MVC as it is popularly called, is a software design pattern for developing web applications. A Model View Controller pattern is made up of the following three parts: Model - The lowest level of the pattern which is responsible for maintaining data. View - This is responsible for displaying all or a portion of the data to the user. Controller - Software Code that controls the interactions between the Model and View. MVC is popular as it isolates the application logic from the user interface layer and supports separation of concerns. Here the Controller receives all requests for the application and then works with the Model to prepare any data needed by the View. The View then uses the data prepared by the Controller to generate a final presentable response. The MVC abstraction can be graphically represented as follows.

The model The model is responsible for managing the data of the application. It responds to the request from the view and it also responds to instructions from the controller to update itself. The view A presentation of data in a particular format, triggered by a controller's decision to present the data. They are script based templating systems like JSP, ASP, PHP and very easy to integrate with AJAX technology. The controller The controller is responsible for responding to user input and perform interactions on the data model objects. The controller receives the input, it validates the input and then performs the business operation that modifies the state of the data model. Struts2 is a MVC based framework. In the coming chapters, let us see how we can use the MVC methodology within Struts2.

Struts 2 FrameWork Tutorial


A framework tries to automate the common tasks and provides a platform for the users to build applications quickly. Struts 2 is based on the OpenSymphony Web Works Framework. Struts 2 framework implements the Model-View-Controller (MVC) design pattern. In Struts 2 the model, view and controller are implemented by the action, result and FilterDispatcher respectively. The controller's job is to map the user request to appropriate action. In Struts 2 FilterDispatcher does the job of Controller. Model contains the data and the business logic. In Struts 2 the model is implemented by the Action component. View is the presentation component of the MVC Pattern. In Struts 2 the view is commonly implemented using JSP, Velocity Template, Freemaker or some other presentation-layer technology.

The controller receives the user request and determine which Struts 2 action to invoke. The framework creates an instance of this action and associate it with the newly created instance of the ActionInvocation. In Struts 2 the invocation of action should pass through a series of interceptors as defined in the application's XML file. The framework calls the ActionInvocations invoke() method to start the execution of the action. Each time the invoke() method is called, ActionInvocation consults its state and executes whichever interceptor comes next. ActionInvocation hands control over to the interceptor in the stack by calling the interceptors intercept() method. The intercept() method of the interceptor inturn calls the invoke() method of the ActionInvocation till all the interceptors are invoked, in the end the action itself will be called and the corresponding result will be returned back to the user. Some interceptor do work before the action is executed and some do work after the action is executed. It's not necessary that it should do something each time it is invoked. These interceptors are invoke both before and after the action. First all the interceptors are executed in the order they are defined in the stack.

Then the action is invoked and the result is generated. Again all the interceptors present in the stack are invoked in the reverse order. The other important features of Struts 2 are OGNL and ValueStack. Object-Graph Navigation Language (OGNL) is a powerful expression language that is used to reference and manipulate data on the ValueStack. OGNL help in data transfer and type conversion. OGNL expression language provides simplified syntax to reference java objects. OGNL is used to bind the java-side data properties to the string-based view layer.

In Struts 2 the action resides on the ValueStack which is a part of the ActionContext. ActionContext is a global storage area that holds all the data associated with the processing of a request. When a request comes the params interceptor helps in moving the request data to the ValueStack. Now the OGNL does the job of converting the string based form data to their corresponding java types. OGNL does this by using the set of available built-in type converters. Again when the results are generated the OGNL converts the java types of the property on the ValueStack to the string-based HTML output. ActionContext is thread local which means that the values stored in the ActionContext are unique per thread, this makes the Struts 2 actions thread safe.

Understanding MVC Design Pattern


The MVC (Model View Controller) design pattern is used by the struts for building an applications. It makes the application more maintainable. The main aim of using this design pattern is to separate the business data from the presentation of the users. This design pattern is very flexible and allows the users (developers) to extend their meet for specific project. The MVC architecture contains the three parts they are Model, View, Controller.

Model - This components contains the application data which is represented by the view. Those data which is part of persistent state must reside in model object. When the state of data changes it notify the view. The controller access the model object data for effecting their state change. It represents and maintains the application state. View - The view represents the state of the model to the user. It is actually represents the application data to the user and also takes the data from the user and send to the controller. There is no any business logic, flow logic inside the view it contains tags. The view renders the model data for presenting to the user. An application can contain many view. Controller - The controller is responsible for controlling entire application. It accepts the input coming from the view, translates into the action to be performed by the view. The controller is only responsible for accessing model and and rendering it to the various user interfaces. Simply you can say a controller accepts the data from the client, performs the business operation, and return it to the client. The flow of data in the application is controlled by the controller.It it responsible for forwarding the request to the appropriate handler.

You might also like