117A. Spring-MVC Web Applications
Version 3.1

Book cover

This course enables the experienced Java developer to use the Spring application framework to manage objects in a lightweight "IoC" (inversion-of-control) container; to create simple and complex web applications; and to manage persistent objects using Spring's support for DAOs and transaction control. Spring is a far-reaching framework that aims to facilitate all sorts of Java development, including every level of multi-tier distributed systems. Here we focus on the Core and Web/MVC modules, with a slightly lighter touch on persistence through DAO and ORM modules.

The Core module gives the developer declarative control over object creation and assembly; this is useful for any tier of any Java application. So is Spring's validation framework, and so we study these things in a mix of standalone (Java SE) applications and web applications. Then students build web applications that use the Spring MVC framework to rationalize their designs into coherent request/response cycles. They use Spring command objects to manage HTML forms and their data, and connect these to the validation framework. We connect our applications to persistent stores and study the DAO and ORM modules, to better understand JDBC and JPA persistence models and declarative transaction control.


Learning Objectives

  • Understand the scope, purpose, and architecture of Spring
  • Use Spring's bean factories and application contexts to declare application components, rather than hard-coding their states and lifecycles
  • Use dependency injection to further control object relationships from outside the Java code base
  • Use annotations to take advantage of Spring post-processors for automated bean instantiation and wiring
  • Create validators for business objects, and associate them for application-level and unit-testing uses
  • Build a web application as a Spring DispatcherServlet and associated application context
  • Use Spring MVC annotations to develop web controllers, mapping request URLs and other criteria to Java methods and binding request data to method parameters
  • Build and manage HTML forms with Spring command objects and custom tags
  • Use Spring interceptors to implement horizontal features in the web application
  • Connect business objects to persistent stores using Spring's DAO and ORM modules
  • Simplify JDBC code using Spring templates
  • Integrate JPA entities and DAOs into Spring applications
  • Declare and enforce transaction requirements using Spring

Timeline: 5 days.

IDE Support: Eclipse Juno

  • In addition to the primary lab files, an optional overlay is available that adds support for Eclipse Juno. Students can code, build, deploy, and test all exercises from within Eclipse, and take advantage of Eclipse's built-in editors and wizards for web applications, XML files, JSPs, and more. See also our orientation to Using Capstone's Eclipse Overlays.

Chapter 1. Overview of Spring

  • Java EE: The Good, The Bad, and the Ugly
  • Enter the Framework
  • Spring Value Proposition
  • The Spring Container
  • Web Applications
  • Persistence Support
  • Aspect-Oriented Programming
  • The Java EE Module(s)
  • Integrating Other Frameworks

Chapter 2. The Container

  • JavaBeans, Reconsidered
  • The Factory Pattern
  • Inversion of Control
  • XML View: Declaring Beans
  • Java View: Using Beans
  • Singletons and Prototypes

Chapter 3. Instantiation and Configuration

  • Configuring Through Properties
  • Configuration Namespaces
  • The p: Notation
  • Bean (Configuration) Inheritance
  • Configuring Through Constructors
  • Bean Post-Processors
  • Lifecycle Hooks
  • Integrating Existing Factory Code
  • Awareness Interfaces

Chapter 4. Dependency Injection

  • Complex Systems
  • Assembling Object Graphs
  • Dependency Injection
  • Single and Multiple Relationships
  • The Utility Schema
  • Bean Aliases
  • Inner Beans
  • Autowiring
  • @Component, @Service, & Company
  • @Autowired Properties
  • Best Practices with Spring Annotations

Chapter 5. Assembling Object Models

  • Collections and Maps
  • Support for Generics
  • The Spring Utility Schema (util:)
  • Autowiring to Multiple Beans
  • Order of Instantiation
  • Bean Factory vs. Application Context

Chapter 6. Validation

  • Validators
  • The Errors Object
  • ValidationUtils
  • Error Messages and Localization
  • Nested Property Paths

Chapter 7. The Web Module

  • Servlets and JSPs: What's Missing
  • The MVC Pattern
  • The Front Controller Pattern
  • DispatcherServlet
  • A Request/Response Cycle
  • The Strategy Pattern
  • Web Application Contexts
  • Annotation-Based Handler Mappings
  • @Controller and @RequestMapping
  • "Creating" a Model
  • Views and View Resolvers

Chapter 8. Handling Requests

  • Matching URLs
  • Identifying Views
  • Request Parameters
  • Injectable Parameters
  • Command Objects
  • Return Types
  • HTTP Methods
  • Path Variables
  • Scope and Granularity of Command Objects
  • Headers and Cookies
  • RESTful Web Services

Chapter 9. Working with Forms

  • Form Processing in Spring MVC
  • Command Objects in Request Methods
  • Spring Custom Tags
  • <form:form> and Friends
  • Command objects at Session Scope
  • Limitations of @SessionAttributes

Chapter 10. Data Binding

  • A Consolidated Process
  • Property Editors
  • Custom Property Editors
  • DataBinder and @InitBinder Methods
  • Converters and Formatters
  • Using <mvc:annotation-driven/>
  • Custom Formatters

Chapter 11. Binding and Validation

  • Validating Form Input
  • Spring Validators
  • Deriving a Validator Reference
  • Applying a Validator
  • <form:error> and <form:errors>
  • JSR 303, a/k/a Bean Validation
  • Configurating JSR-303 Support
  • The Spring JSR-303 Validator
  • Automatic Support with @Valid

Chapter 12. Configuring Spring MVC

  • Configuring Message Sources
  • Resolving Error Codes
  • HandlerExceptionResolver
  • @ExceptionHandler
  • Interceptors
  • Filters in the Request-Handling Cycle
  • Support and Utility Classes
  • Spring Web Scopes

Chapter 13. Templates and DAOs

  • The DAO Pattern
  • The DaoSupport Hierarchy
  • The DataAccessException Hierarchy
  • JdbcTemplate and RowMapper<T>

Chapter 14. Working with JPA

  • Object/Relational Mapping
  • The Java Persistence API
  • Hibernate
  • @Entity and Other JPA Annotations
  • Entity Managers and Factories
  • The Persistence Unit
  • JpaDaoSupport and JpaTemplate
  • Configuration Issues

Chapter 15. Transactions

  • Transaction Managers
  • Transaction Advice
  • AOP vs. Annotations
  • JDBC Transaction Manager
  • JPA Transaction Manager
  • Entity States
  • Extended Persistence Contexts

System Requirements

Hardware Requirements (Minimum) Core 2 Duo, 1.5 GHz, 4 gig RAM, 1 gig disk space.
Hardware Requirements (Recommended) Core 2 Duo, 2.5 GHz GHz, 6 gig RAM, 1 gig disk space.
Operating System Tested on Windows 7. Course software should be viable on all systems which support a Java SE 7 Developer's Kit.
Network and Security Limited privileges required -- please see our standard security requirements.
Software Requirements All free downloadable tools.