Building Struts 2 Applications

Document Sample
Building Struts 2 Applications Powered By Docstoc
					Building Struts 2 Applications




                  Struts University Series
  Building S2 Applications
Can we use a conventional design?
Why use frameworks?
What value does Struts 2 add?
  Building S2 Applications
Can we use a conventional design?
Why use frameworks?
What value does Struts 2 add?
Can we use a conventional design?
Can we use a conventional design?

     “The essential purpose of MVC
            is to bridge the gap
  between the human user's mental model
           and the digital model
        that exists in the computer.”

             [Reenskaug 2006|
Can we use a conventional design?
Can we use a conventional design?
Can we use a conventional design?
Can we use a conventional design?
Can we use a conventional design?



Desktop MVC
 View pulls state
 All three components interconnected

Enterprise MVC
 Controller pushes state
 Controller connects View with Model
           Why bother?
For a small application, don't
For a large application, MVC ...
 Helps page share code
 Encapsulates navigation

 Creates robust pages
          Welcome to the Jungle
<% User user = ActionContext.getContext() %>
<form action="Profile_update.action" method="post">
 <table>
  <tr>
     <td> align="right"<label>First name:</label></td>
     <td><input type="text" name="user.firstname"
         value="<%=user.getFirstname() %> /></td>
  </tr>
  <tr>
    <td>
     <input type="radio" name="user.gender" value="0"
       id="user.gender0"
      <% if (user.getGender()==0) { %>
      checked="checked" %> } %> />
      <label for="user.gender0">Female</label>
...
    Separating code and markup
<s:actionerror/>
<s:form action="Profile_update" validate="true">
 <s:textfield label="Username" name="username"/>
 <s:password label="Password" name="password"/>
 <s:password label="(Repeat) Password" name="password2"/>
 <s:textfield label="Full Name" name="fullName"/>
 <s:textfield label="From Address" name="fromAddress"/>
 <s:textfield label="Reply To Address" name="replyToAddress"/>
 <s:submit value="Save" name="Save"/>
 <s:submit action="Register_cancel" value="Cancel"
name="Cancel"
   onclick="form.onsubmit=null"/>
</s:form>
       Review – Why MVC?
(1) Model        (a) Renders Model
(2) View         (b) Selects View
(3) Controller   (c) Retains State
       Review – Why MVC?
(1) Model        (a) Renders Model
(2) View         (b) Selects View
(3) Controller   (c) Retains State
       Review – Why MVC?
(1) Model        (a) Renders Model
(2) View         (b) Selects View
(3) Controller   (c) Retains State
       Review – Why MVC?
(1) Model        (a) Renders Model
(2) View         (b) Selects View
(3) Controller   (c) Retains State
       Review – Why MVC?
(1) Model        (a) Renders Model
(2) View         (b) Selects View
(3) Controller   (c) Retains State
       Review – Why MVC?
(1) Model        (a) Renders Model
(2) View         (b) Selects View
(3) Controller   (c) Retains State
       Review – Why MVC?
(1) Model        (a) Renders Model
(2) View         (b) Selects View
(3) Controller   (c) Retains State
       Review – Why MVC?
(1) Model        (c) Retains State
(2) View         (a) Renders Model
(3) Controller   (b) Selects View
  Building S2 Applications
Can we use a conventional design?
Why use frameworks?
What components does the framework
add to the mix?
     Why Frameworks?
Code reuse    Separation of
Incremental   Concerns
development   Don't Repeat
Long-term     Yourself
maintenance
by a team
     Why Frameworks?
Code reuse    Separation of
Incremental   Concerns
development   Don't Repeat
Long-term     Yourself
maintenance
by a team
     Why Frameworks?
Code reuse    Separation of
Incremental   Concerns
development   Don't Repeat
Long-term     Yourself
maintenance
by a team
     Why Frameworks?
Code reuse    Separation of
Incremental   Concerns
development   Don't Repeat
Long-term     Yourself
maintenance
by a team
     Why Frameworks?
Code reuse    Separation of
Incremental   Concerns
development   Don't Repeat
Long-term     Yourself
maintenance
by a team
Why MVC Frameworks?
Review – Why Frameworks?
Code re***    ********** of
In*********   Concerns
development   Don't ******
Long-term     Yourself
m**********
by a team
Review – Why Frameworks?
Code reuse    ********** of
In*********   Concerns
development   Don't ******
Long-term     Yourself
m**********
by a team
Review – Why Frameworks?
Code reuse    ********** of
Incremental   Concerns
development   Don't ******
Long-term     Yourself
m**********
by a team
Review – Why Frameworks?
Code reuse    ********** of
Incremental   Concerns
development   Don't ******
Long-term     Yourself
maintenance
by a team
Review – Why Frameworks?
Code reuse    Separation of
Incremental   Concerns
development   Don't ******
Long-term     Yourself
maintenance
by a team
Review – Why Frameworks?
Code reuse    Separation of
Incremental   Concerns
development   Don't Repeat
Long-term     Yourself
maintenance
by a team
Review – Why Frameworks?
Code reuse    Separation of
Incremental   Concerns
development   Don't Repeat
Long-term     Yourself
maintenance
by a team
  Building S2 Applications
Can we use a conventional design
Why use frameworks?
What value does Struts 2 add?
     Why add anything?
Servlets
Server Pages
Session Tracking
Localization
        Why add anything?
Servlets
   Heavy-weight, hard to configure, web bound
Server Pages
Session Tracking
Localization
        Why add anything?
Servlets
   Heavy-weight, hard to configure, web bound
Server Pages
   Encourages mixing code with markup
Session Tracking
Localization
        Why add anything?
Servlets
   Heavy-weight, hard to configure, web bound
Server Pages
   Encourages mixing code with markup
Session Tracking
   URL writing is verbose
Localization
         Why add anything?
Servlets
   Heavy-weight, hard to configure, web bound
Server Pages
   Encourages mixing code with markup
Session Tracking
   URL writing is verbose
Localization
   No direct support in scriptlets
      Why add anything?

               Struts
The nearly invisible pieces that hold up
    buildings, houses, and bridges.
 What Value does S2 Add?
Workflow Components
Elegant Architecture
 What Value does S2 Add?
Workflow Components
 Everyday use
 Input / Output

Elegant Architecture
 What Value does S2 Add?
Workflow Components
 Everyday use
 Input / Output

Elegant Architecture
 Easy to extend
 Easy to customize
   Workflow Components
Action handler
Result handler
Custom tags
      Workflow Components
Action handler (“Model”)
   Interacts with other layers
Result handler
Custom tags
      Workflow Components
Action handler
   Interacts with other layers
Result handler (“Controller”)
   Dispatches to server page, HTML, PDF, ...
Custom tags
      Workflow Components
Action handler
   Interacts with other layers
Result handler
   Dispatches to server page, HTML, PDF, ...
Custom tags (“View”)
   Render dynamic content
Struts 2 Workflow
                 struts.xml
<struts>
    <package name="default"
             extends="struts-default">

       <action name="Hello" class="Hello">
           <result>/pages/Hello.jsp</result>
       </action>

        <!-- Add your actions here -->
    </package>
</struts>
                 Hello.java
import com.opensymphony.xwork2.ActionSupport;

public class Hello extends ActionSupport {

    public String execute() throws Exception {

        return SUCCESS;

    }

}
              Hello.jsp
                  s"                 "
<%@ taglib prefix=" uri="/struts-tags %>

<html>
<head>
    <title>Hello</title>
</head>

<body>
<h2><s:text name="hello.message"/></h2>
</body>
</html>
             resources.properties
hello.message = Congratulations! Struts is up and running ...
# Add your messages here ...
Hello World!
           Welcome.jsp
                   s"                 "
<%@ taglib prefix=" uri="/struts-tags %>
<html>
<body>
<h3>Options</h3>
<ul>
<li><a href="                         "
             <s:url action="Register"/> >
  Register</a></li>
<li><a href="                      ">
             <s:url action="Logon"/>
  Sign On</a></li>
</ul>
</body>
</html>
Pick your Poison!
    Review – Workflow MVC
(1) Model        (a) Result Handler
(2) View         (b) Action Handler
(3) Controller   (c) Custom Tag
    Review – Workflow MVC
(1) Model        (a) Result Handler
(2) View         (b) Action Handler
(3) Controller   (c) Custom Tag
    Review – Workflow MVC
(1) Model        (a) Result Handler
(2) View         (b) Action Handler
(3) Controller   (c) Custom Tag
    Review – Workflow MVC
(1) Model        (a) Result Handler
(2) View         (b) Action Handler
(3) Controller   (c) Custom Tag
    Review – Workflow MVC
(1) Model        (a) Result Handler
(2) View         (b) Action Handler
(3) Controller   (c) Custom Tag
    Review – Workflow MVC
(1) Model        (a) Result Handler
(2) View         (b) Action Handler
(3) Controller   (c) Custom Tag
    Review – Workflow MVC
(1) Model        (a) Result Handler
(2) View         (b) Action Handler
(3) Controller   (c) Custom Tag
    Review – Workflow MVC
(1) Model        (b) Action Handler
(2) View         (c) Custom Tag
(3) Controller   (a) Result Handler
    Review – Components
The key framework components are:
 A***** H******
 R***** H******

 C***** T***

The Struts 2 default configuration file is
named ******.xml.
To reference the Struts taglib, use the
URI “/******-*****”.
    Review – Components
The key framework components are:
 Action Handler
 R***** H******

 C***** T***

The Struts 2 default configuration file is
named ******.xml.
To reference the Struts taglib, use the
URI “/******-*****”.
    Review – Components
The key framework components are:
 Action Handler
 Result Handler

 C***** T***

The Struts 2 default configuration file is
named ******.xml.
To reference the Struts taglib, use the
URI “/******-*****”.
    Review – Components
The key framework components are:
 Action Handler
 Result Handler

 Custom Tags

The Struts 2 default configuration file is
named ******.xml.
To reference the Struts taglib, use the
URI “/******-*****”.
    Review – Components
The key framework components are:
 Action Handler
 Result Handler

 Custom Tags

The Struts 2 default configuration file is
named struts.xml.
To reference the Struts taglib, use the
URI “/******-*****”.
    Review – Components
The key framework components are:
 Action Handler
 Result Handler

 Custom Tags

The Struts 2 default configuration file is
named struts.xml.
To reference the Struts 2 taglib, use the
URI “/struts-tags”.
    Review – Components
The key framework components are
 Action handler
 Result handler

 Custom tags

The Struts 2 default configuration file is
named struts.xml.
To reference the Struts 2 taglib, use the
URI “/struts-tags”.
    Struts 2 Architecture
Interceptors
Value Stack
Expression Language
    Struts 2 Architecture
Interceptors
Value Stack
Expression Language
Struts 2 in a Nutshell   The web browser requests the
                         page
                          The Filter Dispatcher looks at the
                         request and determines the
                         appropriate Action
                         The Interceptors automatically apply
                         common functionality to the request
                         like workflow, validation, and file
                         upload handling
                         The Action method executes, usually
                         storing and/or retrieving information
                         from a database
                         The Result either renders the output
                         (images, PDF, JSON) or dispatches
                         to another resource, like a JavaServer
                         Page, to complete the response.
                         The server page utilizes Struts Tags
                         and the expression language to
                         output dynamic data
Interceptors: Domain AOP
             Interceptors allow
             custom code into the call
             stack
             Much of the core
             functionality of the
             framework is
             implemented as
             Interceptors
             Custom Interceptors are
             easy to add
               TimerInterceptor
    TimerInterceptor is the simplest Interceptor
    Times the execution of the Action
public String intercept(ActionInvocation invocation)
  throws Exception {
    if (log.isInfoEnabled()) {
      long startTime = System.currentTimeMillis();
       String result = invocation.invoke();
       long executionTime =
         System.currentTimeMillis() - startTime;
       String namespace =
         invocation.getProxy().getNamespace();
         …
}
                  Preparable
public interface Preparable {
    void prepare() throws Exception;
}


 protected void intercept(ActionInvocation invocation)
   throws Exception {
   Object action = invocation.getAction();
   if (action instanceof Preparable) {
       ...
      ((Preparable) action).prepare();
       ...
   }
 }
}
     Review - Interceptor
Interceptors inject custom **** into the
request processing pipeline
Much of the core ************* of the
framework is implemented as
Interceptors
Custom Interceptors are (hard / easy) to
add
     Review - Interceptor
Interceptors inject custom logic into the
request processing pipeline
Much of the core ************* of the
framework is implemented as
Interceptors
Custom Interceptors are (hard / easy) to
add
     Review - Interceptor
Interceptors inject custom logic into the
request processing pipeline
Much of the core functionality of the
framework is implemented as
Interceptors
Custom Interceptors are (hard / easy) to
add
     Review - Interceptor
Interceptors inject custom logic into the
request processing pipeline
Much of the core functionality of the
framework is implemented as
Interceptors
Custom Interceptors are (hard / easy) to
add
     Review - Interceptor
Interceptors inject custom code into the
request processing pipeline
Much of the core functionality of the
framework is implemented as
Interceptors
Custom Interceptors are easy to add
      Struts Architecture
Interceptors
Value Stack
Expression Language
What is the
ValueStack?
The ValueStack builds a
stack of objects
Objects are examined to
find property values
The ValueStack allows
the expression language
to find property values
across multiple objects
What is the
ValueStack?
The ValueStack builds a
stack of objects
Objects are examined to
find property values
The ValueStack allows
the expression language
to find property values
across multiple objects
What is the
ValueStack?
The ValueStack builds a
stack of objects
Objects are examined to
find property values
The ValueStack allows
the expression language
to find property values
across multiple objects
What is the
ValueStack?
The ValueStack builds a
stack of objects
Objects are examined to
find property values
The ValueStack allows
the expression language
to find property values
across multiple objects
    How is the ValueStack used?
The Action instance is always pushed onto
the ValueStack
The Model is pushed on by the
ModelDrivenInterceptor
The UI tags use it to push values on during
their scope and evaluate expressions
   The <s:iterator> tag pushes the current item onto the stack
   The <s:bean> tag pushes a bean instance onto the stack
   The <s:property> tag evaluates an expression against the stack
   All tag attribute values are evaluated against the stack when being
    set onto the tag instances
    How is the ValueStack used?
The Action instance is always pushed onto
the ValueStack
The Model is pushed on by the
ModelDrivenInterceptor
The UI tags use it to push values on during
their scope and evaluate expressions
   The <s:iterator> tag pushes the current item onto the stack
   The <s:bean> tag pushes a bean instance onto the stack
   The <s:property> tag evaluates an expression against the stack
   All tag attribute values are evaluated against the stack when being
    set onto the tag instances
    How is the ValueStack used?
The Action instance is always pushed onto
the ValueStack
The Model is pushed on by the
ModelDrivenInterceptor
The UI tags use it to push values on during
their scope and evaluate expressions
   The <s:iterator> tag pushes the current item onto the stack
   The <s:bean> tag pushes a bean instance onto the stack
   The <s:property> tag evaluates an expression against the stack
   All tag attribute values are evaluated against the stack when being
    set onto the tag instances
    How is the ValueStack used?
The Action instance is always pushed onto
the ValueStack
The Model is pushed on by the
ModelDrivenInterceptor
The UI tags use it to push values on during
their scope and evaluate expressions
   The <s:iterator> tag pushes the current item onto the stack
   The <s:bean> tag pushes a bean instance onto the stack
   The <s:property> tag evaluates an expression against the stack
   All tag attribute values are evaluated against the stack when being
    set onto the tag instances
      Review - ValueStack
The ValueStack builds a ***** of objects.
Objects are examined to find property ******.
The ValueStack allows the ********** ******** to find
property values across multiple objects.
      Review - ValueStack
The ValueStack builds a stack of objects.
Objects are examined to find property ******.
The ValueStack allows the ********** ******** to find
property values across multiple objects.
      Review - ValueStack
The ValueStack builds a stack of objects
Objects are examined to find property values
The ValueStack allows the ********** ******** to find
property values across multiple objects
      Review - ValueStack
The ValueStack builds a stack of objects
Objects are examined to find property values
The ValueStack allows the expression language to find
property values across multiple objects
      Review - ValueStack
The ValueStack builds a stack of objects
Objects are examined to find property values
The ValueStack allows the expression language to find
property values across multiple objects
      Struts Architecture
Interceptors
Value Stack
Expression Language
OGNL Expression Language
For expressions, the framework uses
OGNL (Object Graph Navigation Language)
   An expression and binding language for getting and setting
    properties of Java objects
   Normally the same expression is used for both getting and
    setting the value of a property
   Easy to learn, yet powerful
   Incrementally compiled expressions - fast!
   Embedded everywhere – views, ValueStack, *.xml
   Independent Open Source project - http://www.ognl.org
OGNL Expression Language
For expressions, the framework uses
OGNL (Object Graph Navigation Language)
   An expression and binding language for getting and setting
    properties of Java objects
   Normally the same expression is used for both getting and
    setting the value of a property
   Easy to learn, yet powerful
   Incrementally compiled expressions - fast!
   Embedded everywhere – views, ValueStack, *.xml
   Independent Open Source project - http://www.ognl.org
OGNL Expression Language
For expressions, the framework uses
OGNL (Object Graph Navigation Language)
   An expression and binding language for getting and setting
    properties of Java objects
   Normally the same expression is used for both getting and
    setting the value of a property
   Easy to learn, yet powerful
   Incrementally compiled expressions - fast!
   Embedded everywhere – views, ValueStack, *.xml
   Independent Open Source project - http://www.ognl.org
OGNL Expression Language
For expressions, the framework uses
OGNL (Object Graph Navigation Language)
   An expression and binding language for getting and setting
    properties of Java objects
   Normally the same expression is used for both getting and
    setting the value of a property
   Easy to learn, yet powerful
   Incrementally compiled expressions - fast!
   Embedded everywhere – views, ValueStack, *.xml
   Independent Open Source project - http://www.ognl.org
OGNL Expression Language
For expressions, the framework uses
OGNL (Object Graph Navigation Language)
   An expression and binding language for getting and setting
    properties of Java objects
   Normally the same expression is used for both getting and
    setting the value of a property
   Easy to learn, yet powerful
   Incrementally compiled expressions - fast!
   Embedded everywhere – views, ValueStack, *.xml
   Independent Open Source project - http://www.ognl.org
OGNL Expression Language
For expressions, the framework uses
OGNL (Object Graph Navigation Language)
   An expression and binding language for getting and setting
    properties of Java objects
   Normally the same expression is used for both getting and
    setting the value of a property
   Easy to learn, yet powerful
   Incrementally compiled expressions - fast!
   Embedded everywhere – views, ValueStack, *.xml
   Independent Open Source project - http://www.ognl.org
OGNL Expression Language
For expressions, the framework uses
OGNL (Object Graph Navigation Language)
   An expression and binding language for getting and setting
    properties of Java objects
   Normally the same expression is used for both getting and
    setting the value of a property
   Easy to learn, yet powerful
   Incrementally compiled expressions - fast!
   Embedded everywhere – views, ValueStack, *.xml
   Independent Open Source project - http://www.ognl.org
OGNL Expression Language
For expressions, the framework uses
OGNL (Object Graph Navigation Language)
   An expression and binding language for getting and setting
    properties of Java objects
   Normally the same expression is used for both getting and
    setting the value of a property
   Easy to learn, yet powerful
   Incrementally compiled expressions - fast!
   Embedded everywhere – views, ValueStack, *.xml
   Independent Open Source project - http://www.ognl.org
             OGNL samples
        OGNL                         Result

user.name               getUser().getName()

user.toString()         getUser().toString()

item.categories[0]      First element of Categories
                        collection
@com.example.Test@foo   Calls the static foo() method on
()                      the com.example.Test class

name in {null,”fred”}   True if name is null or “fred”

categories.{name}       Calls getName() on each Category in
                        the collection, returning a new
                        collection (projection)
             OGNL samples
        OGNL                         Result

user.name               getUser().getName()

user.toString()         getUser().toString()

item.categories[0]      First element of Categories
                        collection
@com.example.Test@foo   Calls the static foo() method on
()                      the com.example.Test class

name in {null,”fred”}   True if name is null or “fred”

categories.{name}       Calls getName() on each Category in
                        the collection, returning a new
                        collection (projection)
             OGNL samples
        OGNL                         Result

user.name               getUser().getName()

user.toString()         getUser().toString()
item.categories[0]      First element of Categories
                        collection
@com.example.Test@foo   Calls the static foo() method on
()                      the com.example.Test class

name in {null,”fred”}   True if name is null or “fred”

categories.{name}       Calls getName() on each Category in
                        the collection, returning a new
                        collection (projection)
             OGNL samples
        OGNL                         Result

user.name               getUser().getName()

user.toString()         getUser().toString()

item.categories[0]      First element of Categories
                        collection
@com.example.Test@foo   Calls the static foo() method on
()                      the com.example.Test class

name in {null,”fred”}   True if name is null or “fred”

categories.{name}       Calls getName() on each Category in
                        the collection, returning a new
                        collection (projection)
             OGNL samples
        OGNL                         Result

user.name               getUser().getName()

user.toString()         getUser().toString()

item.categories[0]      First element of Categories
                        collection
@com.example.Test@foo   Calls the static foo() method on
()                      the com.example.Test class
name in {null,”fred”}   True if name is null or “fred”

categories.{name}       Calls getName() on each Category in
                        the collection, returning a new
                        collection (projection)
             OGNL samples
        OGNL                         Result

user.name               getUser().getName()

user.toString()         getUser().toString()

item.categories[0]      First element of Categories
                        collection
@com.example.Test@foo   Calls the static foo() method on
()                      the com.example.Test class

name in {null,”fred”}   True if name is null or “fred”

categories.{name}       Calls getName() on each Category in
                        the collection, returning a new
                        collection (projection)
             OGNL samples
        OGNL                         Result

user.name               getUser().getName()

user.toString()         getUser().toString()

item.categories[0]      First element of Categories
                        collection
@com.example.Test@foo   Calls the static foo() method on
()                      the com.example.Test class

name in {null,”fred”}   True if name is null or “fred”

categories.{name}       Calls getName() on each Category in
                        the collection, returning a new
                        collection (projection)
        Review - OGNL
OGNL stands for O****** G****
Navigation L*********
OGNL is an expression and *******
language for getting and setting
properties of Java objects
Within the framework, OGNL is ********
everywhere – views, ValueStack, XML
configuration files
        Review - OGNL
OGNL stands for Object Graph
Navigation Language
OGNL is an expression and *******
language for getting and setting
properties of Java objects
Within the framework, OGNL is ********
everywhere – views, ValueStack, XML
configuration files
        Review - OGNL
OGNL stands for Object Graph
Navigation Language
OGNL is an expression and binding
language for getting and setting
properties of Java objects
Within the framework, OGNL is ********
everywhere – views, ValueStack, XML
configuration files
        Review - OGNL
OGNL stands for Object Graph
Navigation Language
OGNL is an expression and binding
language for getting and setting
properties of Java objects
Within the framework, OGNL is
embedded everywhere – views,
ValueStack, XML configuration files.
        Review - OGNL
OGNL stands for Object Graph
Navigation Language
OGNL is an expression and binding
language for getting and setting
properties of Java objects
Within the framework, OGNL is
embedded everywhere – views,
ValueStack, XML configuration files.
  Building S2 Applications
Can we use a conventional design?
Why use framework?
What value does Struts 2 add?
  Building S2 Applications
Can we use a conventional design?
Why use frameworks?
What value does Struts 2 add?
  Building S2 Applications
Why use frameworks?
 Code Reuse

 Separation of Concerns

What value does Struts 2 add?
  Building S2 Applications
Why use frameworks?
 Code Reuse

 Separation of Concerns

What value does Struts 2 add?
  Building S2 Applications
Why use frameworks?
 Code Reuse

 Separation of Concerns

What value does Struts 2 add?
 Actions, Results, Tags

 Interceptors, Value Stack, Expressions
Struts University Series