SlideShare une entreprise Scribd logo
1  sur  38
Introduction to Jakarta Struts 1.3
Ilio Catallo – info@iliocatallo.it
Outline
¤ Model-View-Controller vs. Web applications
¤ From MVC to MVC2
¤ What is Struts?
¤ Struts Architecture
¤ Building web applications with Struts
¤ Setting up the Controller
¤ Writing Views
¤ References
2
Model-View-Controller vs.
Web applications
3
Model-View-Controller
design pattern
¤ In the late seventies, software architects saw applications
as having three major parts:
¤ The part that manages the data (Model)
¤ The part that creates screens and reports (View)
¤ The part that handles interactions between the user and the
other subsystems (Controller)
¤ MVC turned out to be a good way to design applications
¤ Cocoa (Apple)
¤ Swing (Java)
¤ .NET (Microsoft)
4
Model-View-Controller
design pattern
5
View
Controller
Model
State query
Change notification
Event
Method
invocation
Model-View-Controller vs.
Web applications
¤ What is the reason not to use the same MVC pattern also
for web applications?
¤ Java developers already have utilities for:
¤ building presentation pages, e.g., JavaServer Pages (View)
¤ handling databases, e.g., JDBC and EJB (Model)
¤ But…
¤ the HTTP protocol imposes limitations on the applicability of
the MVC design pattern
¤ we don’t have any component to act as the Controller
6
HTTP limitations
¤ The MVC design pattern requires a push protocol for the
views to be notified by the model
¤ HTTP is a pull protocol: no request implies no response
¤ The MVC design pattern requires a stateful protocol to
keep track of the state of the application
¤ HTTP is stateless
7
HTTP limitations: Struts solutions
¤ HTTP is stateless: we can implement the MVC design
pattern on top of the Java Servlet Platform
¤ the platform provides a session context to help track users in
the application
¤ HTTP is a pull protocol: we can increase the Controller
responsibility. It will be responsible for:
¤ state changes
¤ state queries
¤ change notifications
8
Model-View-Controller 2
design pattern
¤ The resulting design pattern is sometimes called MVC2 or
Web MVC
¤ Any state query or change notification must pass through
the Controller
¤ The View renders data passed by the Controller rather than
data returned directly from the Model
9
View Controller Model
What is Jakarta Struts?
¤ Jakarta Struts is an open source framework
¤ It provides a MVC2-style Controller that helps turn raw
materials like web pages and databases into a coherent
application
¤ The framework is based on a set of enabling
technologies common to every Java web application:
¤ Java Servlets for implementing the Controller
¤ JavaServer Pages for implementing the View
¤ EJB or JDBC for implementing the Model
10
Struts Architecture
11
Struts Main Components:
ActionForward, ActionForm, Action
¤ Each web application is made of three main
components:
¤ Hyperlinks lead to pages that display data and other
elements, such as text and images
¤ HTML forms are used to submit data to the application
¤ Server-side actions which performs some kind of business
logic on the data
12
Struts Main Components:
ActionForward, ActionForm, Action
¤ Struts provides components that programmers can use to
define hyperlinks, forms and custom actions:
¤ Hyperlinks are represented as ActionForward objects
¤ Forms are represented as ActionForm objects
¤ Custom actions are represented as Action objects
13
Struts Main Components:
ActionMapping
¤ Struts bundles these details together into an
ActionMapping object
¤ Each ActionMappinghas its own URI
¤ When a specific resource is requested by URI, the
Controller retrieves the corresponding ActionMapping
object
¤ The mapping tells the Controller which Action, ActionForm
and ActionForwards to use
14
Struts Main Components:
ActionServlet
¤ The backbone component of the Struts framework is
ActionServlet (i.e., the Struts Controller)
¤ For every request, the ActionServlet:
¤ uses the URI to understand which ActionMapping to use
¤ bundles all the user input into an ActionForm
¤ call the Action in charge of handling the request
¤ reads the ActionForwardcoming from the Action and
forward the request to the JSP page what will render the
result
15
Struts Control Flow
16
Controller
(ActionServlet)
Action
(ActionMapping,
ActionForm)
JSP page
(with HTML
form)
JSP page
(result page)
HTTP
request
HTTP
response
①
②
③
④ Model
(e.g., EJB)
ActionForward
⑤
Controller Model
Struts main component responsibilities
Class Description
ActionForward A user’s gesture or view selection
ActionForm The data for a state change
ActionMapping The state change event
ActionServlet The part of the Controller that receives
user gestures and stare changes and
issues view selections
Action classes The part of the Controller that interacts
with the model to execute a state
change or query and advises
ActionServlet of the next view to select
17
Building Web applications with
Struts
Setting up the Controller
18
Setting up the Controller:
The big picture
19
struts-
config.xml
web.xml
ActionMapping 1 ActionMapping N
Setting up the Controller:
Servlet Container (1/3)
¤ The web.xml deployment descriptor file describes how to
deploy a web application in a servlet container (e.g., Tomcat)
¤ The container reads the deployment descriptor web.xml, which
specifies:
¤ which servlets to load
¤ which requests are sent to which servlet
20
Setting up the Controller:
Servlet Container (2/3)
¤ Struts implements the Controller as a servlet
¤ Like all servlets it lives in the servlet container
¤ Conventionally, the container is configured to sent to
ActionServlet any request that matches the pattern
*.do
¤ Remember: Any valid extension or prefix can be used,
.do is simply a popular choice
21
Setting up the Controller:
Servlet Container (3/3)
web.xml (snippet)
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
22
¤ Forward any request that matches the pattern *.do to
the servlet named action (i.e., the Struts controller)
Struts Controller:
struts-config.xml
¤ The framework uses the struts-config.xml file as a
deployment descriptor
¤ It contains all the ActionMappings definedfor the web
application
¤ At boot time, Struts reads it to create a database of objects
¤ At runtime, Struts refers to the object created with the
configuration file, not the file itself
23
Struts Controller:
ActionForm (1/4)
¤ A JavaBean is a reusable software component which
conform to a set of design patterns
¤ The access to the bean’s internal state is provided through
two kinds of methods: accessors and mutators
¤ JavaBeans are used to encapsulate many objects into a
single object
¤ They can be passed around as a single bean object instead
of as multiple individual objects
24
Struts Controller:
ActionForm (2/4)
¤ Struts model ActionForms as JavaBeans
¤ The ActionForm has a corresponding property for each field
on the HTML form
¤ The Controller matches the parameters in the HTTP
request with the properties of the ActionForm
¤ When they correspond, the Controller calls the setter
methods and passes the value from the HTTP request
25
Struts Controller:
ActionForm (3/4)
LoginForm.java
pubic class LoginForm extends org.apache.struts.action.ActionForm {
private String username;
private String password;
public String getUsername() {return this.username;}
public String getPassword() {return this.password;}
public void setUsername(String username) {this.username =
username;}
public void setPassword(String password) {this.password =
password;}
}
26
¤ An ActionForm is a JavaBean that extends
org.apache.struts.action.ActionForm
Struts Controller:
ActionForm (4/4)
Specifying a new ActionForm in struts-config.xml
<form-beans>
<form-bean name=”loginForm"
type=”app.LoginForm"/>
</form-beans>
27
¤ Define a mapping between the actual ActionForm and
its logical name
Struts Controller:
ActionForwards
Specifying new ActionForwardsin struts-config.xml
<forward name="success" path="/success.html"/>
<forward name=”failure" path="/success.html"/>
<forward name=”logon" path=”/Logon.do"/>
28
¤ Define a mapping between the resource link and its
logical name
¤ Once defined, throughout the web application it is
possible to reference the resource via its logical name
Struts Controller:
Action
¤ Actions are Java classes that extend
org.apache.struts.Action
¤ The Controller populates the ActionForm and then passes it
to the Action
¤ the entry method is perform (Struts 1.0) or execute (Struts 1.1+)
¤ The Action is generally responsible for:
¤ validating input
¤ accessing business information
¤ determining which ActionForward to return to the Controller
29
Struts Controller:
Action
LoginAction.java
import javax.servet.http.*;
public class LoginAction extends org.apache.struts.action.Action {
public ActionForward execute(ActionMapping mapping, ActionForm
form,
HttpServletRequest req, HttpServletResponse
res) {
// Extract data from the form
LoginForm lf = (LoginForm) form;
String username = lf.getUsername();
String password = lf.getPassword();
// Apply business logic
UserDirectory ud = UserDirectory.getInstance();
if (ud.isValidPassword(username, password))
return mapping.findForward("success");
return mapping.findForward("failure");
}
} 30
Struts Controller:
struts-config.xml
struts-config.xml (snippet)
<form-beans>
<form-bean name="loginForm"
type="app.LoginForm"/>
</form-beans>
<action-mappings>
<action path="/login"
type="app.LoginAction"
name="loginForm">
<forward name="success" path="/success.jsp"/>
<forward name="failure" path="/failure.jsp"/>
</action>
</action-mappings>
31
Struts trims
automatically
the .do
extension
Building web applications with
Struts
Writing Views
32
Writing Views:
JavaServer Pages (JSP)
¤ JavaServer Pages is a technology that helps Java
developers create dynamically generated web pages
¤ A JSP page is a mix of plain old HTML tags and JSP scripting
elements
¤ JSP pages are translated into servlets at runtime by the JSP
container
33
JSP Scripting Element
<b> This page was accessed at <%= new Date() %></b>
Writing Views:
JSP tags
¤ JSP scripting elements require that developers mix Java
code with HTML. This situation leads to:
¤ non-maintainable applications
¤ no opportunity for code reuse
¤ An alternative to scripting elements is to use JSP tags
¤ JSP tags can be used as if they were ordinary HTML tags
¤ Each JSP tag is associated with a Java class
¤ It’s sufficient to insert the same tag on another page to reuse
the same code
¤ If the code changes, all the pages are automatically updated
34
Writing Views:
JSP Tag Libraries
¤ A number of prebuilt tag libraries are available for
developers
¤ Example: JSP Standard Tag Library (JSTL)
¤ Each JSP tag library is associated with a Tag Library
Descriptor (TLD)
¤ The TLD file is an XML-style document that defines a tag
library and its individual tags
¤ For each tag, it defines the tag name, its attributes, and the
name of the class that handles tag semantics
35
Writing Views:
JSP Tag Libraries
¤ JSP pages are an integral part of the Struts Framework
¤ Struts provides its own set of custom tag libraries
36
Tag library descriptor Purpose
struts-html.tld JSP tag extension for
HTML forms
struts-bean.tld JSP tag extension for
handling JavaBeans
struts-logic.tld JSP tag extension for
testing the values of
properties
Writing Views
login.jsp
<%@ taglib uri=”http://struts.apache.org/tags-html" prefix="html" %>
<html>
<head>
<title>Sign in, Please!</title>
</head>
<body>
<html:form action="/login" focus="username">
Username: <html:text property="username"/> <br/>
Password: <html:password property="password"/><br/>
<html:submit/> <html:reset/>
</html:form>
</body>
</html>
37
the taglib directive
makes accessible the
tag library to the JSP
page
JSP tag from the
struts-html tag
library
References
¤ Struts 1 In Action, T. N. Husted, C. Dumoulin, G. Franciscus,
D. Winterfeldt, , Manning Publications Co.
¤ JavaBeans, In Wikipedia, The Free
Encyclopedia,http://en.wikipedia.org/w/index.php?title=
JavaBeans&oldid=530069922
¤ JavaServer Pages, In Wikipedia, The Free
Encyclopediahttp://en.wikipedia.org/w/index.php?title=
JavaServer_Pages&oldid=528080552
38

