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 Common Gateway Interface (CGI)
Perl  PHP  Active Server Pages / ASP.NET  Java / JavaServer Faces

Web application

Java Web Applications
Document Root

*.html, *.jsp

JSP tags web.xml (Web Application Deployment Descriptor)

WEB-INF (Web Application Infrastructure)

Servlets, Filters, Listeners, Security Descriptors *.JAR (Java Archive) *.class, *.properties, *.tld (tag library descriptor)

lib

classes

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 (2) WEB-INF (3) web.xml (4) JAR (5) TLD
(a) Tag Library Descriptor (b) Java Archive (c) Web Application Infrastructure (d) Computer Gateway Interface (e) Web Application Deployment Descriptor

Review
(1) CGI (2) WEB-INF (3) web.xml (4) JAR (5) TLD
(d) Computer Gateway Interface (c) Web Application Infrastructure (e) Web Application Deployment Descriptor (b) Java Archive (a) Tag Library Descriptor

Building Applications
Web Application Infrastructure Struts Framework Components
Model View Controller Architecture Struts Architecture

Why Frameworks?
Code reuse Incremental development Long-term maintenance by a team Separation of Concerns Don't Repeat Yourself

Why Frameworks?
Code reuse Incremental development Long-term maintenance by a team Separation of Concerns Don't Repeat Yourself

Why Frameworks?
Code reuse Incremental development Long-term maintenance by a team Separation of Concerns Don't Repeat Yourself

Why Frameworks?
Code reuse Incremental development Long-term maintenance by a team Separation of Concerns Don't Repeat Yourself

Why Frameworks?
Code reuse Incremental development Long-term maintenance by a team Separation of Concerns Don't Repeat Yourself

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 Dispatches to server page, HTML, PDF, ...

Result handler

Custom tags

Struts Components
Action handler

Interacts with other layers Dispatches to server page, HTML, PDF, ... Render dynamic content

Result handler

Custom tags

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=" uri="/tags" %> s" <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=" uri="/tags" %> s" <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*** In********* development Long-term m********** by a team ********** of Concerns Don't ****** Yourself

Review – Why Frameworks?
Code reuse Incremental development Long-term maintenance by a team Separation of Concerns Don't Repeat Yourself

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 (2) View (3) Controller (a) Renders Model (b) Selects View (c) Retains State

Review
(1) Model (2) View (3) Controller (c) Retains State (a) Renders Model (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
user.name user.toString() item.categories[0] @com.example.Test@foo () name in {null,”fred”} categories.{name}

Result
getUser().getName() getUser().toString() First element of Categories collection Calls the static foo() method on the com.example.Test class True if name is null or “fred” 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