Tuesday, July 29, 2014

Book Review: Spring MVC Beginner's Guide

Spring MVC Beginner's Guide features the subtitle "Your ultimate guide to building a complete web application using all the capabilities of Spring MVC." It is published by Packt Publishing and authored by Amuthan G. Spring MVC Beginner's Guide is comprised of ten chapters and two appendices spanning over 280 pages of substantive content.

What is Spring MVC?

In case you're wondering what Spring MVC is, tutorialspoint's Spring MVC Framework Tutorial describes Spring MVC: "The Spring web MVC framework provides model-view-controller architecture and ready components that can be used to develop flexible and loosely coupled web applications. The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements." In the JavaWorld article Mastering Spring MVC, Steven Haines describes Spring MVC as "a full MVC implementation that follows the patterns and paradigms that Spring is known for, including [dependency injection]."


The Preface of Spring MVC Beginner's Guide opens by stating the objective of the book: "to introduce you to the incredible simplicity and power of Spring MVC." This Preface follows the pattern established in many other Packt books of presenting short (2-4 sentences each) summaries of its ten chapters and two appendices, presenting what readers need to try out the examples (Java SE 7 JDK Update 45 as stated in Preface, but it is stated in Chapter 1 that "Java 6 or any higher version is also sufficient"; Maven 3.1.0; Apache Tomcat 7.0; and Eclipse-based Spring Tool Suite 3.4.0), and describing the intended audience ("ideally suited for beginners and intermediate developers" with no assumed knowledge of the Spring Framework). I felt that my Spring experience made the early chapters of Spring MVC Beginner's Guide much more approachable and would recommend that readers of Spring MVC Beginner's Guide with no experience with Spring Framework at least read Overview of Spring Framework before reading this book.

Chapter 1: Configuring a Spring Development Environment

As its title suggests, the first chapter of Spring MVC Beginner's Guide is about setting up the environment for Spring-related development. The chapter includes sections on Windows installation of the JDK, Maven, Tomcat, and Spring Tool Suite (STS). These sections include text and screen snapshots, are highly detailed, and are easy to follow, but they only cover Windows installation of these products. A screen snapshot in the section on installing Java shows Java SE Development Kit 7 Update 51 being installed and the text instructions and output show that same Java 7 Update 51 installed.

Chapter 1 indicates how to download, install, and individually configure JDK, Maven, Tomcat, and Spring Tool Suite. It then demonstrates how to configure these tools to work together for a complete, integrated Spring development environment. The chapter then moves onto preliminary steps of delivering on the book's stated theme: "developing a simple e-commerce site step-by-step."

Chapter 2: Spring MVC Architecture – Architecting Your Web Store

Although the end of the first chapter introduced a simple Spring MVC-based application, Chapter 2 of Spring MVC Beginner's Guide explains the concepts behind this and presents a "deep-dive into Spring MVC architecture." The chapter introduces the Spring MVC DispatcherServlet in more detail (including discussion of the web.xml file and @RequestMapping annotation) than in the first chapter. The discussion of Spring MVC web configuration in the second chapter specifically discusses the XML elements <mvc:annotation-driven /> and <context:component-scan />.

Chapter 3: Control Your Store with Controllers

The initial two chapters of Spring MVC Beginner's Guide are primarily high-level, general, and relatively introductory, but Chapter 3 marks the beginning of the more focused chapters. This chapter focused on Controllers opens up with the statement, "Controllers are presentation layer components that are responsible for responding to user actions." In the course of this chapter's coverage of Spring MVC Controllers, topics such as the @Controller annotation and @RequestMapping annotation. The discussion under the heading "The role of a controller in Spring MVC" well into the third chapter (page 74) discusses the role the Controller plays in Spring MVC.

The third chapter also discusses general web application architecture and describes the typical "layers" in a web application: presentation, domain/model, services, and persistence. Each layer is described in text and then illustrated with code samples. Other discussed topics include handler mappings, URI template patterns, matrix variables, and handling GET request parameters.

