You are on page 1of 6

Struts MVC architecture

unit-7 Web Technologies

MVC architecture
This will present a simple JSP-architecture and also a handful of useful techniques that will help you build web-applications fast, without losing focus on quality. If you are building prototypes, smaller applications, or simply want to experiment with Javatechnologies, then this might be the architecture for you. The building blocks are JSPpages and JavaBeans in an MVC-architecture.

The MVC architecture


The Model-View-Controller architecture is today a de-facto standard used in thousands of applications. The main idea behind this architecture is to separate business logic, presentation and program flow in a simple manner. In JSP-applications you may implement the MVC-architecture by: The "C" (Controller): coding a controller that handles common tasks like authentication examines the requests from the user invokes JavaBeans controls error handling controls module flow The "M" (Model): coding a set of JavaBeans to handle the database access (or access to other external resources) The "V" (View): coding a view for presentation of the data fetched by the beans The controller is often programmed as a Java Servlet, but in this article we will use the simpler JSP-format, which is also commonly used for the view. Our application is therefore simplified by only containing JSP-pages and JavaBeans.

JSP Architecture
JSP is simply html pages with the jsp extension. In the JSP page programmers embeds Java code for generating dynamic content. JSP is mainly used to develop the view for web applications. Sometimes it also used for business logic, But in majority of cased it is used to create the different types of view for the applications. We the help of JSP you can create data entry forms, reports; dynamic pages to display some information top the user.
M.Gangappa, Assoc Prof, CSE, VNRVJIET

Struts MVC architecture

unit-7 Web Technologies

While developing the web applications two types of the architecture is used. These architectures are known as Model 1 and Model 2 architectures. So, in JSP there are two types of architecture of the JSP: 1. Model 1 Architecture 2. Model 2 Architecture JSP Model 1 Architecture In case of Model 1 architecture the request is processed by JSP or Servlet. The JSP or Servlet handles all the responsibility of request processing such as validating the data, business logic validation and processing, show error messages, finally generating the view for the application. JSP Model 1 architecture is good for very small application, but it's not a good solution for big enterprise application. The JSP Model 1 architecture do not separate the view, business processing logic and the view is tightly integrated with the business logics. Here is the diagram of JSP Model 1 architecture:

JSP Model 2 architecture The JSP Model 2 architecture is based on the popular MVC architecture. Here Model, View and Controller is responsible for specific work for making the architecture ideal for developing complex enterprise web applications. In JSP Model 2 architecture JSP is used for creating the view for the application. A centralized Servlet is used to handle all the requests for the application. The Servlet works as the controller for the application. It then uses the Java beans for processing the business logic and getting the data (Model) from the database. Finally it uses the JSP to render the view which is displayed to the user.

M.Gangappa, Assoc Prof, CSE, VNRVJIET

Struts MVC architecture Here is the diagram of JSP Model 2 architecture:

unit-7 Web Technologies

The MVC architecture is very popular architecture and there are many frameworks available for developing Java based web applications. MVC based frameworks are: Struts Jsf Spring MVC

Advantages of MVC:
1. Separating controller from view ----permits runtime selection of appropriate views based on workflow, user preferences or model view. 2. Separating controller from model ---- allows configurable mapping of user actions on the controller to application functions on model .

Consequences or benefits:
1. we make changes without bringing down the server. 2. we leave the core data alone. 3. we can have the multiple versions of the same data displayed. 4. we can test our changes in the actual environment .

M.Gangappa, Assoc Prof, CSE, VNRVJIET

Struts MVC architecture

unit-7 Web Technologies

Struts Framework
What Is the Struts Framework?
The Struts Framework is a standard for developing well-architected Web applications. It has the following features: Open source Based on the Model-View-Controller (MVC) design paradigm, distinctly separating all three levels: o Model: application state o View: presentation of data (JSP, HTML) o Controller: routing of the application flow Implements the JSP Model 2 Architecture Stores application routing information and request mapping in a single core file, strutsconfig.xml

The Struts Framework, itself, only fills in the View and Controller layers. The Model layer is left to the developer.

Architecture Overview

All incoming requests are intercepted by the Struts servlet controller. The Struts Configuration file struts-config.xml is used by the controller to determine the routing of the flow. This flows consists of an alternation between two transitions: From View to Action A user clicks on a link or submits a form on an HTML or JSP page. The controller receives the request, looks up the mapping for this request, and forwards it to an action. The action in turn calls a Model layer (Business layer) service or function.
4

M.Gangappa, Assoc Prof, CSE, VNRVJIET

Struts MVC architecture

unit-7 Web Technologies

From After the call to an underlying function or service returns to the action class, Action to the action forwards to a resource in the View layer and a page is displayed in View a web browser. The diagram below describes the flow in more detail:

1. User clicks on a link in an HTML page. 2. Servlet controller receives the request, looks up mapping information in strutsconfig.xml, and routes to an action. 3. Action makes a call to a Model layer service. 4. Service makes a call to the Data layer (database) and the requested data is returned. 5. Service returns to the action. 6. Action forwards to a View resource (JSP page) 7. Servlet looks up the mapping for the requested resource and forwards to the appropriate JSP page. 8. JSP file is invoked and sent to the browser as HTML. 9. User is presented with a new HTML page in a web browser.

M.Gangappa, Assoc Prof, CSE, VNRVJIET

Struts MVC architecture

unit-7 Web Technologies

Struts Components
The Controller
This receives all incoming requests. Its primary function is the mapping of a request URI to an action class selecting the proper application module. It's provided by the framework.

The struts-config.xml File


This file contains all of the routing and configuration information for the Struts application. This XML file needs to be in the WEB-INF directory of the application.

Action Classes
It's the developer's responsibility to create these classes. They act as bridges between user-invoked URIs and business services. Actions process a request and return an ActionForward object that identifies the next component to invoke. They're part of the Controller layer, not the Model layer.

View Resources
View resources consist of Java Server Pages, HTML pages, JavaScript and Stylesheet files, Resource bundles, JavaBeans, and Struts JSP tags.

ActionForms
These greatly simplify user form validation by capturing user data from the HTTP request. They act as a "firewall" between forms (Web pages) and the application (actions). These components allow the validation of user input before proceeding to an Action. If the input is invalid, a page with an error can be displayed.

Model Components
The Struts Framework has no built-in support for the Model layer. Struts supports any model components: JavaBeans EJB CORBA JDO any other

M.Gangappa, Assoc Prof, CSE, VNRVJIET

You might also like