Вы находитесь на странице: 1из 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 Java- technologies, then this might be the architecture for you. The building blocks are JSP- pages 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 authenticationby: The "C" (Controller): coding a controller that examines the requests from the user invokes JavaBeans

examines the requests from the usera controller that handles common tasks like authentication invokes JavaBeans controls error handling controls module

invokes JavaBeanslike authentication examines the requests from the user controls error handling controls module flow The "M"

controls error handlingexamines the requests from the user invokes JavaBeans controls module flow The "M" (Model): coding a

controls module flowfrom the user invokes JavaBeans controls error handling The "M" (Model): coding a set of JavaBeans

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

1

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:

Struts MVC architecture

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:

logics. Here is the diagram of JSP Model 1 architecture: JSP Model 2 architecture The JSP

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

2

Struts MVC architecture

Here is the diagram of JSP Model 2 architecture:

unit-7

Web Technologies

diagram of JSP Model 2 architecture: unit-7 Web Technologies The MVC architecture is very popular architecture

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

StrutsJava based web applications. MVC based frameworks are: Jsf Spring MVC Advantages of MVC: 1. Separating

Jsfbased web applications. MVC based frameworks are: Struts Spring MVC Advantages of MVC: 1. Separating controller

Spring MVCbased web applications. MVC based frameworks are: Struts Jsf Advantages of MVC: 1. Separating controller from

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

3

Struts MVC architecture

Struts Framework

What Is the Struts Framework?

unit-7

Web Technologies

The Struts Framework is a standard for developing well-architected Web applications. It has the following features:

Open sourceWeb applications. It has the following features: Based on the Model-View-Controller (MVC) design paradigm,

Based on the Model-View-Controller (MVC) design paradigm, distinctly separating all three levels:Web applications. It has the following features: Open source o Model: application state o View: presentation

o

Model: application state

o

View: presentation of data (JSP, HTML)

o

Controller: routing of the application flow

Implements the JSP Model 2 ArchitectureHTML) o Controller: routing of the application flow Stores application routing information and request mapping

Stores application routing information and request mapping in a single core file, struts- config.xmlthe application flow Implements the JSP Model 2 Architecture The Struts Framework, itself, only fills in

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

Architecture Overview

Model layer is left to the developer. Architecture Overview All incoming requests are intercepted by the

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.

M.Gangappa, Assoc Prof, CSE, VNRVJIET

4

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:

The diagram below describes the flow in more detail: 1. User clicks on a link in

1. User clicks on a link in an HTML page.

2. Servlet controller receives the request, looks up mapping information in struts- config.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

5

Struts MVC architecture

Struts Components

The Controller

unit-7

Web Technologies

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:

JavaBeansfor the Model layer. Struts supports any model components: EJB CORBA JDO any other M.Gangappa, Assoc

EJBModel layer. Struts supports any model components: JavaBeans CORBA JDO any other M.Gangappa, Assoc Prof, CSE,

CORBAthe Model layer. Struts supports any model components: JavaBeans EJB JDO any other M.Gangappa, Assoc Prof,

JDOModel layer. Struts supports any model components: JavaBeans EJB CORBA any other M.Gangappa, Assoc Prof, CSE,

any otherfor the Model layer. Struts supports any model components: JavaBeans EJB CORBA JDO M.Gangappa, Assoc Prof,

M.Gangappa, Assoc Prof, CSE, VNRVJIET

6