Model View Controller architecture can be defined as both a design pattern and an architectural pattern used in software engineering for developing interactive web applications (Prajwal Tuladhar 15 oct 2008). Model- View- Controller architecture divides the application components into three different parts each with a unique responsibility and independent of the other component. The three major components are:
Model: This represents the core database code and business logic of the application. It provides data from the database and saves the data into a data store. The Model is involved in data validation as well as data access.
View: This represents User Interface of the application. It is responsible for receiving request from the browser and delegating it to the controller. It also receives responses from the controller and display it to the user.
Controller: This acts as a gateway between the View and the Model. It receives requests from the view and communicates with the Model to return the required View. It represents the navigational code.
MVC design pattern was used because of the following reasons:
MVC pattern makes the application more expressive and easy to follow
Modularity: Since MVC has three components, it provides a clearer separation of the business logic and data from the presentational data thereby reducing the complexity of the application. Modularity also allow easier changes to any component of the application without changing other as the program is not coupled to other aspects, reducing nasty debugging situations. For example, if you want to change databases like from MySQL to Oracle, the Model is only the one to be altered.
Separation of data from presentation is something that most experienced developers strive for, but often, this emerges out of trial and error and not a deliberate plan. MVC enforces the separation of these portions of an application. While it requires extra effort up front to architect an MVC application, the payoffs are impressive.
Multiple Views: MVC provides the ability to have more than one views that all relies on a single Model. This allows the creation of more scalable and modular application that can display the state of the applications Model in different ways. This eliminates code duplication
Greater control over URLs: MVC based frameworks provides URL routing facilities that gives greater control over URL.
Code Reuse and easier maintenance: MVC architecture supports the reuse and easier maintenance of the application as it decouples objects.
Test Driven Development: Testing highly interdependent component especially user interface is difficult and often requires complex setup to test a single component. However, since MVC allows the separation of concerns into three components, it makes it easier to test each component individually and easier to identify the component with errors when a problem occurs.
STRUTS MVC ARCHITECTURE
The MVC architecture divides the application components into three different sections, the Model, the View and the Controller. It solves many of the problems and challenges faced by software developers by providing a clear separation of the business logic and application data from the presentation data.
Struts Model component
The Model component encapsulates the business data and business logic of the application. It represents the actual state of the application and it is aware of all the data and how they need to be displayed in the browser. The Model is isolated from the view and the controller and provides interface to the database.
Struts support Model components like Enterprice Java Beans (EJBs), Java beans, CORBA or JDO. Java classes or beans were used as Model component based on the user requirements.
Struts View component
Java Server Pages (JSP) were used for the View presentation and Java script and custom tags to extends its functionality
Struts Controller component
The Controller component acts as a bridge between the View and the Model. It receives requests from the View and passes it to the Model which in turn executes the appropriate business logic. The Controller will then direct the response to the View component. The Controller components used includes ActionServlet, Action class, ActionForm and struts-config.xml. The ActionServlet handles requests made by user or web browser by intercepting the URL and forwards handing of the request to the Action class based on the Struts configuration file. The Action class is the part of the controller that communicates with the Model.
OVERVIEW OF STRUTS FRAMEWORK
Figure 4-1 MVC and the Struts framework
Struts's Controller which is the ActionServlet acts as a gateway between the View and the applications's Model and invokes an Action class when a HTTP request is received from a browser. This Action class then communicates with the Model (backend business logic) and updates the state of the application respectively. The ActionForm class or bean enables the transfer of data between the View and the Model.
The Struts Framework was used basically because it is based on the MVC pattern separating business logic and presentation logic and was used to give modularity and loose coupling in the whole Application.
WEB SERVICE INTERFACE DEVELOPMENT
The Struts framework encloses browser request with parameters in an org.apache.struts.action.ActionFormÂ bean.
These were used basically to capture user data during an HTTP request for form validation. ActionForm beans enables validation of user inputs before they are passed to the Action. An error page will be displayed if the data entered in the form was incorrect. They were also used to pre-populate forms with default values obtained from the database.
Struts provides a Controller which is the ActionServlet (org.apache.struts.action.ActionServletÂ ), acts as a gateway between the View and the application's Model and invokes an Action class when a HTTP request is received from a browser (Mark McLaren). It maps a URL request to an appropriate Action class using the Struts configuration file struts-config.xml.
The struts-config.xml file has all the information for the routing and configuration for the struts application (Exadel, Inc., 2004). The XML file have to be placed in the WEB-INF directory of the application.
All URL request that ends with .do (example, *.do) in the application were mapped to the ActionServlet in the web deployment descriptor in order to configure the application to pass all browser requests to the ActionServlet. (http://www.ibm.com/developerworks/websphere/techjournal/0302_fung/fung.html)
This is a basic Java class or bean. During a HTTP request, the Action bean is invoked by the ActionServlet. The Action bean communicates with the Model (backend business logic) and updates the state of the application respectively. It intercepts the request and returns an Action Forward object which shows the next component to invoke (Mark McLaren).
SYSTEM DEVELOPMENT TOOLS
J2EE (SDK & JRE)
MYSQL SERVER 5.1
MYSQL JDBC CONNECTOR
APACHE TOMCAT WEB SERVER
2) SERVLETS / JSP
The software was developed using a web application framework called Struts. Struts is open source and is maintained as part of Apache Jakarta project .The Struts is a framework used for developing J2EE and java-based web applications based on the Model-View-Controller (MVC) design pattern (Malcolm G. Davis, 2001).
Struts extend Java Servlet API to encourage the implementation of MVC architecture which allows the programmer to concentrate more on the business logic and less on plumbing. It consists of a set of java classes, servlet, beans and JSP tags used for creating complex and reusable, multi-tier Enterprise-level web applications. The Struts Framework provides its own web controller component that can handle the control-flow logic and can integrate with other technologies to provide the View and the Model (Roseindia, 2010). In this project, for the View, Struts was integrated with Java Server Pages (JSP) and XSLT while for the Model, it uses Java beans (EJB) and JDBC for data access as well as Hibernate.
Components of Struts
Struts has three major components categorised based on its MVC architecture (Keld H. Hansen)
A controller acting as a request handler used for mapping a request URI to a particular Action class
Response handler usually a collection of java beans and other helper classes used for transferring control to other resources to complete a response
A collection of tag library that can be used in JSP pages to create an interactive web application.
ADVANTAGES OF STRUTS
There are a lot of open source web application development frameworks available today which offers their own unique features to create an extensible development framework to help developers meet the demands of today's public web and enterprise applications. However, Struts Framework was used for this application solely because it combines Java Server Pages, Java Servlets, message resources, and custom tags into a common framework and also provides a readymade implementation of the MVC design pattern. Additionally, Struts is a robust, extensible and flexible framework. Other Benefits includes:
Centralized file-based configuration: Struts enhances loose coupling in an application by representing values in XML or property files. This allows imply that large changes can be made on the application by simply editing a single file. These changes can be done without modifying or recompiling the Java codes. This makes the developer concentrate on the business logic to satisfy user requirements.
Form beans: Struts enables automatic population of JavaBeans component based on the request parameters received. This can be done by setting property="*" with jsp:setProperty.
Bean tags: with Struts, the developer can output the properties of a Java beans easily using a set of custom JSP tags like bean:write.
HTML tags: Struts enables the creating of HTML forms that are associated with JavaBeans components that allows the developer to get initial form values from Java objects and redisplay the forms using (maintaining) values previously entered. This form redisplay is complex and poorly supported in the standard Servlet and JSP API. However, Struts provides an easier way to do so.
Consistent approach: Struts enforces consistent use of MVC throughout the application.
Plumbing code(contained within the struts framework): Struts framework handles mapping of HTTP request parameter to Java objects relieving the programmer from doing this and allowing him to focus more on the business domain instead of the building infrastructure.
Good documentation: Struts provides a good documentation for future improvement and maintenance.