Contenu connexe

Tendances

Asp.net server controls
Asp.net server controlsAsp.net server controls
Asp.net server controls
Raed Aldahdooh
 
What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2
Santosh Singh Paliwal
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
Jafar Nesargi
 
Active Server Page(ASP)
Active Server Page(ASP)Active Server Page(ASP)
Active Server Page(ASP)
Keshab Nath
 

Tendances (20)

Web forms in ASP.net
Web forms in ASP.netWeb forms in ASP.net
Web forms in ASP.net
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
Introduction to ASP.NET
Introduction to ASP.NETIntroduction to ASP.NET
Introduction to ASP.NET
 
ASP.NET Page Life Cycle
ASP.NET Page Life CycleASP.NET Page Life Cycle
ASP.NET Page Life Cycle
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
Asp.net server controls
Asp.net server controlsAsp.net server controls
Asp.net server controls
 
Servlets
ServletsServlets
Servlets
 
ASP.NET MVC.
ASP.NET MVC.ASP.NET MVC.
ASP.NET MVC.
 
What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2
 
Web api
Web apiWeb api
Web api
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
MVC in PHP
MVC in PHPMVC in PHP
MVC in PHP
 
Active Server Page(ASP)
Active Server Page(ASP)Active Server Page(ASP)
Active Server Page(ASP)
 
