Java Building Web Apps with Spring Rob Harrop, Interface21 Ltd.
Contact me at rharrop@interface21.com About the Speaker VP at Interface21 Core Developer on Spring Founder of Spring Modules JMX 2.0 Expert Group Member Author of Pro Spring
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 SiteMesh 1. 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
Flow Registry Web Flow Concepts 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