Introduction

Welcome to the first edition of Pro Spring MVC with Web Flow; the first Pro Spring book focused entirely on web development using the Spring Framework 3.1 ecosystem.

What This Book Will Teach You

This book will teach you everything you need to know in order to get started building enterprise-quality web applications using version 3.1 of the Spring Framework. Topics include but are not limited to:

  • The building blocks of the Spring MVC components
  • Configuring your development environment
  • Providing a web front end to a Spring based application
  • A pragmatic approach to testing the web front end
  • Deploying to a local web server and to a remote cloud-based deployment platform
  • An introduction to Spring Web Flow
  • How to build applications with Spring Web Flow

After reading this book you will be familiar with the Spring MVC toolkit and capable of building your own web application from scratch or providing a new web interface to an existing application.

All too often trivial examples are used to demonstrate the power of a framework; it is only when you start using the framework in real situations that its limitations appear. The intention of this book is to show how Spring MVC answers those hard questions (as well as the easy questions!) that web developers are faced with, like managing state in non-trivial multi-page use cases, for example, or providing multiple views of the same resource for different consumers.

And because the real-world problems this book tackles are hard, sometimes the answers Spring MVC provides are not as easy as one would like. This book will not shy away from highlighting those issues or providing pragmatic advice on how to do the right thing.

Who Is This Book For?

This book is for those who are familiar with Spring and want to gain an in-depth understanding of Spring MVC. While it is primarily aimed at those new to Spring MVC, there is information here that will take even expert MVCers by surprise!

The typical reader will be a web developer who has some understanding of the core Spring framework (after reading Pro Spring for example) and wants to investigate Spring MVC in more detail.

If you are unfamiliar with Spring, then by all means continue reading; particularly Chapter 2. However if you find that is insufficient then you might find the Spring Reference Guide1 or Pro Spring 3 (Apress, 2012).

The original Spring book, Expert One-on-One J2EE Design and Development (Wrox, 2002)2, by Rod Johnson (the “father“ of the Spring framework), is fairly old now but still relevant and full of wisdom.

How to Read This Book

This book will take the reader through the thought process of designing, implementing, and deploying a Java web application. The order of the chapters follows the chronological order defined by the development lifecycle. During these chapters we use a sample application to illustrate the topics discussed in the chapter.

Each chapter will address a real-world problem by introducing a new concept or capability which is then used to upgrade the sample application.

Image Note There is a never-ending dilemma that authors face: should we show the answers first and then the questions, or show the questions and then the answers? The first approach risks overloading the reader, while the second approach can be frustrating and slow for those already familiar with the subject matter. The authors believel that the pace of this book is sufficient for those unfamiliar with Spring MVC; more experienced readers may want to skip certain chapters. For example, if you already have a development environment configured and are familiar with Spring then you can skim Chapter 1 to get hold of the sample application and then jump straight to Chapter 3.

The Application Example

While the example cannot be a true enterprise application (because we need to keep this book under 10K pages ;)), it is realistic enough to highlight the typical design challenges that are faced in real-world projects.

The example in question is the site for a web-enabled bookstore. It offers users the following capabilities:

  • An anonymous user can browse the site.
  • A logged-in user can submit an order for one or more books.

__________

  • A logged-in user can see his previous orders.
  • An author can manage the books.
  • An administrator can manage the categories

Throughout the rest of this book, each chapter adds a new aspect to the sample application. The simplest way of capturing this in the example was to have a new project for each chapter.

The Sample Code

We need to explain a little about the coding and referencing style used throughout the book. You’ll find that some of the code listings are complete and can be taken directly from the book to the development environment. In others we omit some imports or methods that are mentioned in earlier listings; we’ve marked those with // Imports omitted or // Methods omitted. Most include a reference to the code listing in which the methods or imports can be found. This approach was needed for readability, as some code listings contain only a few new lines and others would span multiple pages.

Another thing to mention is that we did not include the fully qualified package names for common classes like java.lang.String or java.util.HashMap (most of the java.util packages aren’t included), as a Java developer in general knows where to find those classes. Also, we only mention the fully qualified classname on the first significant reference (after the main heading); for example, the package first identified as org.springframework.web.servlet.DispatcherServlet will in subsequent references be called simply DispatcherServlet. This is again for readability, but we also feel the need to show you the location of the class and believe this was an acceptable approach.

How This Is Book Structured

The book consists of a series of chapters, each explaining a part of the framework or how to use a certain technology. The first four chapters are quite theoretical and are used to explain some of the more general concepts and how they apply or work within Spring MVC (the same goes for the start of the Spring Web Flow part of the book; Chapter 10 is also more or less theoretical).

After that introduction to the concepts behind Spring MVC, the remaining chapters follow a more practical and hands-on approach as we start to develop an application.