MVC Architecture
MVC ArchitectureMVC Architecture
MVC Architecture
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
Model view controller (mvc)
Model view controller (mvc)Model view controller (mvc)
Model view controller (mvc)
 
Servlets
ServletsServlets
Servlets
 
Java servlets
Java servletsJava servlets
Java servlets
 

Similaire à Introduction to Struts 1.3

SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe
 
Apachecon 2002 Struts
Apachecon 2002 StrutsApachecon 2002 Struts
Apachecon 2002 Struts
yesprakash
 
Struts An Open-source Architecture for Web Applications
Struts An Open-source Architecture for Web ApplicationsStruts An Open-source Architecture for Web Applications
Struts An Open-source Architecture for Web Applications
elliando dias
 

Similaire à Introduction to Struts 1.3 (20)

Asp.Net Mvc
Asp.Net MvcAsp.Net Mvc
Asp.Net Mvc
 
MVC
MVCMVC
MVC
 
Spring Framework-II
Spring Framework-IISpring Framework-II
Spring Framework-II
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
 
ASP.NET MVC introduction
ASP.NET MVC introductionASP.NET MVC introduction
ASP.NET MVC introduction
 
Struts Interceptors
Struts InterceptorsStruts Interceptors
Struts Interceptors
 
Spring Portlet MVC
Spring Portlet MVCSpring Portlet MVC
Spring Portlet MVC
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Jinal desai .net
Jinal desai .netJinal desai .net
Jinal desai .net
 
