1. Struts 2
Juan Carlos
Figueroa
Duran
Struts 2
Understanding
the context
Struts history
See Struts 2
Juan Carlos Figueroa Duran
from outside
the forest
See Struts 2
from inside
the forest Otoño -2010
Resume:
Request
Lifecycle in
Struts 2
Departamento de Tecnologias de la Informacion
Declaring
will we do y Ciencias de la Computación
Universidad del Bío-Bío
One example
2. Outline
Struts 2
Juan Carlos
Figueroa 1 Understanding the context
Duran
Understanding 2 Struts history
the context
Struts history
3 See Struts 2 from outside the forest
See Struts 2
from outside
the forest 4 See Struts 2 from inside the forest
See Struts 2
from inside
the forest 5 Resume: Request Lifecycle in Struts 2
Resume:
Request
Lifecycle in
Struts 2
6 Declaring will we do
Declaring
will we do 7 One example
One example
3. What is a framework?
Struts 2
Juan Carlos
Figueroa
Duran
Understanding
A framework is a piece of structural software. We say structural
the context because structure is perhaps a larger goal of the framework
Struts history than any specic functional requirement. A framework tries to
See Struts 2
from outside
make generalizations about the common tasks and workow of
the forest a specic domain.
See Struts 2
from inside
the forest
Resume:
Request
Lifecycle in
Struts 2
Declaring
will we do
One example
4. What is a MVC?
Struts 2
Juan Carlos
Figueroa
Duran
Understanding
The MVC pattern provides a separation of concerns that applies
the context well to web applications. Separation of concerns allows us to
Struts history manage the complexity of large software systems by dividing
See Struts 2
from outside them into high-level components.The MVC design pattern
the forest identies three distinct concerns: model, view, and controller.
See Struts 2
from inside
the forest
Resume:
Request
Lifecycle in
Struts 2
Declaring
will we do
One example
5. Struts history
Struts 2
Juan Carlos
Struts 1.X
Figueroa
Duran Apache Struts is an open-source framework that is used for
developing Java web application. Originally developed by the
Understanding
the context programmer and author Craig R. McClanahan, this was later
Struts history taken over by the Apache Software Foundation in 2002.
See Struts 2
from outside
the forest Struts 2.X
See Struts 2
from inside But with the development of new and lightweight MVC based
the forest
frameworks like Spring, Stripes and Tapestry, it becomes
Resume:
Request necessary to modify the Struts 1 framework. So, the team of
Lifecycle in
Struts 2 Apache Struts and another J2EE framework, WebWork of
Declaring OpenSymphony joined hand together to develop an advanced
will we do
framework with all possible developing features that will make it
One example
developer and user friendly.
6. Understanding MVC in Struts 2
Struts 2
Juan Carlos
Figueroa
Duran
The high-level design of Struts 2
Understanding
the context follows the well-established Web Browser
Struts history Model-View-Controller desing Client
See Struts 2 pattern. We see what MVC design
from outside request
the forest pattern identies three distinct render page
See Struts 2 concerns: model, view, and Controller
FilterDispatcher
from inside
the forest controller: In Struts 2, these are
invoke action
Resume: implemented by the action, result,
Request
Lifecycle in and FilterDispatcher, respectively. Model
Action select result
View
Result
Struts 2
Declaring
will we do
One example
7. The controller-FilterDispatcher
Struts 2
Juan Carlos
Figueroa
Duran The controller's job is map
requests to actions. In a web
Understanding
the context application, the incoming HTTP Web Browser
Struts history requests can be thought of as Client
See Struts 2 commands that the user issues to
from outside request
the forest the application. One of the render page
See Struts 2 fundamental tasks of a web Controller
FilterDispatcher
from inside
the forest application is routing these
invoke action
Resume: requests to the appropriate set of
Request
Lifecycle in actions that should be taken Model
Action select result
View
Result
Struts 2
Declaring
within the application itself.
will we do
One example
8. Model-Actions
Struts 2
Juan Carlos
Figueroa
Duran In more technical terms, the
model is the internal state of the
Understanding
the context application. This state is Web Browser
Struts history composed of both the data model Client
See Struts 2 and the business logic. From the
from outside request
the forest high-level black box view, the data render page
See Struts 2 and the business logic merge Controller
FilterDispatcher
from inside
the forest together into the monolithic state
invoke action
Resume: of the application. This actions
Request
Lifecycle in encapsulate the calls to business Model
Action select result
View
Result
Struts 2
Declaring
logic into a single unit of work.
will we do
One example
9. View-Result
Struts 2
Juan Carlos
Figueroa
Duran
Understanding
the context Web Browser
Struts history The view is the presentation the Client
See Struts 2 page returned to the web browser.
from outside request
the forest This page is the user interface render page
See Struts 2 that presents a representation of Controller
FilterDispatcher
from inside
the forest the application's state to the user.
invoke action
Resume:
Request Model View
Lifecycle in Action select result Result
Struts 2
Declaring
will we do
One example
10. Interceptor
Struts 2 We can see that this interceptors
Juan Carlos appears like a stack of this in front of
Figueroa
Duran the action. The invocation of the
action must travel through this stack. invoke action finished
Understanding
the context The interceptor allow implement
Struts history common and cross-cutting task, in a
See Struts 2 clean and reusable component that
Interceptors
from outside
the forest you can keep separate from your
See Struts 2 action code. Logging, data validation
from inside
the forest and le upload are good examples of Action
OGNL
invoke result
Result
Resume:
Request
task should be implemented in a OGNL
Lifecycle in
Struts 2
interceptor. Most every actions will ActionContext (ThreadLocal)
Declaring
have a stack of interceptor associated ValueStack request session . . .
will we do with it. These interceptors are
One example invoked both before and after the
action.
11. Interceptor ... continuation
Struts 2
Juan Carlos
Figueroa
Duran
invoke action finished
Understanding
the context
Struts history
The using of interceptors make your Interceptors
See Struts 2
from outside application more manageable. Whit
the forest
this exibility we gain two primary
See Struts 2
from inside benets, reuse and conguration. Action
OGNL
invoke result
Result
the forest
Resume: OGNL
Request
Lifecycle in ActionContext (ThreadLocal)
Struts 2 ValueStack request session . . .
Declaring
will we do
One example
12. Action
Struts 2 Struts 2 action serves three roles.
Juan Carlos First, an action is an encapsulation of
Figueroa
Duran the calls to business logic into a
single unit of work. Second, the invoke action finished
Understanding
the context action serves as a locus of data
Struts history transfer. Third, select the result that
See Struts 2 should render the result page.
Interceptors
from outside
the forest
See Struts 2
Who class can be Actions?
from inside
the forest All action what be or pretend be a invoke result
Action
OGNL Result
Resume: POJO.
Request OGNL
Lifecycle in
Struts 2 Convention ActionContext (ThreadLocal)
ValueStack request session . . .
Declaring
will we do All action class, must use a naming
One example convention where our class names
end with the word Action.
13. Result
Struts 2
Juan Carlos
Figueroa
Duran
invoke action finished
Understanding
the context
Struts history
See Struts 2
Is the component for rendering the Interceptors
from outside
the forest
view back to the user. We will see
See Struts 2
this with more details in the lecture
from inside
the forest
of JSP. Action
OGNL
invoke result
Result
Resume: OGNL
Request
Lifecycle in ActionContext (ThreadLocal)
Struts 2 ValueStack request session . . .
Declaring
will we do
One example
14. ValueStack
Struts 2
Juan Carlos
Figueroa
Duran Simpling, the ValueStack is a
storage area that holds all the data invoke action finished
Understanding
the context associated with the processing of a
Struts history request. So keep all data in a Interceptors
See Struts 2 convenient central location. The
from outside
the forest OGNL is a tool that allows us to
See Struts 2 access the data we put in the
from inside invoke result
the forest ValueStack. More especically this Action
OGNL Result
Resume:
Request
languaje allows reference and OGNL
Lifecycle in
Struts 2
manipulate the data on the ActionContext (ThreadLocal)
ValueStack request session . . .
Declaring ValueStack.
will we do
One example
15. Request Lifecycle
Struts 2
Juan Carlos
Figueroa
Duran
Understanding
the context myAction.action
Struts history
Action
Interceptors
See Struts 2
from outside
the forest
See Struts 2
from inside
the forest Result
Resume:
Request
Lifecycle in HTML
Struts 2
Declaring
will we do
One example
16. Request Lifecycle
Struts 2
Juan Carlos
Figueroa
Duran
User Sends request:
Understanding
the context User sends a request to the server for
Struts history some resource. myAction.action
See Struts 2
from outside Action
Interceptors
the forest FilterDispatcher determines the
See Struts 2
from inside
appropriate action: Result
the forest
The FilterDispatcher looks at the HTML
Resume:
Request request and then determines the
Lifecycle in
Struts 2 appropriate Action.
Declaring
will we do
One example
17. Request Lifecycle
Struts 2
Juan Carlos
Figueroa Interceptors are applied:
Duran
Interceptors congured for applying
Understanding
the context the common functionalities such as
Struts history workow, validation, le upload etc.
See Struts 2 are automatically applied to the myAction.action
from outside Action
request.
Interceptors
the forest
See Struts 2
from inside Result
the forest Execution of Action:
Resume: HTML
Request Then the action method is executed
Lifecycle in
Struts 2 to perform the database related
Declaring operations like storing or retrieving
will we do
data from the database.
One example
18. Request Lifecycle
Struts 2
Juan Carlos
Figueroa
Duran Return of Request:
Understanding Then the request returns through the
the context interceptors in the reverse order. The
Struts history
returning request allows us to myAction.action
See Struts 2
from outside perform the clean-up or additional Action
Interceptors
the forest
processing.
See Struts 2
from inside Result
the forest
Display the result to user:
Resume: HTML
Request
Lifecycle in Finally the control is returned to the
Struts 2 servlet container, which sends the
Declaring
will we do output to the user browser.
One example
19. Request Lifecycle
Struts 2
Juan Carlos
Figueroa
Duran Output rendering:
Understanding Then the Result renders the output.
the context
Struts history
See Struts 2
from outside
the forest myAction.action
Action
See Struts 2
Interceptors
from inside
the forest
Resume: Result
Request
Lifecycle in HTML
Struts 2
Declaring
will we do
One example
20. Declarative architecture
Struts 2
Juan Carlos
Figueroa
Duran The more important conguration that you can do in Struts 2.
With this you can dening the Struts 2 components that your
Understanding
the context application will use and linking them together to form your
Struts history required workow paths. We means is it in this step we can
See Struts 2 dene what action is red with a particular URL, and what
from outside
the forest results might be chosen by that action to complete processing.
See Struts 2 We have two option to dene the architecture of our web
from inside
the forest application: XML le or Java Annotation.
Resume:
Request
Lifecycle in
Struts 2
Declaring
will we do
One example
21. Java Annotation version
Struts 2
A relatively new feature of the Java language, annotations allow
Juan Carlos
Figueroa you to add metadata directly to Java source. Struts 2 uses Java
Duran
annotations in this way. If you don't want to use XML les, the
Understanding
the context
declarative architecture mechanism can be congured to scan
Struts history
Java classes for Struts 2related annotations. We can see a
See Struts 2 example:
from outside
the forest @Results ({
@ R e s u l t ( name= i n p u t , v a l u e= / R e g i s t r a t i o n S u c c e s s . j s p
See Struts 2
from inside )
the forest @ R e s u l t ( v a l u e= / R e g i s t r a t i o n S u c c e s s . j s p )
Resume: })
Request p u b l i c c l a s s R e g i s t e r implements Action {
Lifecycle in public String execute () {
Struts 2
// B u s i n e s s logic for login
Declaring }
will we do
}
One example ...
22. XML le version
Struts 2
Juan Carlos Struts 2 allows you to use XML les to describe your
Figueroa
Duran application's desired Struts 2 architectural components. The
Understanding
ocial XML le where declare actions and results is the
the context struts.xml.
Struts history
a c t i o n name= L o g i n c l a s s = m a n n i n g . L o g i n
See Struts 2 r e s u l t / A c c o u n t P a g e . j s p/ r e s u l t
from outside
the forest r e s u l t name= i n p u t / L o g i n . j s p/ r e s u l t
/ a c t i o n
See Struts 2
a c t i o n name= R e g i s t r a t i o n
from inside
the forest r e s u l t / R e g i s t r a t i o n . j s p/ r e s u l t
/ a c t i o n
Resume:
Request a c t i o n name= R e g i s t e r c l a s s = m a n n i n g . R e g i s t e r
Lifecycle in r e s u l t / R e g i s t r a t i o n S u c c e s s . j s p/ r e s u l t
Struts 2
r e s u l t name= i n p u t / R e g i s t r a t i o n . j s p/ r e s u l t
Declaring / a c t i o n
will we do ...
One example
23. Understanding XML declarative architecture:package
name
Struts 2
Juan Carlos
Figueroa
Duran
Struts 2 packages are similar to Java packages. They providing
a mechanism for grouping your actions based on commona lity
Understanding
the context
of function or domain. The name attribute is a merely logical
Struts history name by which we can reference the package.
See Struts 2 1 s t r u t s
from outside 2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=
the forest
struts − d e f a u l t
See Struts 2 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l o
from inside
the forest m e t h o d= e x e c u t e
4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s p
Resume:
/ S a y H e l l o . j s p/ r e s u l t
Request
Lifecycle in 5 / a c t i o n
Struts 2 6 / p a c k a g e
Declaring 7 / s t r u t s
will we do
One example
24. Understanding XML declarative
architecture:namespace
Struts 2
As we've seen, the name space attribute is used to generate the
Juan Carlos
Figueroa
URL namespace to which the action of these package are
Duran mapped.
Understanding
the context http:// + localhost:8080 + /HelloYou + /actions + /MakeHello.action
Struts history
See Struts 2 protocol host name: port number servlet context package namespace action name.action
from outside
the forest
See Struts 2 1 s t r u t s
from inside
the forest 2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=
struts − d e f a u l t
Resume:
Request 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l o
Lifecycle in m e t h o d= e x e c u t e
Struts 2 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s p
Declaring / S a y H e l l o . j s p/ r e s u l t
will we do 5 / a c t i o n
One example 6 / p a c k a g e
7 / s t r u t s
25. Understanding XML declarative architecture:extends
Struts 2
Juan Carlos
Figueroa
Duran This important attribute names another package whose
Understanding
components should be inherited by the current package. Like
the context struts-default package.
Struts history
1 s t r u t s
See Struts 2 2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=
from outside
the forest struts − d e f a u l t
3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l o
See Struts 2 m e t h o d= e x e c u t e
from inside
the forest 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s p
/ S a y H e l l o . j s p/ r e s u l t
Resume:
Request 5 / a c t i o n
Lifecycle in 6 / p a c k a g e
Struts 2 7 / s t r u t s
Declaring
will we do
One example
26. Understanding XML declarative architecture:action
name
Struts 2
Juan Carlos
Figueroa
Duran
Understanding 1 s t r u t s
the context 2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=
Struts history struts − d e f a u l t
See Struts 2
3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l o
from outside m e t h o d= e x e c u t e
the forest 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s p
See Struts 2 / S a y H e l l o . j s p/ r e s u l t
from inside 5 / a c t i o n
the forest 6 / p a c k a g e
Resume: 7 / s t r u t s
Request
Lifecycle in
Struts 2
Declaring
will we do
One example
27. Understanding XML declarative architecture:class
Struts 2
Juan Carlos
Figueroa
Duran
In the class attribute specify a class to provide the action
Understanding implementation.
the context
Struts history
1 s t r u t s
2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=
See Struts 2 struts − d e f a u l t
from outside
the forest 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l o
m e t h o d= e x e c u t e
See Struts 2
from inside 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s p
the forest / S a y H e l l o . j s p/ r e s u l t
Resume: 5 / a c t i o n
Request 6 / p a c k a g e
Lifecycle in 7 / s t r u t s
Struts 2
Declaring
will we do
One example
28. Understanding XML declarative architecture:method
Struts 2
Juan Carlos
Figueroa
Duran
Now for method attribute, we can mapped a method from
action dened in class attribute that contains the business logic.
Understanding
the context
Here we can specify the method to be invoked for execute there
Struts history business logic.
See Struts 2 1 s t r u t s
from outside 2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=
the forest
struts − d e f a u l t
See Struts 2 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l o
from inside
the forest m e t h o d= e x e c u t e
4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s p
Resume:
/ S a y H e l l o . j s p/ r e s u l t
Request
Lifecycle in 5 / a c t i o n
Struts 2 6 / p a c k a g e
Declaring 7 / s t r u t s
will we do
One example
29. Understanding XML declarative architecture:result
name
Struts 2
Juan Carlos
Figueroa
Duran
The name attribute is a like tag. And this tag should be used to
Understanding map the result to render.
the context
Struts history
1 s t r u t s
2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=
See Struts 2 struts − d e f a u l t
from outside
the forest 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l o
m e t h o d= e x e c u t e
See Struts 2
from inside 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s p
the forest / S a y H e l l o . j s p/ r e s u l t
Resume: 5 / a c t i o n
Request 6 / p a c k a g e
Lifecycle in 7 / s t r u t s
Struts 2
Declaring
will we do
One example
30. Understanding XML declarative architecture:type
Struts 2
Juan Carlos
Figueroa
Duran
Here we must specify to the browser the type of resource to
Understanding redirect. types: dispatcher, redirect, redirect-action.
the context
Struts history
1 s t r u t s
2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=
See Struts 2 struts − d e f a u l t
from outside
the forest 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l o
m e t h o d= e x e c u t e
See Struts 2
from inside 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s p
the forest / S a y H e l l o . j s p/ r e s u l t
Resume: 5 / a c t i o n
Request 6 / p a c k a g e
Lifecycle in 7 / s t r u t s
Struts 2
Declaring
will we do
One example
31. The goal
Struts 2
Juan Carlos
Figueroa
Duran
Understanding
the context
Struts history
See Struts 2
from outside
the forest
See Struts 2
from inside
the forest
Resume:
Request
Lifecycle in
Struts 2
Declaring
will we do
One example
32. The JSP code: JavaScript
Struts 2
Juan Carlos
Figueroa
Duran
% User user = ActionContext . getContext () %
Understanding form a c t i o n = M a k e H e l l o n a m e s p a c e=/ a c t i o n s method= p o s t
the context
Struts history t a b l e
t r
See Struts 2
from outside td a l i g n = r i g h t l a b e l U s e r name :/ l a b e l / td
the forest td i n p u t type= t e x t name= u s e r N a m e
See Struts 2
v a l u e= %=u s e r . g e t U s e r N a m e ( ) % // td
from inside / t r
the forest t r
Resume: td
Request % i f ( u s e r . getName ( )== ) { %
Lifecycle in ...
Struts 2
Declaring
will we do
One example
33. The JSP code: Tags
Struts 2
Juan Carlos
Figueroa
Duran
Understanding
the context
s : a c t i o n e r r o r /
Struts history
s : form a c t i o n = M a k e H e l l o n a m e s p a c e= / a c t i o n s method=
See Struts 2 p o s t
from outside
the forest s : t e x t f i e l d l a b e l = U s e r Name name= u s e r N a m e /
s : s u b m i t v a l u e= s a l u d a r ! a l i g n = c e n t e r /
See Struts 2
/ s : form
from inside
the forest
Resume:
Request
Lifecycle in
Struts 2
Declaring
will we do
One example
34. Struts conguration: struts.xml
Struts 2
Juan Carlos
Figueroa
Duran
Understanding
s t r u t s
the context
p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=
Struts history struts − d e f a u l t
See Struts 2 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l o
from outside m e t h o d= e x e c u t e
the forest
r e s u l t / j s p / S a y H e l l o . j s p/ r e s u l t
See Struts 2 / a c t i o n
from inside
/ p a c k a g e
the forest
/ s t r u t s
Resume:
Request
Lifecycle in
Struts 2
Declaring
will we do
One example
35. Action: MakeHello
Struts 2
Juan Carlos
Figueroa public class MakeHello extends ActionSupport {
Duran
Understanding
private String userName ;
the context
p u b l i c S t r i n g e x e c u t e ( ) throws Exception {
Struts history
r e t u r n SUCCESS ;
See Struts 2 }
from outside
the forest
p u b l i c S t r i n g getUserName ( ) {
See Struts 2
from inside r e t u r n userName ;
the forest }
Resume:
Request p u b l i c void setUserName ( S t r i n g userName ) {
Lifecycle in t h i s . userName = userName ;
Struts 2 }
Declaring }
will we do
One example
36. JSP: SayHello
Struts 2
Juan Carlos
Figueroa
Duran
Understanding html
the context head
Struts history meta h t t p − e q u i v= C o n t e n t −Type content= t e x t / h t m l ;
c h a r s e t =UTF−8
See Struts 2
from outside t i t l e H e l l o y o u/ t i t l e
the forest / head
See Struts 2 body
from inside h1 H e l l o s : p r o p e r t y v a l u e = u s e r N a m e / ! ! ! / h1
the forest / body
Resume: / html
Request
Lifecycle in
Struts 2
Declaring
will we do
One example