You are on page 1of 55

Semester IV

Architecting Web Applications using JSF and Struts

AWAJS Course Overview


Session T1 L1 T2 L2 T3 L3 L6
Assignments/Mock

Topic
MVC Design Pattern Introduction to Struts Controller Components Model and View Components Introduction to JSF JSF Tag Library

Module 1, 2 3, 4 5, 6

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

AWAJS - Session 2
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

Objectives

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

MVC Architecture
MVC Pattern Relationship between components Logical Layers in Web Application
Model View Controller

Evolution of MVC Architecture Non-MVC Applications

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

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

What is MVC?

User

View

Controller

Model

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

MVC Architecture
Object maintaining application state

Model

Components for data visualization

View

Controller

Components that process user Interaction, consult Model and present a view
ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts

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

10

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

11

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
ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts

View
12

Relationship(cnt)
Controller

View

Step 4
View transforms Model into appropriate output format
ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts

13

Relationship(cnt)
Controller

View
Response

Step 5
Response is rendered
ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts

14

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

15

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

16

MVC - Controller

Controlle r

Serves logical connection between users 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

17

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

18

Logical Layers in Web Application


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

<form method="post" action="Login"> <input type="text" name="txtUserName"></td> <input type="text" name="txtUserName"></td> <td>${u.userName} </td> <td>${u.userPassword} </td>

View

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

Controller

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

19

Non-MVC and MVC Apps

NO MVC

Model 1 Architecture
20

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

MVC Model 1

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

21

MVC 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

22

MVC Model 1

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

23

MVC Model 1

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

24

MVC 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

25

MVC Model 2

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

26

MVC 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

27

MVC Model 2

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

28

MVC 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

29

Model 1 and Model 2 Comparison

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

30

Web application Requirements


Business Logic Implementation

Data Access

User Interaction User Interface Display


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

31

Module 2

INTRODUCTION TO STRUTS

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

32

Objectives
Need of a framework Struts MVC Architecture Components of Struts

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

33

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

34

Characteristics of a Framework
Consist of various classes or components which gives an abstraction of a specific concept

Provide an organized pattern of an application

Characteristics of a Framework Gives definition of how these classes or components work with each other to provide a solution to a problem

Components and classes can be reused

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

35

Role of a Framework
Framework captures the design decisions that are common to an application domain Role

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

36

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

37

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 Foundations 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

38

The major components of Struts framework


Basic framework Provide the MVC 2 functionality Helps to view logic in JSP

Validator-Plugin Sub-framework, Sub-framework allows facilitates validation of the reuse of as well as data on server presentation side on the client code

Components of Framework

Tag Libraries

Tiles-Plugin

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

39

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

40

Struts MVC Components


Model component View component Controller component

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

41

Struts View Components


JSP Pages
They contain static HTML and JSP library tags to generate dynamic HTML page. The JSP contains code for user interaction.The static and the dynamic HTML generated page is sent to the browser. Struts provide JSP tag library which Form creating HTML channel for data transfer allow beans provide a forms for capturing data between the view and control and also displaying the data. layers of struts application. When a Struts application receives an HTML form it takes the data from the incoming form and populates the corresponding FormBean. The Controller accesses the data using the getter method of FormBean.

JSP Tag Library


Struts have its own JSP tag libraries which help in development of JSP pages. These custom tags libraries provide a means to create HTML forms whose data will be captured in Form Beans and These allow internationalisation of Java application by display the data stored in Form Beans. There are having application content placed into central utility tags which helps to implement conditional repository called bundles. Thus, the content are not logic,coded in the application and is read from the hard iteration and many more.
bundle by the application during execution. The advantage of using resource bundles for storing the application content is that it can be changed without recompiling the application.

Struts View Components

Form Bean

Resource Boundles

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

42

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. Each of these three sublayers need not get implemented as separate classes. However, the layers depict the breakdown from the functional aspect.

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

43

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

44

Controller Mechanism
Browser

ActionServlet

RequestProcessor

Action

Model Layer

View Layer
45

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

Working of Struts MVC


JSP (View) Action Class 1

Action Servlet (Controller)

Action Class 2

Model

JSP (View)

Action Class 3

Implementing Struts in MVC


46

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

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

47

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
Doesnt support Event Model (Similar to Swing Component)
ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts

48

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

49

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 GUIs of Java Server Applications. Provides standard JSP tags and APIs 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

50

Anatomy of Struts Application


Create the required view (JSP/HTML) pages Create the ActionForm Bean Configure ActionForm Bean in struts-config.xml Create the Applications 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
51

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

Struts Component Package


ANTLR Jakarta ORO Jakarta Commons BeanUtils

Jakarta Commons Validator

Struts component package


Jakarta Commons Logging Jakarta Commons FileUpload

Jakarta Commons Digester .

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

52

Tag Libraries

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

53

Summary

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

54

Summary

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

55

You might also like