Struts course material
Struts course materialStruts course material
Struts course material
 
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
 
Apachecon 2002 Struts
Apachecon 2002 StrutsApachecon 2002 Struts
Apachecon 2002 Struts
 
Asp.net With mvc handson
Asp.net With mvc handsonAsp.net With mvc handson
Asp.net With mvc handson
 
Unit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptxUnit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptx
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
MVC 6 Introduction
MVC 6 IntroductionMVC 6 Introduction
MVC 6 Introduction
 
Asp.net,mvc
Asp.net,mvcAsp.net,mvc
Asp.net,mvc
 
Simple mvc4 prepared by gigin krishnan
Simple mvc4 prepared by gigin krishnanSimple mvc4 prepared by gigin krishnan
Simple mvc4 prepared by gigin krishnan
 
Struts 1
Struts 1Struts 1
Struts 1
 
Struts An Open-source Architecture for Web Applications
Struts An Open-source Architecture for Web ApplicationsStruts An Open-source Architecture for Web Applications
Struts An Open-source Architecture for Web Applications
 

Plus de Ilio Catallo

Plus de Ilio Catallo (20)

C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
 
Regular types in C++
Regular types in C++Regular types in C++
Regular types in C++
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
 
Memory management in C++
Memory management in C++Memory management in C++
Memory management in C++
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
 
Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 
Spring MVC - Wiring the different layers
Spring MVC -  Wiring the different layersSpring MVC -  Wiring the different layers
Spring MVC - Wiring the different layers
 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platforms
 
