IN THE PREVIOUS CHAPTER, I gave you an overview of the Spring Framework.We looked at what Spring is, how it is packaged, and the various modules it contains. I also mentioned that with Spring, you do not have to take an all-or-nothing approach when trying to decide whether you should use Spring. In other words, based on your needs, you can phase in the Spring Framework one module at a time (along with any dependencies). In this chapter, I will demonstrate how to use Spring Web MVC Framework (module), to build Time Expression, our sample web application.
Note that from this point on, I will refer to the Spring Web MVC Framework as simply Spring MVC, in most places.
What’s Covered in This Chapter
In this chapter, we will
- n Look at the various benefits of using Spring MVC
- n Take an in-depth look at the Spring Web MVC Framework
- n Build three of the screens in Time Expression using Spring MVC: a no-form controller,
- two form controllers, and a Spring HTTP interceptor.
This is an exciting chapter, so I won’t waste any more time boring you with introductory material. Let’s spring into action!.
Benefits of the Spring Web MVC Framework
The Spring Web MVC Framework is a robust, flexible, and well-designed framework for rapidly developing web applications using the MVC design pattern.The benefits achieved from using this Spring module are similar to those you get from the rest of the Spring Framework. Let’s review a few of these. I will demonstrate some of these benefits later in this chapter.
- Easier testing—This is a common theme you will find across all the Spring classes. The fact that most of Spring’s classes are designed as JavaBeans enables you to inject test data using the setter methods of these classes. Spring also provides mock classes to simulate Java HTTP objects (HttpServletRequest, for example), which makes unit testing of the web layer much simpler.
- n Bind directly to business objects—Spring MVC does not require your business (model) classes to extend any special classes; this enables you to reuse your business objects by binding them directly to the HTML forms fields. In fact, your controller classes are the only ones that are required to extend Spring classes (or implement a Spring controller interface).
- Clear separation of roles—Spring MVC nicely separates the roles played by the various components that make up this web framework. For example, when we discuss concepts such as controllers, command objects, and validators, you will begin
to see how each component plays a distinct role.
- Adaptable controllers—If your application does not require an HTML form, you can write a simpler version of a Spring controller that does need all the extra components required for form controllers. In fact, Spring provides several types of controllers, each serving a different purpose. For example, there are no-form controllers, simple form controllers, wizardlike form controllers, views with no controllers, and even prepackaged controllers that enable you to write views without your own custom controller.
- Simple but powerful tag library—Spring’s tag library is small, straightforward, but powerful. For example, Spring uses the JSP expression language (EL) for arguments to the <spring:bind> tag.
- Web Flow—This module is a subproject and is not bundled with the Spring core distribution. It is built on top of Spring MVC and adds the capability to easily write wizardlike web applications that span across several HTTP requests (an
online shopping cart, for example).
- View technologies and web frameworks—Although we are using JSP as our view technology, Spring supports other view technologies as well, such as Apache Velocity (jakarta.apache.org/velocity/) and FreeMarker (freemarker.org).This is a powerful concept because switching from JSP to Velocity is a matter of configuration. Furthermore, Spring provides integration support for Apache Struts (struts. apache.org), Apache Tapestry (jakarta.apache.org/tapestry), and OpenSymphony’s WebWork (opensymphony.com/webwork/).
- Lighter-weight environment—As I mentioned in the previous chapter, Spring enables you to build enterprise-ready applications using POJOs; the environment setup can be simpler and less expensive because you could develop and deploy your application using a lighter-weight servlet container.