Chapter 4: Working with Spring Tag Libraries

Chapter 4 of Spring MVC Beginner's Guide focuses on "predefined [JSP] tag libraries of Spring" that enable "[binding of] form elements to model data" and "support various other common functionalities, such as, externalizing messages and error handling." The chapter introduces the JSP Standard Tag Library (JSTL) and then introduces Spring Form Tags. Several pages of descriptive text and code examples demonstrate how @ModelAttribute can be used to bind a Java object to a Spring MVC form. The chapter also discusses and demonstrates how to customize Spring MVC binding using WebDataBinder and @InitBinder.

Moving text out of the JSP and into external properties files in Spring MVC forms is another subject covered in Chapter 4. This section demonstrates and explains how to apply <spring:messsage>. This chapter on applying Spring Tags concludes with a demonstration of adding a small portion of Spring Security to the Spring MVC application to enable authentication and authorization.

Chapter 5: Working with View Resolver

The fifth chapter of Spring MVC Beginner's Guide begins with the assertion that "Spring MVC maintains a high level of decoupling between the view and controller" and then demonstrates how this is accomplished using View Resolvers and Views. The chapter provides in-depth descriptions and code examples demonstrating use of the RedirectView, access of static resources with <mvc:resources>, parsing of request into multiple parts, and serving "different representations of the same resource" with ContentNegotiatingViewResolver.

One of the main points of Chapter 5 is that Spring MVC separates the view approach from the controller. This is demonstrated with examples of using InternalResourceViewResolver for JSP views, MappingJacksonJsonView for JSON views, and MarshallingView for XML views. Chapter 5 also discusses and demonstrates use of the ResponseStatusExceptionResolver and ExceptionHandlerExceptionResolver implementations of the HandlerExceptionResolver interface for "[resolving] exceptions that are thrown during controller mapping or execution."

Chapter 6: Intercept Your Store with Interceptor

Chapter 6 of Spring MVC Beginner's Guide is about Spring MVC interceptors, which the chapter states are "used to intercept actual web requests before or after they are processed" that can be related to the "filter concept of servlet programming." The chapter demonstrates through text and code how to write an implementation of the HandlerInterceptor interface (including referencing HandlerInterceptorAdapter as an easier way to implement HandlerInterceptor). The chapter also demonstrates application of standard provided interceptor for internationalization (LocaleChangeInterceptor) as well as providing two custom interceptors for performing audit logging and for performing conditional redirection.

Chapter 7: Validate Your Products with a Validator

Spring MVC Beginner's Guide's seventh chapter explains the "basics of setting up validation with Spring MVC." The chapter begins with a discussion of Java Bean Validation (JSR-303). The first example demonstrates "how to validate a form submission in a Spring MVC application" and uses Hibernate Validator and standard annotations-based constraints defined in package javax.validation.constraints. The next example expands on this to demonstrate customized constraints. The example demonstrates writing a custom constraint annotation and applying that annotation.

Chapter 7 transitions from JSR-303 validation to "classic Spring validation." The example in this section illustrates using "classic Spring validation" to validate the relationships between fields rather than individual field values ("cross-field validation"). The chapter concludes with a discussion and demonstration on how to combine JSR 303 Bean Validation and classic Spring Validation in the same application.

Chapter 8: Give REST to Your Application with Ajax

The eighth chapter of Spring MVC Beginner's Guide is on REST and Ajax. The chapter provides a very brief introduction to REST before moving into an example of applying @RequestBody and @ResponseBody to handle transformations between the serialization format and Java objects. The chapter discusses using Postman Chrome extension as client for testing the REST application just written.

The second part of Chapter 8 discusses using Ajax for communication between client and the REST service. This section begins with brief background on Ajax and then moves onto an example that uses trendy Angular.js.

Chapter 9: Apache Tiles and Spring Web Flow in Action