Spring MVC - Web Forms
Spring MVC  - Web FormsSpring MVC  - Web Forms
Spring MVC - Web Forms
 
Spring MVC - The Basics
Spring MVC -  The BasicsSpring MVC -  The Basics
Spring MVC - The Basics
 
Web application architecture
Web application architectureWeb application architecture
Web application architecture
 
Introduction To Spring
Introduction To SpringIntroduction To Spring
Introduction To Spring
 
Gestione della memoria in C++
Gestione della memoria in C++Gestione della memoria in C++
Gestione della memoria in C++
 
Array in C++
Array in C++Array in C++
Array in C++
 
Puntatori e Riferimenti
Puntatori e RiferimentiPuntatori e Riferimenti
Puntatori e Riferimenti
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag Library
 
Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3
 

Dernier

Dernier (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Introduction to Struts 1.3

  • 1. Introduction to Jakarta Struts 1.3 Ilio Catallo – info@iliocatallo.it
  • 2. Outline ¤ Model-View-Controller vs. Web applications ¤ From MVC to MVC2 ¤ What is Struts? ¤ Struts Architecture ¤ Building web applications with Struts ¤ Setting up the Controller ¤ Writing Views ¤ References 2
  • 4. Model-View-Controller design pattern ¤ In the late seventies, software architects saw applications as having three major parts: ¤ The part that manages the data (Model) ¤ The part that creates screens and reports (View) ¤ The part that handles interactions between the user and the other subsystems (Controller) ¤ MVC turned out to be a good way to design applications ¤ Cocoa (Apple) ¤ Swing (Java) ¤ .NET (Microsoft) 4
  • 6. Model-View-Controller vs. Web applications ¤ What is the reason not to use the same MVC pattern also for web applications? ¤ Java developers already have utilities for: ¤ building presentation pages, e.g., JavaServer Pages (View) ¤ handling databases, e.g., JDBC and EJB (Model) ¤ But… ¤ the HTTP protocol imposes limitations on the applicability of the MVC design pattern ¤ we don’t have any component to act as the Controller 6
  • 7. HTTP limitations ¤ The MVC design pattern requires a push protocol for the views to be notified by the model ¤ HTTP is a pull protocol: no request implies no response ¤ The MVC design pattern requires a stateful protocol to keep track of the state of the application ¤ HTTP is stateless 7
  • 8. HTTP limitations: Struts solutions ¤ HTTP is stateless: we can implement the MVC design pattern on top of the Java Servlet Platform ¤ the platform provides a session context to help track users in the application ¤ HTTP is a pull protocol: we can increase the Controller responsibility. It will be responsible for: ¤ state changes ¤ state queries ¤ change notifications 8
  • 9. Model-View-Controller 2 design pattern ¤ The resulting design pattern is sometimes called MVC2 or Web MVC ¤ Any state query or change notification must pass through the Controller ¤ The View renders data passed by the Controller rather than data returned directly from the Model 9 View Controller Model
  • 10. What is Jakarta Struts? ¤ Jakarta Struts is an open source framework ¤ It provides a MVC2-style Controller that helps turn raw materials like web pages and databases into a coherent application ¤ The framework is based on a set of enabling technologies common to every Java web application: ¤ Java Servlets for implementing the Controller ¤ JavaServer Pages for implementing the View ¤ EJB or JDBC for implementing the Model 10
  • 12. Struts Main Components: ActionForward, ActionForm, Action ¤ Each web application is made of three main components: ¤ Hyperlinks lead to pages that display data and other elements, such as text and images ¤ HTML forms are used to submit data to the application ¤ Server-side actions which performs some kind of business logic on the data 12
  • 13. Struts Main Components: ActionForward, ActionForm, Action ¤ Struts provides components that programmers can use to define hyperlinks, forms and custom actions: ¤ Hyperlinks are represented as ActionForward objects ¤ Forms are represented as ActionForm objects ¤ Custom actions are represented as Action objects 13
  • 14. Struts Main Components: ActionMapping ¤ Struts bundles these details together into an ActionMapping object ¤ Each ActionMappinghas its own URI ¤ When a specific resource is requested by URI, the Controller retrieves the corresponding ActionMapping object ¤ The mapping tells the Controller which Action, ActionForm and ActionForwards to use 14
  • 15. Struts Main Components: ActionServlet ¤ The backbone component of the Struts framework is ActionServlet (i.e., the Struts Controller) ¤ For every request, the ActionServlet: ¤ uses the URI to understand which ActionMapping to use ¤ bundles all the user input into an ActionForm ¤ call the Action in charge of handling the request ¤ reads the ActionForwardcoming from the Action and forward the request to the JSP page what will render the result 15
  • 16. Struts Control Flow 16 Controller (ActionServlet) Action (ActionMapping, ActionForm) JSP page (with HTML form) JSP page (result page) HTTP request HTTP response ① ② ③ ④ Model (e.g., EJB) ActionForward ⑤ Controller Model
  • 17. Struts main component responsibilities Class Description ActionForward A user’s gesture or view selection ActionForm The data for a state change ActionMapping The state change event ActionServlet The part of the Controller that receives user gestures and stare changes and issues view selections Action classes The part of the Controller that interacts with the model to execute a state change or query and advises ActionServlet of the next view to select 17
  • 18. Building Web applications with Struts Setting up the Controller 18
  • 19. Setting up the Controller: The big picture 19 struts- config.xml web.xml ActionMapping 1 ActionMapping N
  • 20. Setting up the Controller: Servlet Container (1/3) ¤ The web.xml deployment descriptor file describes how to deploy a web application in a servlet container (e.g., Tomcat) ¤ The container reads the deployment descriptor web.xml, which specifies: ¤ which servlets to load ¤ which requests are sent to which servlet 20
  • 21. Setting up the Controller: Servlet Container (2/3) ¤ Struts implements the Controller as a servlet ¤ Like all servlets it lives in the servlet container ¤ Conventionally, the container is configured to sent to ActionServlet any request that matches the pattern *.do ¤ Remember: Any valid extension or prefix can be used, .do is simply a popular choice 21
  • 22. Setting up the Controller: Servlet Container (3/3) web.xml (snippet) <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> 22 ¤ Forward any request that matches the pattern *.do to the servlet named action (i.e., the Struts controller)
  • 23. Struts Controller: struts-config.xml ¤ The framework uses the struts-config.xml file as a deployment descriptor ¤ It contains all the ActionMappings definedfor the web application ¤ At boot time, Struts reads it to create a database of objects ¤ At runtime, Struts refers to the object created with the configuration file, not the file itself 23
  • 24. Struts Controller: ActionForm (1/4) ¤ A JavaBean is a reusable software component which conform to a set of design patterns ¤ The access to the bean’s internal state is provided through two kinds of methods: accessors and mutators ¤ JavaBeans are used to encapsulate many objects into a single object ¤ They can be passed around as a single bean object instead of as multiple individual objects 24
  • 25. Struts Controller: ActionForm (2/4) ¤ Struts model ActionForms as JavaBeans ¤ The ActionForm has a corresponding property for each field on the HTML form ¤ The Controller matches the parameters in the HTTP request with the properties of the ActionForm ¤ When they correspond, the Controller calls the setter methods and passes the value from the HTTP request 25
  • 26. Struts Controller: ActionForm (3/4) LoginForm.java pubic class LoginForm extends org.apache.struts.action.ActionForm { private String username; private String password; public String getUsername() {return this.username;} public String getPassword() {return this.password;} public void setUsername(String username) {this.username = username;} public void setPassword(String password) {this.password = password;} } 26 ¤ An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm
  • 27. Struts Controller: ActionForm (4/4) Specifying a new ActionForm in struts-config.xml <form-beans> <form-bean name=”loginForm" type=”app.LoginForm"/> </form-beans> 27 ¤ Define a mapping between the actual ActionForm and its logical name
  • 28. Struts Controller: ActionForwards Specifying new ActionForwardsin struts-config.xml <forward name="success" path="/success.html"/> <forward name=”failure" path="/success.html"/> <forward name=”logon" path=”/Logon.do"/> 28 ¤ Define a mapping between the resource link and its logical name ¤ Once defined, throughout the web application it is possible to reference the resource via its logical name
  • 29. Struts Controller: Action ¤ Actions are Java classes that extend org.apache.struts.Action ¤ The Controller populates the ActionForm and then passes it to the Action ¤ the entry method is perform (Struts 1.0) or execute (Struts 1.1+) ¤ The Action is generally responsible for: ¤ validating input ¤ accessing business information ¤ determining which ActionForward to return to the Controller 29
  • 30. Struts Controller: Action LoginAction.java import javax.servet.http.*; public class LoginAction extends org.apache.struts.action.Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) { // Extract data from the form LoginForm lf = (LoginForm) form; String username = lf.getUsername(); String password = lf.getPassword(); // Apply business logic UserDirectory ud = UserDirectory.getInstance(); if (ud.isValidPassword(username, password)) return mapping.findForward("success"); return mapping.findForward("failure"); } } 30
  • 31. Struts Controller: struts-config.xml struts-config.xml (snippet) <form-beans> <form-bean name="loginForm" type="app.LoginForm"/> </form-beans> <action-mappings> <action path="/login" type="app.LoginAction" name="loginForm"> <forward name="success" path="/success.jsp"/> <forward name="failure" path="/failure.jsp"/> </action> </action-mappings> 31 Struts trims automatically the .do extension
  • 32. Building web applications with Struts Writing Views 32
  • 33. Writing Views: JavaServer Pages (JSP) ¤ JavaServer Pages is a technology that helps Java developers create dynamically generated web pages ¤ A JSP page is a mix of plain old HTML tags and JSP scripting elements ¤ JSP pages are translated into servlets at runtime by the JSP container 33 JSP Scripting Element <b> This page was accessed at <%= new Date() %></b>
  • 34. Writing Views: JSP tags ¤ JSP scripting elements require that developers mix Java code with HTML. This situation leads to: ¤ non-maintainable applications ¤ no opportunity for code reuse ¤ An alternative to scripting elements is to use JSP tags ¤ JSP tags can be used as if they were ordinary HTML tags ¤ Each JSP tag is associated with a Java class ¤ It’s sufficient to insert the same tag on another page to reuse the same code ¤ If the code changes, all the pages are automatically updated 34
  • 35. Writing Views: JSP Tag Libraries ¤ A number of prebuilt tag libraries are available for developers ¤ Example: JSP Standard Tag Library (JSTL) ¤ Each JSP tag library is associated with a Tag Library Descriptor (TLD) ¤ The TLD file is an XML-style document that defines a tag library and its individual tags ¤ For each tag, it defines the tag name, its attributes, and the name of the class that handles tag semantics 35
  • 36. Writing Views: JSP Tag Libraries ¤ JSP pages are an integral part of the Struts Framework ¤ Struts provides its own set of custom tag libraries 36 Tag library descriptor Purpose struts-html.tld JSP tag extension for HTML forms struts-bean.tld JSP tag extension for handling JavaBeans struts-logic.tld JSP tag extension for testing the values of properties
  • 37. Writing Views login.jsp <%@ taglib uri=”http://struts.apache.org/tags-html" prefix="html" %> <html> <head> <title>Sign in, Please!</title> </head> <body> <html:form action="/login" focus="username"> Username: <html:text property="username"/> <br/> Password: <html:password property="password"/><br/> <html:submit/> <html:reset/> </html:form> </body> </html> 37 the taglib directive makes accessible the tag library to the JSP page JSP tag from the struts-html tag library
  • 38. References ¤ Struts 1 In Action, T. N. Husted, C. Dumoulin, G. Franciscus, D. Winterfeldt, , Manning Publications Co. ¤ JavaBeans, In Wikipedia, The Free Encyclopedia,http://en.wikipedia.org/w/index.php?title= JavaBeans&oldid=530069922 ¤ JavaServer Pages, In Wikipedia, The Free Encyclopediahttp://en.wikipedia.org/w/index.php?title= JavaServer_Pages&oldid=528080552 38