When it comes to discuss web application the first thing come into mind is client and server architecture. Client is the entity which always request to the server by sending some arguments or page request in response server receive the request, process it and return the pure html to the browser.
Interaction of user with server is done through browsers which only understand Html, processing of dynamic pages which involve languages such as asp, jsp, C# and etc are processed at server side.
From last few years web application programmers working to develop framework to improve the journey from client to server. Focus is mainly in web application areas like optimization, security, database support, platform, integration with other technologies, object oriented programming model, eliminate dependency and many more.
This tutorial discuss struts the popular implementation of model-view-controller pattern, benefits and features.
1 Jakarta Struts
There are number of technologies available to develop static and dynamic web pages but programmer always looking for efficient solution. Jakarta struts now a days is most demanding framework for developing complex website.
A Jakarta strut is open source framework encompassed rich features, libraries and components to make the life of web developers easy. Struts organize and manage different components of java to improve the performance of web applications. Struts framework developed on Model-View- Controller (MVC) pattern.
2 MVC Pattern
The pattern is the solution of the problems, MVC pattern purpose to consolidate the underlying framework on which web application are developed. MVC divides the functionality into three layers
The model is responsible for handling intelligence of web application, business logic, rules and data access functionality.
The presentation layer interacts directly with the user for getting request and displaying response. There can be multiple views for web application passing request to proper model and returning
Controller as name suggest establish the interaction between model and view layers. This layer gets requests from presentation and forward to the designated model component to process the request and get back the results to the view.
There can be multiple views for web application passing request to proper model and returning results to the right view is done by controller.
The three layers are not depend on each other in other word they are loosely coupled, change in one layer not effect the other layer. This model reduces programming hours and complexity and motivates the reusability.
3 Benefits of MVC
3.1 Proper Structuring
Multiple layers unified different programming components
3.2 Eliminate dependency
Change in one layer not impact on other layer, every portion has its own implementation.
3.3 Concurrent programming
As there are not dependency programmer can work at the same time on each layer.
3.4 Ease of maintenance
The structure and flow of the application are clearly defined, making them easier to understand and modify. Parts are loosely coupled with each other.
4 Jakarta Struts Implementation of MVC
Jakarta Struts is the implementation of MVC framework although there are no specific rule of thumb for view and model; programmers can mold it according to the nature of application requirements.
The controller implementation of strut is concrete bind model and view to organize components and maintain the flow of application
4.1 The Struts Controller
Struts implementation complies with the MVC model provide the solid connection with model and view part of the web application. The implementation of controller in struts mainly handled by java servlet named as action servlet. The action servlet is responsible for interaction with multiple views of the application which may have different implementations.
The communication configuration between view and controller defined in struts configuration file. The incoming URL requests forwarded to controller action servlet and which can further send to business logic, another view or database. The java beans contain the request information or response information returns the results to the action class which invokes the related handler to display result to the right view.
4.2 The Struts View
As discussed earlier, struts view is not dependent on any specific technology for presentation layer implementation. Many struts application use JSP (Java Server Pages) along with the struts tag library, JSTL (Java Standard Tag Library) and JSF (Java Server Faces).
Some other possibilities are Apache Cocoon, Jakarta Velocity templates and XSLT (extensible style sheet language Transformation)
4.3 The Struts Model
Struts model view are not bound to constraint, programmer can implement this layer according to requirements but the most appropriate way to encapsulate application operations and data into java beans.
The java beans are nothing but a class which variables can be accessed to getter and setter methods of class properties. It can be implemented in same class on different class.
The operations are rules for business entity and data are the database values residing in the application database. The interaction between controller and model lies in the code, the action sub class initiates any action required to handle user’s request thereby creating the connection with the model.
In response java beans pass some data or complete data to the view through action form bean.
4.4 The Struts Configuration File
The configuration file in struts is not part of MVC model, view or controller but it plays a vital role in the interaction of layers. Configuration file define connection between view and controller to decide which sub action to be used in circumstances and which action form to call for the sub action class.
Struts deals with multiple views, configuration file also select the view to be returned to the user. Changes in configuration file not require any modification in model, view or controller.
<form-bean name=”userForm” type=”org.apache.struts.validator.DynaValidatorForm”>
<form-property name=”user” type=”org.appfuse.model.User”/>
<action path=”/user” type=”org.springframework.web.struts.DelegatingActionProxy”
name=”userForm” scope=”request” parameter=”method” validate=”false”>
<forward name=”list” path=”/userList.jsp”/>
<forward name=”edit” path=”/userForm.jsp”/>
5 Struts Features
Struts framework developed in Java, which is one of the well known and commonly used programming platforms.
Developers most of the time looking for the frameworks supports Ajax. Struts has best support with Ajax through Dojo built-in, plug-ins for GWT, JSON
5.3 MVC framework
Struts purely comply with MVC framework that makes it most attractive development framework for web application development
5.4 Testing Framework
Struts 2 allow easy testing with mocks e.g. EasyMock, jMock, Spring Mocks and it can also StrutsTestCase.
5.5 Form Validation Framework
Validation should be easy to configure, be robust on the client side and either provide good out of the box messages or allow you to easily customize them. Struts 2 uses OGNL for powerful expressions – client side only works when specifying rules on Actions
5.6 Integrated Development Environment
Struts has a lot of IDE support and even has frameworks built on top of it (i.e. Beehive’s Page Flow)
5.7 Spring Integration
Struts framework support integration with spring by using ContextLoaderPlugin and Support classes
5.8 Bookmarking and URLs
Using container-managed authentication (or other filter-based security systems) allows users to bookmark pages. They can click the bookmark, login and go directly to the page. Struts allow full URL support.