SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

KCD Italy 2022 - Application driven infrastructure with Crossplane
KCD Italy 2022 - Application driven infrastructure with CrossplaneKCD Italy 2022 - Application driven infrastructure with Crossplane
KCD Italy 2022 - Application driven infrastructure with Crossplane
sparkfabrik
 
Model view controller (mvc)
Model view controller (mvc)Model view controller (mvc)
Model view controller (mvc)
M Ahsan Khan
 
Spring framework core
Spring framework coreSpring framework core
Spring framework core
Taemon Piya-Lumyong
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
kamal kotecha
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
Information Technology
 
angular fundamentals.pdf angular fundamentals.pdf
angular fundamentals.pdf angular fundamentals.pdfangular fundamentals.pdf angular fundamentals.pdf
angular fundamentals.pdf angular fundamentals.pdf
NuttavutThongjor1
 
Apache tomcat
Apache tomcatApache tomcat
Apache tomcat
Shashwat Shriparv
 
Asp net
Asp netAsp net
Asp net
Dr. C.V. Suresh Babu
 
Servlets api overview
Servlets api overviewServlets api overview
Servlets api overview
ramya marichamy
 
Single Page Applications
Single Page ApplicationsSingle Page Applications
Single Page Applications
Massimo Iacolare
 
Introduction to Java 11
Introduction to Java 11 Introduction to Java 11
Introduction to Java 11
Knoldus Inc.
 
Lecture 7: Server side programming
Lecture 7: Server side programmingLecture 7: Server side programming
Lecture 7: Server side programming
Artificial Intelligence Institute at UofSC
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
11prasoon
 
.Net Core
.Net Core.Net Core
.Net Core
Bertrand Le Roy
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Serhat Can
 
Spring Boot
Spring BootSpring Boot
Spring Boot
Jaydeep Kale
 
OOP java
OOP javaOOP java
OOP java
xball977
 
Java Server Faces (JSF) - Basics
Java Server Faces (JSF) - BasicsJava Server Faces (JSF) - Basics
Java Server Faces (JSF) - Basics
BG Java EE Course
 
Servlets
ServletsServlets
Servlets
ZainabNoorGul
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
VMahesh5
 

Similar to Introduction to Struts 1.3 (20)

struts unit best pdf for struts java.pptx
struts unit best pdf for struts java.pptxstruts unit best pdf for struts java.pptx
struts unit best pdf for struts java.pptx
ozakamal8
 
struts unit best pdf for struts java.pptx
struts unit best pdf for struts java.pptxstruts unit best pdf for struts java.pptx
struts unit best pdf for struts java.pptx
ozakamal8
 
Asp.Net Mvc
Asp.Net MvcAsp.Net Mvc
Asp.Net Mvc
micham
 
MVC
MVCMVC
MVC
akshin
 
Spring Framework-II
Spring Framework-IISpring Framework-II
Spring Framework-II
People Strategists
 
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
jinaldesailive
 
ASP.NET MVC introduction
ASP.NET MVC introductionASP.NET MVC introduction
ASP.NET MVC introduction
Tomi Juhola
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
Hùng Nguyễn Huy
 
Struts Interceptors
Struts InterceptorsStruts Interceptors
Struts Interceptors
Onkar Deshpande
 
Spring Portlet MVC
Spring Portlet MVCSpring Portlet MVC
Spring Portlet MVC
John Lewis
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
nagarajupatangay
 
Jinal desai .net
Jinal desai .netJinal desai .net
Jinal desai .net
rohitkumar1987in
 
Struts course material
Struts course materialStruts course material
Struts course material
Vibrant Technologies & Computers
 
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
 
Asp.net With mvc handson
Asp.net With mvc handsonAsp.net With mvc handson
Asp.net With mvc handson
Prashant Kumar
 
Unit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptxUnit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptx
AbhijayKulshrestha1
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
erdemergin
 
8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated
8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated
8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated
dioduong345
 
Model View Controller-Introduction-Overview.pptx
Model View Controller-Introduction-Overview.pptxModel View Controller-Introduction-Overview.pptx
Model View Controller-Introduction-Overview.pptx
MarioCaday2
 
struts unit best pdf for struts java.pptx
struts unit best pdf for struts java.pptxstruts unit best pdf for struts java.pptx
struts unit best pdf for struts java.pptx
ozakamal8
 
struts unit best pdf for struts java.pptx
struts unit best pdf for struts java.pptxstruts unit best pdf for struts java.pptx
struts unit best pdf for struts java.pptx
ozakamal8
 
Asp.Net Mvc
Asp.Net MvcAsp.Net Mvc
Asp.Net Mvc
micham
 
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
jinaldesailive
 
ASP.NET MVC introduction
ASP.NET MVC introductionASP.NET MVC introduction
ASP.NET MVC introduction
Tomi Juhola
 
Spring Portlet MVC
Spring Portlet MVCSpring Portlet MVC
Spring Portlet MVC
John Lewis
 
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
 
Asp.net With mvc handson
Asp.net With mvc handsonAsp.net With mvc handson
Asp.net With mvc handson
Prashant Kumar
 
Unit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptxUnit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptx
AbhijayKulshrestha1
 
8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated
8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated
8-9-10. ASP_updated8-9-10. ASP_updated8-9-10. ASP_updated
dioduong345
 
Model View Controller-Introduction-Overview.pptx
Model View Controller-Introduction-Overview.pptxModel View Controller-Introduction-Overview.pptx
Model View Controller-Introduction-Overview.pptx
MarioCaday2
 
Ad

More from Ilio Catallo (20)

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

Recently uploaded (20)

Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 

Introduction to Struts 1.3

  • 1. Introduction to Jakarta Struts 1.3 Ilio Catallo – [email protected]
  • 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