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.
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.
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.
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. Configure Hibernate and Spring. 5. Create Hibernate DAO implementation. 6. Run the unit test and verify CRUD with DAO. 7. Create a Manager and Declare Transactions. 8. Create a unit test for the Struts Action. 9. Create an Action and model (DynaActionForm) for the web layer. 10. Run the unit test and verify CRUD with Action. 11. Create JSPs to allow CRUD through a web browser. 12. Verify the JSPs’ functionality through your browser. 13. Replace the JSPs with Velocity templates. 14. Add Validation using Commons Validator.
Download Struts and Spring
Download Struts and Spring1
1. Download and install the following components: • JDK 1.4.2 (or above) • Tomcat 5.0+ • Ant 1.6.1+ 2. Set up the following environment variables: • JAVA_HOME • ANT_HOME • CATALINA_HOME 3. Add the following to your PATH environment variable: • JAVA_HOME/bin • ANT_HOME/bin • CATALINA_HOME/bin To develop a Java-based web application, developers download JARs, create a directory structure, and create an Ant build file. For a Struts-only application, simplify this by using the struts-blank.war, which is part of the standard Struts distribution. For a webapp using Spring’s MVC framework, use the webapp-minimal application that ships with Spring. Both of these are nice starting points, but neither simplifies the Struts-Spring integration nor takes into account unit testing. Therefore, I have made available to my readers Equinox. Equinox is a bare-bones starter application for creating a Struts-Spring web application. It has a pre-defined directory structure, an Ant build file (for compiling, deploying and testing), and all the JARs you will need for a Struts, Spring and Hibernate-based webapp. Much of the directory structure and build file in Equinox is taken from my open-source AppFuse application. Therefore, Equinox is really just an “AppFuse Light” that allows rapid webapp development with minimal setup. Because it is derived from AppFuse, you will see many references...