• No results found

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

N/A
N/A
Protected

Academic year: 2021

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

Copied!
39
0
0

Loading.... (view fulltext now)

Full text

(1)

Java

Building Web Apps with Spring Rob Harrop, Interface21 Ltd.

(2)

About the Speaker

• VP at Interface21

• Core Developer on Spring

• Founder of Spring Modules

• JMX 2.0 Expert Group Member

• Author of Pro Spring

(3)

Agenda

• Tools for Web Development

• Getting Started

• Basic User Interactions

• Handling Site Formatting

• User Conversations with Web Flow

• Adding Security

• Handling Exceptions

(4)

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

(5)

Tools for Web Development

• Spring and Spring Web Flow

• Site Formatting

– SiteMesh

– Tiles

• CSS Framework

(6)

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

(7)

DEMO:

Basic Web Application

Configuration

(8)

Basic User Interactions

• Logic encapsulated in Controllers

– Implement Controller directly

– Extend AbstractController, MultiActionController or SimpleFormController

(9)

Basic User Interactions

• Controllers return a ModelAndView

which describes:

– The view to render

(10)

View Resolution

• Typically views are represented as String names

– Decoupled from the actual view implementation

(11)

View Resolution

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

– InternalResourceViewResolver

– BeanNameViewResolver

– VelocityViewResolver

• Many applications have multiple

(12)

Mapping Controllers to URIs

• Controllers are configured as beans in

the web application config file

– Can have injected dependencies

(13)

Mapping Controllers to URIs

• These Controller beans are mapped to

URIs using a HandlerMapping:

– SimpleUrlHandlerMapping

– BeanNameHandlerMapping

• HandlerMappings are also configured

(14)

DEMO:

Creating and Configuring a

Controller

(15)

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

(16)

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

(17)

Site-Wide Formatting with

SiteMesh

1. Configure the SiteMesh filter

2. Create a decorators.xml file

1. Define your page decorators

(18)

DEMO:

(19)

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

(20)

User Conversations with Web

Flow

• Web Flow is integrated with Spring MVC

– View resolution works the same way

(21)

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

(22)

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

(23)

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

(24)

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

(25)

Web Flow Concepts

• Flow Registry

(26)

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

(27)

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>

(28)

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>

(29)

DEMO:

Order Process with Spring Web

Flow

(30)

Handling Exceptions

• Exceptions that cannot be handled should be allowed to propagate

• These can be handled by some last-ditch process

– Servlet error pages

(31)

Handling Exceptions

• HandlerExceptionResolver allows for:

– Last ditch processing

– Same contract as a Controller

– Access to thrown Exception

– Auto detected from the

(32)

Handling Exceptions

• Spring provides

SimpleMappingExceptionResolver

– Configurable mapping of Exception type to error view name

(33)

DEMO:

Configuring a

(34)

Interception and Filtering

• Spring web applications have three options for interception:

– Servlet Filters

– Spring HandlerInterceptors

(35)

Interception and Filtering

• All three options can be treated as normal Spring beans:

– Dependency Injection

(36)

Uses for Interception and

Filtering

• Conditional request processing

– Maintenance mode

– Redirect based on user type

• Tracing and profiling

(37)

DEMO:

Filtering and Interception in

Action

(38)

Further Information

• Blog - http://blog.interface21.com/ • SiteMesh -http://www.opensymphony.com/sitemes h • ContentWithStyle -http://www.contentwithstyle.co.uk

(39)

References

Related documents

Penerapan Spatial Fuzzy C-Means pada segmentasi citra dan beberapa proses lainnya seperti cropping area objek, ekstraksi ciri warna HSV dan ekstraksi ciri tektur GLCM citra

Urban transportation is a complex system with an increasing number of competing transport modes and with very different patterns across cities in the use of private

Cooperative education and work-integrated learning programs are able to provide career clarification by way of student’s engagement in legitimate peripheral

Consultant will assess (visually inspect) the buildings, building components, systems and systems components as described below to determine the condition of all assets and assess

It is the interest of this paper to examine the level of expectations and satisfaction among undergraduate students pursuing an economics program in a public university in malaysia

On the first laboratory visit, the subjects performed 2 Wingate Anaerobic Tests (WAnTs) to determine peak power (PP) and mean power (MP), as well as tests for 1 repetition

9 This question requires candidates to discuss the circumstances in which the court may lift the veil and ignore the separate legal personality of the company. 6–10 A detailed

After first reviewing some basics about the yield curve and the reasons it might predict future growth, we look at the actual relation- ship and compare predictions from the yield