You are on page 1of 62

Building Applications

Struts University Series


Building Applications
How are web applications organized?
What components does the framework
add to the mix?
Can traditional architectures be applied
to web applications?
Building Applications
Web Application Infrastructure
Struts Framework Components
Model View Controller Architecture
Struts Architecture
Web Applications
A web application uses a web site as the
front-end to a more typical application.
In a web application client data input
executes business logic on the server.
 Minnesota State Colleges & Universities
 http://krypton.mnsu.edu/~spiral/eta/glossary/indxGlossOOxml.html
Web Applications
Web site
 HTML, HTTP
Web application
 Common Gateway Interface (CGI)
 Perl
 PHP

 Active Server Pages / ASP.NET

 Java / JavaServer Faces


Java Web Applications
Document Root
 *.html, *.jsp
 JSP tags
 WEB-INF (Web Application Infrastructure)
 web.xml (Web Application Deployment Descriptor)
 Servlets, Filters, Listeners, Security Descriptors
 lib
 *.JAR (Java Archive)
 classes
 *.class, *.properties, *.tld (tag library descriptor)
Application Infrastructure
Review
In a web application client data input
executes ******** ***** on the server.
To place a resource on the classpath,
you can place it under the ******* folder.
Review
In a web application client data input
executes business logic on the server.
To place a resource on the classpath,
you can place it under the classes folder.
Review
(1) CGI (a) Tag Library Descriptor
(2) WEB-INF (b) Java Archive
(3) web.xml (c) Web Application Infrastructure
(d) Computer Gateway Interface
(4) JAR
(e) Web Application Deployment
(5) TLD Descriptor
Review
(1) CGI (d) Computer Gateway Interface
(2) WEB-INF (c) Web Application Infrastructure
(3) web.xml (e) Web Application Deployment
Descriptor
(4) JAR (b) Java Archive
(5) TLD (a) Tag Library Descriptor
Building Applications
Web Application Infrastructure
Struts Framework Components
Model View Controller Architecture
Struts Architecture
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
Struts Components
Action handler
Result handler
Custom tags
Struts Components
Action handler
 Interacts with other layers
Result handler
Custom tags
Struts Components
Action handler
 Interacts with other layers
Result handler
 Dispatches to server page, HTML, PDF, ...
Custom tags
Struts Components
Action handler
 Interacts with other layers
Result handler
 Dispatches to server page, HTML, PDF, ...
Custom tags
 Render dynamic content
struts.xml
<struts>
<include file="struts-default.xml"/>

<package name="default"
extends="struts-default">

<default-action-ref name="Hello"/>

<action name="Hello" class="Hello">


<result>/pages/Hello.jsp</result>
</action>

<!-- Add your actions here -->


</package>
</struts>
Hello.jsp
<%@ taglib prefix="s" uri="/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
<%@ taglib prefix="s" uri="/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 – Why Frameworks?
Code re*** ********** of
In********* Concerns
development Don't ******
Long-term Yourself
m**********
by a team
Review – Why Frameworks?
Code reuse Separation of
Incremental Concerns
development Don't Repeat
Long-term Yourself
maintenance
by a team
Review
The key framework components are:
 ****** *******
 ****** *******

 ****** ****

The Struts 2 default configuration file is


named ******.xml.
To reference the Struts taglib, use the
URI “*****”.
Review
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 “/tags”.
Building Applications
Web Application Infrastructure
Struts Components
Model View Controller Architecture
Struts Architecture
Model View Controller
Model View Controller
Review
(1) Model (a) Renders Model
(2) View (b) Selects View
(3) Controller (c) Retains State
Review
(1) Model (c) Retains State
(2) View (a) Renders Model
(3) Controller (b) Selects View
Struts Architecture
Interceptors
Value Stack
Expression Language
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 allow 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 allow 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
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 on
 The <s:property> tag evaluates an expression against the
ValueStack
 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 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 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 Object Graph **********
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
embedded everywhere – views,
ValueStack, xml configuration files.
Building Web Applications
How are web applications organized?
What components does the framework
add to the mix?
Can traditional architectures be applied
to web applications?
Building Web Applications
How are web applications organized?
 WEB-INF, web.xml, /classes

What components does the framework


add to the mix?
Can traditional architectures be applied
to web applications?
Building Web Applications
How are web applications organized?
 WEB-INF, web.xml, /classes

What components does the framework


add to the mix?
 Actions, Results, Tags

Can traditional architectures be applied


to web applications?
Building Web Applications
How are web applications organized?
 WEB-INF, web.xml, /classes

What components does the framework


add to the mix?
 Actions, Results, Tags

Can traditional architectures be applied


to web applications?
 Absolutely!
Struts University Series

You might also like