Download as pdf or txt
Download as pdf or txt
You are on page 1of 103

Building Web Applications

Struts University Series


Building Web Applications
Why build web applications?
Can we use a conventional design?
How are web applications organized?
What value does Struts 2 add?
Building Web Applications
Why build web applications?
Can we use a conventional design?
How are web applications organized?
What value does Struts 2 add?
Why build web applications?
Easier to find, use, and deploy
Browsers pre-installed
No desktop to maintain
Suitable for intranet or Internet
Why build web applications?
Downside
 Complex task
 Web site front-end
 Desktop application equivalent
Are web applications so different?
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
Are web applications different?
Regular Web Site
 HTML, JavaScript, Graphics
Dynamic Web Application
 Common Gateway Interface (CGI)
 Perl, PHP
 CGI Equivalents
 Fast CGI, Java, ASP.NET
How do we build web sites?
Create dynamic content
 via code
 via server pages

 via client-side scripting

 and all three in any combination


Dynamic content via code
Dynamic content via server pages
Plain Old JSP
<% 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>
...
<td>
<input type="radio" name="user.gender" value="0"
id="user.gender0"
<% if (user.getGender()==0) { %>
checked="checked" %> } %> />
<label for="user.gender0">Female</label>
...
Server page as rendered
Dynamic content via JavaScript
How much HTML?
What is HTML?
What tools can we use to write HTML?
Can we start with a “Hello World” page?
What are the standard elements?
What are the most common HTML tags?
How much HTML?
Can we dress up “Hello World”?
Is there an easier way to style pages?
How do we add JavaScript to a page?
How do we prepare images for a page?
How do we add Flash to a page?
Review
In a web application client data input
executes ******** ***** on the server.
Review
In a web application client data input
executes business logic on the server.
Building Web Applications
Why web applications?
Can we use a conventional design?
How are web applications organized?
What value does Struts 2 add?
Can we use a conventional design?
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
Separating code and markup
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
Building Web Applications
Why web applications?
Can we use a conventional design?
How are web applications organized?
What components does Struts 2 add?
How are web applications organized?

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)
How are web applications organized?
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
Review
To place a resource on the classpath,
you can place it under the ******* folder.
Review
To place a resource on the classpath,
you can place it under the classes folder.
Building Web Applications
Why web applications?
Can we use a conventional design?
How are web applications organized?
What value does Struts 2 add?
What value does Struts 2 add?

Why add anything?


What components does Struts 2 add?
How do the Struts internals work?
Why use a framework?
What value does Struts 2 add?

Why add anything?


What comes with Struts?
How do the Struts internals work?
Why use a framework?
Why add anything?
Servlets
Server Pages
Session Tracking
Localization
An embarrassment of riches
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 Struts 2 add?

Why add anything?


What comes with Struts?
How do the Struts internals work?
Why use a framework?
What comes with Struts?

Custom tags
Action handler
Result handler
What value does Struts 2 add?

Custom tags
 Render dynamic content
Action handler
Result handler
What value does Struts 2 add?

Custom tags
 Render dynamic content
Action handler
 Interacts with other layers
Result handler
What value does Struts 2 add?

Custom tags
 Render dynamic content
Action handler
 Interacts with other layers
Result handler
 Dispatches to server page, HTML, PDF, ...
What value does Struts 2 add?

Custom tags
 Render dynamic content (View)
Action handler
 Interacts with other layers (Model)
Result handler
 Dispatches to server page (Controller)
Hello.jsp
<%@ taglib prefix="s" uri="/tags" %>
<html>
<body>
<h2><s:text name="message"/></h2>
</body>
</html>
Hello.properties
message = Congratulations! Struts is up and running ...
# Add your messages here ...
Hello_es.properties
message = ¡Struts está bien! ...
# Add your messages here ...
struts.xml
<struts>
<package name="default" extends="struts-default">
<action name="Hello">
<result>/Hello.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</struts>
Hello World!
Hello.jsp
<h3>Options</h3>
<ul><li>
<s:url id="en" action="Hello">
<s:param name="request_locale">en</s:param>
</s:url>
<s:a href="%{en}">English</s:a>
</li><li>
<s:url id="es" action="Hello">
<s:param name="request_locale">es</s:param>
</s:url>
<s:a href="%{es}">Español</s:a>
</li></ul>
Pick your Poison!
What does Struts 2 add?

Why add anything?


What value does Struts 2 add?
How do the Struts internals work?
Why use a framework?
Struts Architecture
Struts Architecture

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 renders the output, be it
HTML, images, or PDF, to the
browser
Struts Internals
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.
What components does Struts 2 add?

Why add anything?


What value does Struts 2 add?
How do the Struts internals work?
Why use a framework?
Why Frameworks?
Code reuse
Incremental development
Long-term maintenance
by a team
Why Frameworks?
Code reuse
Incremental development
Long-term maintenance
by a team
Why Frameworks?
Code reuse
Incremental development
Long-term maintenance
by a team
Why Frameworks?
Code reuse
Incremental development
Long-term maintenance
by a team
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
 Custom tags
 Action handler

 Result handler

The Struts 2 default configuration file is


named struts.xml.
To reference the Struts 2 taglib, use the
URI “/tags”.
Struts University Series
Building Web Applications
Why web applications?
Can we use a conventional design?
How are web applications organized?
What components does Struts 2 add?
Building Web Applications
Why web applications?

Can we use a conventional design?


How are web applications organized?
What components does Struts 2 add?
Building Web Applications
Why web applications?

Can we use a conventional design?


 Absolutely!

How are web applications organized?


What components does Struts 2 add?
Building Web Applications
Why web applications?

Can we use a conventional design?


 Absolutely!

How are web applications organized?


 WEB-INF, web.xml, /classes

What components does Struts 2 add?


Building Web Applications
Why web applications?

Can we use a conventional design?


 Absolutely!

How are web applications organized?


 WEB-INF, web.xml, /classes

What components does Struts 2 add?


 Tags, Actions, Results
Building Web Applications
....
Struts University Series

You might also like