Sunteți pe pagina 1din 54

Semester IV

Architecting Web Applications


using JSF and Struts
AWAJS Course Overview

Session Topic Module

T1 – L1  MVC Design Pattern


1, 2
 Introduction to Struts
T2 – L2  Controller Components
3, 4
 Model and View Components
T3 – L3  Introduction to JSF 5, 6
 JSF Tag Library
L6
 Assignments/Mock
L7
Assignment Eval
ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts
2
AWAJS - Session 1
Module 1: MVC Design Pattern
Module 2: Introduction to Struts
Module 1

MVC DESIGN PATTERN

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts 4


Objectives

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


5
MVC Pattern

• Design Pattern
– Designing a reusable software is a difficult task
– A Design pattern helps in reusable software
• MVC [Model-View-Controller]
– Main concern of MVC is to separate the data
(model) and the user interface (view)
– Separation is achieved by introducing an controller
component
– Controller defines as how the user interface
should react to a user input

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


6
What is MVC?

User

View Controller

Model

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


7
MVC Architecture
Object maintaining
application state

Model

Components for data View


visualization Controller

Components that process user


Interaction, consult Model and
present a view

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


8
Relationship between Components

• View and Controller


– Controller is responsible for creating or selecting
view

Controller
Request

Step 1
Incoming request is directed to Controller

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


9
Relationship…(cnt)

• Model and Controller


– Controller depends on model
– If a change is made to the model then there might
be required to make parallel changes in the
Controller

Controller
Model
Step 2
Controller processes request and forms a data Model

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


10
Relationship…(cnt)

• Model and View


– View depends on Model
– If a change is made to the model then there might
be required to make parallel changes in the view

Controller

Step 3
Model is passed to View
View

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


11
Relationship…(cnt)

Controller

View

Step 4
View transforms Model into appropriate output format

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


12
Relationship…(cnt)

Controller

View

Response

Step 5
Response is rendered

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


13
Logical Layers in Web Application

• Model [Business Process Layer]

• View [Presentation Layer]

• Controller [Control Layer]

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


14
MVC - Model Model

• Models data and behavior behind business


process
• Manages Information - If Changes
• Contains data and Related Functionality
• Maps Real-World Entities
• Performing DB Queries
• Calculating Business Process
• Encapsulates Domain Logic which are
independent of Presentation

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


15
Controlle
MVC - Controller r

• Serves logical connection between user’s


interaction and the business process
• It receives and Translates input to request on
model or view
• Input from user and instructs the model and view
to perform action
• Responsible for making decision among multiple
presentation
• Maps the end-user action to the application
response
ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts
16
MVC - View View

• Obtains data from model & presents to the user


• Represents Output/Input of the application
• Display results of Business Logic
• Free Access to Model
• Reads Data from Model – Using Query Methods

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


17
Logical Layers in Web Application

public class DbBean{


public string userName { get; set; }
public string password { get; set; } Model


}

<form method="post" action="Login">


<input type="text" name="txtUserName"></td>
<input type="text" name="txtUserName"></td>
View

<td>${u.userName} </td>
<td>${u.userPassword} </td>

