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

Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Feb 03, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Java

Building Web Apps with Spring

Rob Harrop, Interface21 Ltd.

Page 2: 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]

Page 3: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 4: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 5: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Tools for Web Development

• Spring and Spring Web Flow

• Site Formatting

– SiteMesh

– Tiles

• CSS Framework

– ContentWithStyle.co.uk

Page 6: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 7: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

DEMO:

Basic Web Application

Configuration

Page 8: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Basic User Interactions

• Logic encapsulated in Controllers

– Implement Controller directly

– Extend AbstractController,

MultiActionController or SimpleFormController

Page 9: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Basic User Interactions

• Controllers return a ModelAndView

which describes:

– The view to render

– Data for that view

Page 10: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

View Resolution

• Typically views are represented as String

names

– Decoupled from the actual view

implementation

– Easy to test

Page 11: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

View Resolution

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

– InternalResourceViewResolver

– BeanNameViewResolver

– VelocityViewResolver

• Many applications have multiple

ViewResolvers configured in a chain

Page 12: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Mapping Controllers to URIs

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

– Can have injected dependencies

– Declarative services

Page 13: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 14: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

DEMO:

Creating and Configuring a

Controller

Page 15: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 16: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 17: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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)

Page 18: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

DEMO:

SiteMesh in Action

Page 19: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 20: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

User Conversations with Web

Flow• Web Flow is integrated with Spring MVC

– View resolution works the same way

– Exception resolution works the same way

Page 21: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 22: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 23: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 24: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 25: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Web Flow Concepts

• Flow Registry

– Registry of flow definitions

Page 26: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 27: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 28: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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>

Page 29: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

DEMO:

Order Process with Spring Web

Flow

Page 30: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

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

Page 31: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Handling Exceptions

• HandlerExceptionResolver allows for:

– Last ditch processing

– Same contract as a Controller

– Access to thrown Exception

– Auto detected from the ApplicationContext

Page 32: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Handling Exceptions

• Spring provides SimpleMappingExceptionResolver

– Configurable mapping of Exception type to

error view name

Page 33: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

DEMO:

Configuring a HandlerExceptionResolver

Page 34: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Interception and Filtering

• Spring web applications have three

options for interception:

– Servlet Filters

– Spring HandlerInterceptors

– AOP

Page 35: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Interception and Filtering

• All three options can be treated as normal

Spring beans:

– Dependency Injection

– Remote/JMX exposure

Page 36: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Uses for Interception and Filtering

• Conditional request processing

–Maintenance mode

– Redirect based on user type

• Tracing and profiling

• Thread local management

Page 37: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

DEMO:

Filtering and Interception in

Action

Page 38: Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd

Further Information

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

• SiteMesh -

http://www.opensymphony.com/sitemes

h

• ContentWithStyle -

http://www.contentwithstyle.co.uk

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

Q&A