2
Spring Quick Start Tutorial
Developing Your First Spring Web Application The following chapter is a sample from SourceBeat’s Spring Live book by Matt Raible. Please visit www.sourcebeat.com to access the book.
This chapter is a tutorial on how to write a simple Spring web application using the Struts MVC framework for the front end, Spring for the middle-tier glue, and Hibernate for the back end. In Chapter 4, this application will be refactored to use the Spring MVC framework. This chapter covers the following topics: • Writing tests to verify functionality. • Configuring Hibernate and Transactions. • Loading Spring’s applicationContext.xml file. • Setting up dependencies between business delegates and DAOs. • Wiring Spring into the Struts application.
Spring Live
Overview
14
Overview
You will create a simple application for user management that does basic CRUD (Create, Retrieve, Update and Delete). This application is called MyUsers, which will be the sample application throughout the book. It’s a 3tiered webapp, with an Action that calls a business delegate, which in turn calls a Data Access Object (DAO). The diagram below shows a brief overview of how the MyUsers application will work when you finish this tutorial. The numbers below indicate the order of flow – from the web (UserAction) to the middle tier, (UserManager), to the data layer (UserDAO) – and back again.
Figure 2.1:
MyUsers application flow
This application uses Struts as the MVC framework because most readers are familiar with Struts. The real power of Spring lies in its declarative transactions, dependency binding and persistence support (for example Hibernate and iBATIS). Chapter 4 refactors this application to use Spring’s MVC framework.
Spring Live
Overview
15
Below are the ordered steps you will perform: 1. Download Struts and Spring. 2. Create project directories and an Ant build file. 3. Create a unit test for the persistence layer. 4.