Professional Documents
Culture Documents
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
Objectives
MVC Architecture
MVC Pattern Relationship between components Logical Layers in Web Application
Model View Controller
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
MVC Architecture
Object maintaining application state
Model
View
Controller
Components that process user Interaction, consult Model and present a view
ACCPi7.1, AWAJS- Architecting Web Applications using JSF and Struts
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
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
18
<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
19
NO MVC
Model 1 Architecture
20
MVC Model 1
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
23
MVC Model 1
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
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
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
29
30
Data Access
31
Module 2
INTRODUCTION TO STRUTS
32
Objectives
Need of a framework Struts MVC Architecture Components of Struts
33
34
Characteristics of a Framework
Consist of various classes or components which gives an abstraction of a specific concept
Characteristics of a Framework Gives definition of how these classes or components work with each other to provide a solution to a problem
35
Role of a Framework
Framework captures the design decisions that are common to an application domain Role
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.
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
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
39
40
41
Form Bean
Resource Boundles
42
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.
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
Action Class 2
Model
JSP (View)
Action Class 3
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
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
52
Tag Libraries
53
Summary
54
Summary
55