Struts Framework

Contents
• • •

J2EE
Java’s enterprise application specification

MVC
The Model View Controller design pattern

Struts
The framework based on MVC

J2EE

The Specification

Introduction
• •

J2EE is Java’s specification for enterprise application development It cover two major components
Web Components • For designing presentation layer Distributed Components • For developing business components

Cont…
• • •

Sun has provided the specification for developing components The structure or flow of the application is all up to the developer In J2EE scenario the application is segregated into three layers
Presentation Business Logic Integration

The J2EE Scenario

Client

HTML, JSP, Servlet

Java Beans, Session Beans, Message Driven Beans

DAO, Entity Beans, Hibernate

External Resources

Presentation

Business Logic

Integration

Advantages
• • • •

J2EE Scenario gives extensibility benefits The application is loosely coupled Presentation, Business Logic and Integration is independent of each other Changes if needed in either of this will not affect other parts of the application

MVC

Design Pattern

Introduction
• • • • •

Model View Controller It’s a design pattern given by Sun Microsystems. It is a widely used software design pattern It brings about better organization and code reuse It has become the recommended pattern for J2EE platform

MVC
Model
•Encapsulates application state •Responds to state queries •Exposes application functionality •Notifies views of changes State query

Change notification

State change

View
User requests •Renders the models •Requests updates from models •Sends user request to View Selection controller •Allows controller to select view

Controller
•Defined application behavior •Maps user actions to model •Select view for response •One for each functionality

Model Tier
• • •

Model represents Business data and rules Business rules is for changing business data Different parts of the application or even other different applications can share the same Model The model code is independent of View or Controller

View Tier
• • • •

View is what the end-user sees View displays the model data to the user View should never modify the model data directly nor it should interact with rules Different types of views (eg. GUI, Web client) can have the same model

Controller Tier
• • • • •

You can think of Controller as a mediator between View and Model It defines the flow of the application It interprets user requests/events Different type of clients require different type of Controllers It parses the user request/event into actions it performs on the Model

MVC Architecture
• • •

In MVC design pattern, application flow is mediated by a central Controller The Controller delegates requests to an appropriate handler The handlers are tied to a Model, and each handler act as an adapter between the request and the Model The Model represents business logic or state of an application

Cont…

• •

Control is usually then forwarded back through the Controller to the appropriate View The forwarding can be determined by consulting a set of mapping usually loaded from a configuration file This provides a loose coupling between the View and Model This makes application significantly easier to create and maintain

MVC Models

MVC has 2 models
Model I • Model, View + Controller Model II • Model, View, Controller

Lets try to look at them in a web application environment

Model I

Client

JSP
View + Controller

Java Bean
Model

DB

Model II

Servlet
Controller

Client

Java Bean
Model

DB

JSP
View

Model II
• • •

Model II introduced controller Servlet between browser and JSP Controller centralizes the logic for dispatching request to the next view based on the request It is easier to maintain and extend as views do not refer to each other directly

Cont…
• •

Model II controller servlet provides a single point of control for security and logging Model II is recommended for most web applications

Struts

The Framework

Contents
• • •

What is Struts? Why Struts? Struts architecture

What is Struts?
• • •

Struts is an open source Web Application Framework It is developed as Apache Jakarta Project http://jakarta.apache.org/struts/

Cont… (Technical standpoint)
• • • • •

It’s a framework based on MVC design pattern It is used for developing Servlet/JSP based web applications It is pattern oriented, leverages J2EE design patterns It has its own Custom Tag libraries It has support for utility classes too

Struts lets developer…
• • •

Design their JSP/Servlet web applications have a MVC touch Utilise ready-to-use objects through configuration files Leverage extra features such as form validation, internationalization

Why Struts?
• • • • • • •

Easy to use, Feature rich, stable and mature framework Takes much of the complexity out of building you own MVC framework Encourages good design practice and modeling Flexible and Extensible Fits well with J2EE specifications Good Tag library support More modular: makes long term maintenance easier

Above all

Being an Open Source project helps Struts’ adoption

Struts Architecture
Event Http Request Controller Servlet Dispatch Business Logic Action

Forward

strutsconfig.xml

Update Http Response

View JSP

Get <Tag>

Model Application State

Cont…
• • • • •

The controller parses a configuration file and uses it to deploy other control layer objects The Struts Configuration defines the Action Mappings The controller servlet consults the Action Mapping to route request Request may be forwarded to JSP or Action classes provided by developer Often it is first forwarded to an Action and then to JSP

Cont…
• • • •

The Action object can handle the request and respond to the client It sometimes indicates that control should be forwarded elsewhere Action objects have access to the application’s controller servlet An Action object can indirectly forward one or more shared object including Java Beans

Struts Components

Major components of Struts
Servlet - Controller JSP - View BL in any form whichever suits the application Model

Struts is focused on Controller
Struts framework has a concrete set of programming API’s for Controller functions Struts is Model and View independent

Mapping in Struts
• • • •

Configuration file contains action mappings It determines forwarding/navigation Controller uses mapping to turn HTTP requests into application actions Mapping must specify
A request path Action to act upon the request

Controller

Contents
• • • •

What does controller do? The Controller components Controller in Struts framework Controller components in Struts

What does controller do?
• • • • •

It is the Switch board of MVC architecture It is focused on receiving request from the client It is responsible for deciding what business logic function is to be performed It reads configuration file to determine the flow control It delegates responsibility for producing next phase of the user interface

Controller in Struts

Struts framework provides a base servlet
org.apache.struts.action.ActionServlet Servlet mapping has to be configured in web.xml

Configuration done through struts-config.xml
Action mapping defines request URI of incoming request and specifies Action class Defines application modules

What we do?

Write an Action class
It is an extension of the Action class For every request that is received Override execute() method

Write the action mapping file
A configuration file in XML Used to configure the controller servlet

Update web application descriptor
For adding necessary Struts components in application

Controller Components
• • • •

ActionServlet RequestProcessor Action ActionMapping

Thank You

Zubair Shaikh
imzubair@gmail.com Presentation Version 1.0

Sign up to vote on this title
UsefulNot useful