Chapter 9 of Spring MVC Beginner's Guide focuses on two teaching objectives: "Developing flow-based applications using Spring Web Flow" and "Decomposing pages using reusable Apache Tiles templates." The chapter emphasizes that Spring Web Flow and Apache Tiles are two completely independent web frameworks.

The section of Chapter 9 on using Spring Web Flow states about Spring Web Flow, "Spring Web Flow facilitates us to develop a flow-based web application easily" and adds, "A flow in a web application encapsulates a series of steps that guides a user through the execution of a business task." The initial example demonstrates writing an XML-based flow control file. The chapter uses that example XML to explain the contents of a Spring Web Flow XML file including its six possible states and transitions between states. There are a couple very lengthy examples to further explain details of Spring Web Flow.

Chapter 9's section on Apache Tiles describes Tiles as a "template composition framework" that allows "developers to define reusable page fragments (tiles) that can be assembled into a complete web page at runtime." Just as the section on Spring Web Flow focuses on the SWF's web flow XML file, the Apache Tiles section focuses on tile-definition.xml. The example demonstrates using Apache Tiles to implement JSPs consisting of composite templates in a Spring MVC application.

Chapter 10: Testing Your Application

The final chapter of Spring MVC Beginner's Guide opens with an overview of and a brief discussion regarding the benefits and challenges of unit testing web applications. The chapter then explains and illustrates testing a Spring MVC application's domain object (unit testing with JUnit), validator (integration testing with Spring TestContext Framework), controller (Spring Test Context framework mocking), and REST-exposed service.

Appendix A and Appendix B

Appendix A ("Using the Gradle Build Tool") briefly describes Gradle and how to use Gradle to build the book's examples. The examples throughout the book use Maven for the builds, but the presence of this appendix focused on Gradle is a sign of the trend in Java builds. Appendix B contains the answers to the "Pop Quiz" questions that occur throughout the book.

General Observations
  • Spring MVC Beginner's Guide jumps right into discussing how to apply Spring MVC without explaining what it is.
    • On the page labeled 43 that exists well into the second chapter, there is some discussion on "Model View Controller" in general before contrasting "MVC" and "web MVC" (the latter of which is implemented with Spring MVC).
    • The page labeled 45 in Spring MVC Beginner's Guide has a graphic under the heading "An overview of the Spring MVC request flow" that should probably be understood early in the reading of this book.
    • This is likely not a problem in most cases as a reader interested in reading about with "Spring MVC" in its title is likely to know what Spring MVC is.
    • A reader who is not familiar with what Spring MVC is might read a quick introduction before reading this book.
    • I would recommend that a reader of this book have some awareness of servlets and of the Spring Framework. The "Beginner's" part of the title is in reference to Spring MVC, but this book is not an introduction to the general Spring Framework and will be far easier to read for those with some Spring development experience.
  • Many of the examples heavily use Spring Tool Suite and many of the steps in the examples are made simpler because of that Eclipse-based tool. Readers with little experience using Spring or Spring MVC are well-served to use Spring Tool Suite to work through these examples.
  • The PDF version of Spring MVC Beginner's Guide that I reviewed has full-color screen snapshots.
  • Code samples tend to be well organized and indented, but have neither color coded syntax nor line numbers.
  • Multiple versions of the JDK are referenced in Spring MVC Beginner's Guide. The Preface states that JDK 7 Update 45 or newer is required, but Chapter 1 states that any JDK 6 release or later should be sufficient and a screen snapshot in Chapter 1 indicates JDK 7 Update 51 being installed. As far as I can determine, Java SE 6 JDK is probably sufficient for examples in this book, but I cannot be certain of that.

Spring MVC Beginner's Guide is a comprehensive resource on Spring MVC. Although its title includes "Beginner's", I think most readers would be well-advised to have some prior experience with the Spring Framework and with JavaServer Pages and Java servlets to get the most out of this book. The "Beginner's" portion of the title applies specifically to Spring MVC rather than to the more general Spring Framework or JSP/servlet programming.

No comments: