Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

39
Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd.

Transcript of Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Java

Building Web Apps with Spring

Rob Harrop, Interface21 Ltd.

About the Speaker

• VP at Interface21

• Core Developer on Spring

• Founder of Spring Modules

• JMX 2.0 Expert Group Member

• Author of Pro Spring

• Contact me at [email protected]

Agenda

• Tools for Web Development

• Getting Started

• Basic User Interactions

• Handling Site Formatting

• User Conversations with Web Flow

• Adding Security

• Handling Exceptions

• Interception and Filtering

Tools for Web Development

• Get a good IDE:

– IntelliJ IDEA

– Eclipse with WTP

– NetBeans

• Consider using Jetty for development

– Rapid turn around

– Easy to edit view files, flow definitions etc

• Maven is a good tool for managing Jetty

Tools for Web Development

• Spring and Spring Web Flow

• Site Formatting

– SiteMesh

– Tiles

• CSS Framework

– ContentWithStyle.co.uk

Getting Started

1.Create basic web application skeleton

1.Consider Maven as a kick start

2.Configure Spring's DispatcherServlet

3.Create a Spring Web Application config

file

4.Configure ContextLoaderListener

and middle tier configuration files as

needed

DEMO:

Basic Web Application

Configuration

Basic User Interactions

• Logic encapsulated in Controllers

– Implement Controller directly

– Extend AbstractController,

MultiActionController or SimpleFormController

Basic User Interactions

• Controllers return a ModelAndView

which describes:

– The view to render

– Data for that view

View Resolution

• Typically views are represented as String

names

– Decoupled from the actual view

implementation

– Easy to test

View Resolution

• These names are mapped to concrete views using a ViewResolver:

– InternalResourceViewResolver

– BeanNameViewResolver

– VelocityViewResolver

• Many applications have multiple

ViewResolvers configured in a chain

Mapping Controllers to URIs

• Controllers are configured as beans in the web application config file

– Can have injected dependencies

– Declarative services

Mapping Controllers to URIs

• These Controller beans are mapped to

URIs using a HandlerMapping:

– SimpleUrlHandlerMapping

– BeanNameHandlerMapping

• HandlerMappings are also configured

in the web application config file

DEMO:

Creating and Configuring a

Controller

Site-Wide Formatting

• Most web applications require consistent

formatting across the site

– Headers

– Navigation

– Footers

– Ads

• Adding this by hand can be painful and

error-prone

Handling Site-Wide Formatting

• Site-wide formatting can be applied

automatically

– Do not cut and paste across pages!

• Use a pre-built tool

– SiteMesh and Tiles are good candidates

• Couple this with a strong CSS-based

layout

– I use a pre-built CSS framework from

ContentWithStyle

Site-Wide Formatting with

SiteMesh1. Configure the SiteMesh filter

2. Create a decorators.xml file

1. Define your page decorators

3. Create your decorator(s)

DEMO:

SiteMesh in Action

User Conversations with Web

Flow• Web Flow provides a sophisticated

mechanism for controlling long running

user interactions

• Conversations are mapped as fa low using

standard state machines concepts

User Conversations with Web

Flow• Web Flow is integrated with Spring MVC

– View resolution works the same way

– Exception resolution works the same way

Web Flow Concepts

• State

– A single stage in the execution of a flow

• Action

– A piece of logic that can be executed a

various points during the flow

Web Flow Concepts

• Event

– A user or action initiated event such as

“submit” or “process”

• Transition

– A movement from one state to another in

response to some event

Web Flow Concepts

• View State

– A state that constitutes a pause in the flow

execution and renders a view for the user

• Action State

– A state type that executes one or more

Actions before proceeding to another State

Web Flow Concepts

• Flow Executor

–Manages flow execution. Resumes and

pauses flows as they proceed

• Flow Repository

– Store in-progress flow execution state

– Simple and continuation-based

implementations provided

Web Flow Concepts

• Flow Registry

– Registry of flow definitions

Getting Started with Web Flow

1.Configure Flow Registry

2.Configure Flow Executor

3.Optionally configure Flow Repository

4.Create Flow Definition

5.Configure a FlowController for your

flow

Configuring Web Flow

• Configure the FlowExecutor and FlowRegistry

• <flow:executor id="flowExecutor"

• registry-ref="flowRegistry"/>

• <flow:registry id="flowRegistry">

• <flow:location path="/WEB-INF/flows/**.xml"/>

• </flow:registry>

Flows are XML files stored in /WEB-INF/flows

Configuring a FlowController

• <bean name="/checkout.html"

class="o.s.w.ex.mvc.FlowController">

• <property name="flowExecutor"

• ref="flowExecutor"/>

• <property name="defaultFlowId"

• value="checkout"/>

• </bean>

DEMO:

Order Process with Spring Web

Flow

Handling Exceptions

• Exceptions that cannot be handled should

be allowed to propagate

• These can be handled by some last-ditch

process

– Servlet error pages

– Spring HandlerExceptionResolvers

Handling Exceptions

• HandlerExceptionResolver allows for:

– Last ditch processing

– Same contract as a Controller

– Access to thrown Exception

– Auto detected from the ApplicationContext

Handling Exceptions

• Spring provides SimpleMappingExceptionResolver

– Configurable mapping of Exception type to

error view name

DEMO:

Configuring a HandlerExceptionResolver

Interception and Filtering

• Spring web applications have three

options for interception:

– Servlet Filters

– Spring HandlerInterceptors

– AOP

Interception and Filtering

• All three options can be treated as normal

Spring beans:

– Dependency Injection

– Remote/JMX exposure

Uses for Interception and Filtering

• Conditional request processing

–Maintenance mode

– Redirect based on user type

• Tracing and profiling

• Thread local management

DEMO:

Filtering and Interception in

Action

Further Information

• Blog - http://blog.interface21.com/

• SiteMesh -

http://www.opensymphony.com/sitemes

h

• ContentWithStyle -

http://www.contentwithstyle.co.uk

Q&A