protected void processRequest(HttpServletRequest request,


HttpServletResponse response)throws ServletException, IOException {
String userName = request.getParameter("txtUserName");
String userPassword = request.getParameter("txtPassword"); Controller
User u = new User();
UserBO ubo = new UserBO();
u.setUserName(userName);
u.setUserPassword(userPassword);…

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


18
Non-MVC and MVC Apps

NO MVC Model 1 Architecture


ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts
19
JSP Model 1

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


20
JSP Model 1

• Composed of a series of interrelated JSP pages


• JSP pages handle all aspects of the application
like presentation, control, and business process

• Business process logic and control decisions are


hard coded inside JSP pages
• Next page selection is determined by hyperlink or
action of submitting a form
– <a href=“find.jsp”> Search </a>
– <form action=“find.jsp”> … </form>

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


21
JSP Model 1

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


22
JSP Model 1

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


23
JSP Model 1

• Advantages
– Lightweight design – for small, static application
– Suitable for small applications having very simple page
flow, little need for centralized security control/logging
– Separation of presentation from content
• Limitations
– Navigation Problem – to change name of JSP file have
to change in many location
– Difficult to maintain an application – large java code
being embedded in JSP page
– Not suitable for large and complex applications

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


24
JSP Model 2

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


25
JSP Model 2

• Use Servlet and JSP together (Model 2)


• JSP pages are used only for presentation
• Servlet handles initial request, partially process the
data, set up beans, then forward the results to one of
a number of different JSP pages
• Servlet serves as a gatekeeper
– Provides common services, such as authentication
authorization, login, error handling, and etc
• Servlet serves as a central controller
– Act as a state machine or an event dispatcher to
decide upon the appropriate logic to handle the request

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


26
JSP Model 2

Servlet-centric Scenario
ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts
27
JSP Model 2

• Advantages
– Easier to build, maintain and extend
– Single point of control (Servlet) for security &
logging
• Limitations
– Increase Design Complexity

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


28
Model 1 and Model 2 Comparison

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


29
Web application Requirements
Business Logic
Implementation

Data
Access

User
Interaction

User Interface
Display
ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts
30
Module 2

INTRODUCTION TO STRUTS

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts 31


Objectives

• Need of a framework
• Struts MVC Architecture
• Components of Struts

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


32
What is the Framework?

• Framework is a set of classes and interfaces that


helps in building an application
• A good framework should provide generic behaviors
that can be utilized across many different types of
applications
• The classes and interfaces in Java are also
considered as a framework, but these are in fact
libraries.

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


33
Characteristics of a Framework

Consist of various classes or


Provide an organized pattern
components which gives an
of an application
abstraction of a specific concept

Characteristics of
a Framework

Gives definition of how these


classes or components work Components and classes can be
with each other to provide a reused
solution to a problem

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


34
Role of a Framework

• Framework captures the design decisions that are


common to an application domain

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


35
Need of a Framework

• Required for developing rapid, extensible, loosely


coupled and highly cohesive applications
• Needed to incorporate the following features in
software design process
• Modularity - Encapsulating the application specific
details, which may change and separate it from a
stable interface
• Extensibility - Ease with which a new behavior can be
added depending upon the application domain
• Reusability - By defining generic components that
can be reapplied to create new applications
• Inversion Of Control (IOC) - Design pattern which
reduces the tight coupling between associating object
by creating separate event handler objects.

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


36
What is Struts?

• Struts is an open source java-based web application


framework
• Offer a unified framework based on MVC 2
Architecture
• Developed by Craig Mcclanahan and supported by
Apache Software Foundation’s Jakarta group
• This framework resides in the web tier, and the
struts application are hosted by a web container
• Basically consist of two entities
• First, It is an MVC application
• Second, It has a set of utilities and libraries as tool set
to build web applications

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


37
The major components of Struts
framework

Basic framework Validator-Plugin

Provide the MVC 2 Sub-framework,


functionality Components Sub-framework allows
facilitates validation of
Helps to view logic in of the
datareuse of as well as
on server
JSP Framework presentation code
on the client side

Tag Libraries Tiles-Plugin

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


38
Strut Control flow
• Struts uses MVC architecture and hence
follow the same control flow.
• The control flow is as follows:

• Initially the user sends a request to the


controller servlet through a view.
• The controller servlet looks up the requested
URI in the XML configuration file and
determines the name of the Action class that
will process the business logic.

• The Action class acts on the model


component as per the application's logic.
• On completion of processing the request, the
Action class returns the results to the
ActionServlet class.

• Based on the results provided, ActionServlet


class 1 decides which view to be forwarded
with these results.

• The selected view displays the result thus


completing the request - response cycle.

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts 39


Struts MVC Components

• Model component
• View component
• Controller component

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


40
Struts View Components

JSP Pages JSP Tag Library

They contain static HTML and JSP library tags to Struts have its own JSP tag libraries which help in
generate dynamic HTML page. The JSP contains development of JSP pages. These custom tags
code for user interaction.The static and the libraries provide a means to create HTML forms
dynamic HTML generated page is sent to the whose data will be captured in Form Beans and
browser. Struts provide JSP tag library which Struts View These allow internationalisation of Java application by
display the data stored in Form Beans. There are
allow beans
Form creating
provide
HTMLa forms
channelforfor
capturing
data transfer
data Components having application content placed into central
and also displaying
between the view andthecontrol
data. layers of struts
utility tags which helps to implement conditional
repository called bundles. Thus, the content are not
application. When a Struts application receives an logic, iteration
hard coded in theand many more.
application and is read from the
HTML form it takes the data from the incoming bundle by the application during execution. The
form and populates the corresponding FormBean. advantage of using resource bundles for storing the
The Controller accesses the data using the getter application content is that it can be changed without
method of FormBean. recompiling the application.

Form Bean Resource Boundles

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


41
Struts Model Components
• Struts architecture does not
provide any specific constraints
for the Model component.
• The application developer may
use technology such as Enterprise
JavaBean (EJB), Java Data Object
(JDO) or the Data Access Objects
(DAO) pattern to implement the
model component.
• The model layer may be broken
down into three sub layers.

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts 42


Controller Mechanism

• Controller is Java Servlet and Central point of access in


web application
• All request for an MVC-based web application pass
through the controller
• Enables the Controller layer to provide common
processing to all requests such as security, caching,
logging and so on.
• Controller intercepts the client request maps them to
business operations, collects the results from each of
the required business operations and also decide which
view component needs to be called to display result.

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


43
Controller Mechanism

Browser

ActionServlet

RequestProcessor

Action

Model View
Layer Layer

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


44
Working of Struts MVC

JSP
Action Class 1
(View)

Action Servlet
(Controller) Action Class 2 Model

JSP
(View) Action Class 3

Implementing Struts in MVC

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts 45


Merits of Struts

• Code Extensibility
– Many Struts values are represented in XML or in property files,
so changes can be made to the file without recompiling the
java code
• Supports for Localization and Internalization
– Using ResourceBundle
• Simple Request Processing Mechanism
– Provides several utilites, such as StructValidatorUtil,
MessageResource class and so on, which simplifies the request
processing mechanism
• Model-View Communication
– Provides a set of taglib tags which allows us to create HTML
forms that directly associates with JavaBean component
• Input Validation
– Provide built-in capability for validation of form values

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


46
Limitation of Struts

• Steep Learning Curve


– For smaller projects the learning curve for this
framework prove to be too steep
• Only MVC
– Only MVC based application can be created
• Single ActionServlet
– Only a single ActionServlet can be used in an
application, which can cause configuration variance
• Event Model
– Doesn’t support Event Model (Similar to Swing
Component)

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


47
Alternatives to Struts

• Cocoon
– An Apache framework for web development and separates
business logic, presentation logic and component based web
development. The web application is build as a “component
pipeline”.
• WebWork
– Java based Web-application framework. Provides a strong
support to build reusable User Interface templates for form
control, UI themes, internationalization, client and server side
validations and so on.
• Jakarta Velocity
– Another framework similar as Struts has a capacity to do lot
more beyond just creating dynamic web content. Capable of
generating SQL, PostScript, and XML form templates.

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


48
Alternatives to Struts

• Stxx
– An extension of Struts framework to support XML and
XSL without changing the Struts functionality
• JSF
– Designed to simplifies the process of building GUI’s of
Java Server Applications. Provides standard JSP tags and
API’s which helps in managing complex HTML forms,
event handling activities, and presentation of data.
• Spring
– New framework for J2EE applications. Different from
other frameworks, because it provides a way to manage
business objects. It is comprehensive and modular

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


49
Anatomy of Struts Application

• Create the required view (JSP/HTML) pages


• Create the ActionForm Bean
• Configure ActionForm Bean in struts-config.xml
• Create the Application’s Controller component
(ActionServlet)
• Configure ActionServlet in web.xml file
• Create the Controller component (Action)
• Define the relationships that exists between the views
and Controller in struts-config.xml
• Configure struts-config.xml file in web.xml
• Build and Run the Application

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


50
Struts Component Package

ANTLR

Jakarta
Jakarta
Commons
ORO
BeanUtils

Struts
Jakarta
component Jakarta
Commons
Validator
package Commons
Digester .

Jakarta Jakarta
Commons Commons
Logging FileUpload

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts 51


Tag Libraries

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts 52


Summary

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


53
Summary

ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts


54

S-ar putea să vă placă și