Building Java Web Applications with Spring Web MVC
Mark Twain once said, “In the spring, I have counted 136 different kinds of weather.” Almost certainly, he wasn’t referring to the Spring Framework. Or was he clairvoyant? The Spring Framework has grown to become an ecosystem of projects; it includes many distinct modules, integrates numerous frameworks and libraries, and provides a varied range of capabilities in diverse areas such as Flash, enterprise applications, web services, data stores, OSGi,1 and even. NET. Spring applications are supported on all popular cloud platforms such as Cloud Foundry,2 Google App Engine, and Amazon EC23 and can leverage traditional RDBMSs as well as new NoSQL4 solutions and data stores like PostgreSQL,5 MySQL, MongoDB,6, and Redis.
7 Unlike many other frameworks such as Struts, which is confined to developing web applications, the Spring Framework can be used to build stand-alone, web, and JEE applications. Spring provides support for building modern web applications including REST, HTML5, and Ajax, as well as mobile client platforms including Android and iPhone. The Spring Framework has remarkably changed the enterprise Java landscape forever by connecting components with systems so that you do not have to write the plumbing code, thus allowing you to focus on the business of the application.
Spring Framework Fundamentals
An application is composed of components such as web components and business logic components. These components need to collaborate with each other to fulfill the common business goals of the application, so these components depend on one another. This dependency, if uncontrolled, often leads to tight coupling between them, resulting in an unmaintainable application. Controlling this coupling so that it does not result in a tightly coupled application is a nontrivial task.
By contrast, if a component of the application does not depend on another component, it will not have to look for it, all the components will be fully isolated, and the resulting application will be loosely coupled. But such an application will not do anything. Essentially, components should depend on other components but should not look for these components on which they depend.
Instead, such dependencies should be provided to the dependent components. This is the essence of Inversion of Control. The Spring Framework is one such IoC framework, which provides the dependencies to the dependent components by means of dependency injection.
Application Context
Spring comes with several implementations of the Application Context interface out of the box. The three most commonly used are the following:
- ClassPathXmlApplicationContext: Loads a context definition from an XML file located in the classpath
- FileSystemXmlApplicationContext: Loads a context definition from an XML file in the file system
- XmlWebApplicationContext: Loads context definitions from an XML file contained within a web application
Key Objectives of the Spring Framework
Dependency injection is not the only key benefit of using the Spring Framework. The goal of the Spring Framework is to simplify the complexity of developing an enterprise application. This complexity manifests itself in an enterprise application in several ways, and most enterprise applications prior to the Spring Framework were inadvertently afflicted with few or even all of the following tribulations:
- Tight coupling
- Cross-cutting concerns
- Boilerplate code
Summary
This chapter first briefly introduced the Spring Framework and went on to show how you can deal with tight coupling, cross-cutting concerns, and boilerplate code. This chapter transformed the data access layer built to eliminate the boilerplate code resulting from using pure JDBC. Then it discussed Spring MVC’s architecture, including its request-handling life cycle. Next, it showed you how to develop a Hello World application using Spring Web MVC. Then it began implementing the bookstore application using Spring Web MVC, and as the application progressed, it introduced the annotations programming model of Spring Web MVC and processing forms in the web application