Monday, January 9, 2012


Developing a Spring Framework MVC application step-by-step using NetBeans and GlassFish

Authors







Overview


1. What's covered


2. Prerequisite software


3. The application we are building


1. Basic Application and Environment Setup


1.1. Create the NetBeans Spring project


1.2. Create 'index.jsp'


1.3. Deploy the application to GlassFish


1.4. Check the application works


1.5. Download the Spring Framework


1.6. Modify 'web.xml' in the 'WEB-INF' directory


1.7. Copy libraries to 'WEB-INF/lib'


1.8. Create the Controller


1.9. Write a test for the Controller


1.10. Create the View


1.11. Compile and deploy the application


1.12. Try out the application


1.13. Summary


2. Developing and Configuring the Views and the Controller


2.1. Configure JSTL and add JSP header file


2.2. Improve the controller


2.3. Decouple the view from the controller


2.4. Summary


3. Developing the Business Logic


3.1. Review the business case of the Inventory Management System


3.2. Add some classes for business logic


3.3. Summary


4. Developing the Web Interface


4.1. Add reference to business logic in the controller


4.2. Modify the view to display business data and add support for message bundle


4.3. Add some test data to automatically populate some business objects


4.4. Add the message bundle


4.5. Adding a form


4.6. Adding a form controller


4.7. Summary


5. Implementing Database Persistence


5.1. Create database startup script


5.2. Create table and test data scripts


5.3. Run scripts and load test data


5.4. Create a Data Access Object (DAO) implementation for JDBC


5.5. Implement tests for JDBC DAO implementation


5.6. Summary


6. Integrating the Web Application with the Persistence Layer


6.1. Modify service layer


6.2. Fix the failing tests


6.3. Create new application context for service layer configuration


6.4. Add transaction and connection pool configuration to application context


6.5. Final test of the complete application


6.6. Summary


A. Build Scripts

 

Overview

This document is a step-by-step guide on how to develop a web application from scratch using the Spring Framework.
Only a cursory knowledge of Spring itself is assumed, and as such this tutorial is ideal if you are learning or investigating Spring. Hopefully by the time you have worked your way through the tutorial material you will see how the constituent parts of the Spring Framework, namely Inversion of Control (IoC), Aspect-Oriented Programming (AOP), and the various Spring service libraries (such as the JDBC library) all fit together in the context of a Spring MVC web application.
Spring provides several options for configuring your application. The most popular one is using XML files. This is also the traditional way that has been supported from the first release of Spring. With the introduction of Annotations in Java 5, we now have an alternate way of configuring our Spring applications. The new Spring 2.5 release introduces extensive support for using Annotations to configure a web application.
This document uses the traditional XML style for configuration. We are working on an "Annotation Edition" of this document and hope to publish it in the near future.
Please note that we are not going to cover any background information or theory in this tutorial; there are plenty of books available that cover the areas in depth; whenever a new class or feature is used in the tutorial, forward pointers to the relevant section(s) of the Spring reference documentation are provided where the class or feature is covered in depth. 

1. What's covered

The following list details all of the various parts of the Spring Framework that are covered over the course of the tutorial.
·         Inversion of Control (IoC)
·         The Spring Web MVC framework
·         Data access with JDBC
·         Unit and integration testing
·         Transaction management 

2. Prerequisite software

The following prerequisite software and environment setup is assumed. You should also be reasonably comfortable using the following technologies.
·     Java SDK 1.5/1.6
·     Ant 1.7
·     Spring Netbeans Module Release 1.1
·     GlassFish V2 UR1
·     NetBeans 6.0
NetBeans IDE 6.0 Full Pack (http://www.netbeans.org) provides an excellent environment for web application development. The Ant build scripts are omitted from this discussion as they are automatically generated by the IDE. The Spring plugin module for NetBeans should be installed prior to start of this project,
You may of course use pretty much any variation or version of the above software. If you want to use IntelliJ instead of NetBeans or Jetty instead of GlassFish, then many of the tutorial steps will not translate directly to your environment but you should be able to follow along anyway. 

3. The application we are building

The application we will be building from scratch over the course of this tutorial is a very basic inventory management system. This inventory management system is severely constrained in terms of scope; find below a use case diagram illustrating the simple use cases that we will be implementing. The reason why the application is so constrained is so that you can concentrate on the specifics of Spring Web MVC and Spring, and not the finer details of inventory management.


Use case diagram of the inventory management system
We will start by setting up the basic project directory structure for our application, downloading the required libraries, setting up our Ant build scripts, etc. The first step gives us a solid foundation on which to develop the application proper in parts 23, and 4.
Once the basic setup is out of the way, Spring itself will be introduced, starting with the Spring Web MVC framework. We will use Spring Web MVC to display the inventoried stock, which will involve writing some simple Java classes and some JSPs. We will then move onto introducing persistent data access into our application, using Spring's Simple JDBC support.
By the time we have finished all of the steps in the tutorial, we will have an application that does basic inventory management, including listing stock and permitting the price increase of such stock. 

No comments:

Post a Comment