The chapters are:

  • Chapter 1, “Configuring a Spring Development Environment.” This chapter presents the prerequisites for your development environment, which you can use to develop the sample application. The structure and purpose of the sample application (an online bookstore) are also explained in this chapter.
  • Chapter 2, “Spring Framework Fundamentals,” provides a broad overview of the fundamental building blocks of the Spring framework. It introduces the concepts of Dependency Injection (DI) and Inversion of Control (IoC). This chapter is particularly recommended if you are unfamiliar with Spring.
  • Chapter 3, “Web Application Architecture.” In this chapter we take you on a slight detour to explain web application architecture. We will explain the different layers that (generally) make a web application and we will explain the Model View Controller triad.
  • Chapter 4, “Spring MVC Architecture.” This chapter is the first “down and dirty” chapter dealing with Spring MVC. It defines exactly what MVC is, how web applications are typically structured, or layered, and it dives into the powerhouse of the Spring MVC engine: the wonderful DispatcherServlet.
  • Chapter 5, “Implementing Controllers.” At this point, you’re probably ready to say, “Show me the code!” In this chapter we will take the knowledge from the previous chapters and start writing controllers; we’ll also get more insight into the internals of Spring @MVC. At the end we will have the start of the sample application. Exciting!
  • Chapter 6, “Implementing Controllers – Advanced.” Every application requires the same behavior in a number of different places, as well as behavior that isn't really part of your core application but needs to be stuck in somewhere. This chapter will introduce Aspect Oriented Programming (AOP) and how Spring MVC easily solves some common web problems. We will also explore the internals of Spring MVC a bit more and explain how to extend the existing infrastructure and how to tailor it to our needs.
  • Chapter 7, “REST and AJAX.” Now that our bookstore is taking off, we want to add some nifty behavior to our application and we also want to expose our controllers as REST web services so that others might be able to integrate with us. For this we are going to explore REST and AJAX and apply those techniques to our application.
  • Chapter 8, “Resolving and Implementing Views.” Chapter 8 digs a bit deeper into how views are resolved within Spring MVC and builds on the information you’ve learned so far. In this chapter you will get to revisit the ViewResolver infrastructure and start to see the power of the MVC architecture shine through when you re-use the same infrastructure to provide different renditions of the same model.
  • Chapter 9, “Testing Spring MVC Applications.” Now that the craving to get something done has been somewhat satisfied and we’ve written some code, it is time to look at testing. This chapter explains how to test Spring MVC applications but also crucially what to test. This chapter will include a good discussion of different strategies for testing, including how to ensure you are testing only what you need to test. You will also be given the chance to test-drive the HTML as well!
  • Chapter 10, “Spring Web Flow.” Up to now all of the page interactions have been pretty simple; each use-case was one or two pages. Now the manager wants to introduce some nontrivial use-cases. Chapter 10 introduces Spring Web Flow— a companion partner to Spring MVC that provides some pretty nifty features for managing web conversations with clients.
  • Chapter 11, “Building Applications with Spring Web Flow.” Following the previous chapter’s introduction to Spring Web Flow and the problems it helps to solve, Chapter 11 explores in practice how to build an application with Spring Web Flow.
  • Chapter 12, “Advanced Spring Web Flow,” builds on the information gained in Chapter 11 and demonstrates how you can dig a little deeper into Spring Web Flow and find some real gems.
  • Chapter 13, “Spring Security,” shows how to keep the scruffy hackers out of our web-application through the use of another well-established tool in the Spring toolbox, Spring Security.
  • Appendix A, “Cloud Foundry—Deploying to the Cloud.” In this appendix we explain the steps needed to deploy the application to the cloud, especially to Cloud Foundry, as that integrates seamlessly with our chosen development environment.

A Thousand-Mile View of the Spring Ecosystem

So before going any further let's take the first peek at Spring MVC and where it fits into the existing Spring ecosystem.

The first piece of good news is that Spring MVC is Spring. You configure Spring MVC using the existing powerful Spring container. Beans defined in Spring MVC are just like any other beans.

The following image from http://static.springsource.org/spring/docs/3.1.0.M2/spring-framework-reference/html/overview.html is very helpful:

Image

As you can see— Spring MVC is powered by the rest of Spring!

Onward!

Now that you have an idea about the style and purpose of this book let's waste no more time in getting you set up with a development environment. We’ll see you in Chapter 1.

Contacting the Authors

Marten Deinum can be contacted at [email protected] for queries and suggestions for this book. His blog can be found at http://mdeinum.wordpress.com. Feel free to contact him if you have found a mistake, want to ask a question, or want to discuss anything else related to the book.

Koen Serneels can be contacted at [email protected] for anything related to this book. If you have suggestions, remarks, or questions, or have found something to be inaccurate, feel free to drop him a message. His blog can be found at http://www.error.be.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset