Book Image

Spring MVC: Beginner's Guide - Second Edition

By : Amuthan Ganeshan
Book Image

Spring MVC: Beginner's Guide - Second Edition

By: Amuthan Ganeshan

Overview of this book

Spring MVC helps you build flexible and loosely coupled web applications. The Spring MVC Framework is architected and designed in such a way that every piece of logic and functionality is highly configurable. Also, Spring can integrate effortlessly with other popular web frameworks such as Struts, WebWork, Java Server Faces, and Tapestry. The book progressively teaches you to configure the Spring development environment, architecture, controllers, libraries, and more before moving on to developing a full web application. It begins with an introduction to the Spring development environment and architecture so you're familiar with the know-hows. From here, we move on to controllers, views, validations, Spring Tag libraries, and more. Finally, we integrate it all together to develop a web application. You'll also get to grips with testing applications for reliability.
Table of Contents (20 chapters)
Spring MVC Beginner's Guide - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Preface

This book has a very clear aim; to introduce you to the incredible simplicity and power of Spring MVC. I still remember first learning about the Spring framework back in 2009. The best way to test whether or not you really understand a concept is to try to teach it to someone else. In my case, I have taught Spring MVC to MVC; are you confused? I mean that back in 2009, I taught it to my wife Manju Viswambaran Chandrika (MVC). During that course, I was able to understand the kind of doubts that arise in a beginner's mind. I have gathered all my teaching knowledge and put it in this book in an elegant way so that it can be understood without confusion.

It has been more than two years since the first edition of this book presented readers with a beginner-friendly way of explaining the concepts of Spring MVC. The popular reception of the book and the rapid development of the Spring MVC framework naturally demands a new edition.

In the two years since the first edition of this book was published I have received all kinds of suggestions from readers how it could be improved. With the aid of all this information I have completely revised the book. The most obvious changes in this second edition are usage of the latest and greatest versions of Spring and other libraries.

The examples in this book are completely rewritten using Spring 4.3.0.RELEASE version with Java-based configuration. Also in this edition we incorporated the popular in-memory database (HSQL DB) as our backend data-store for the example project. Though this edition includes many changes, my main audience remains the beginners.

I hope you will find this second edition more useful for learning Spring MVC thoroughly from a beginner's perspective.

What this book covers

Chapter 1, Configuring a Spring Development Environment, will give you a quick overview of Spring MVC and guide you with detailed notes of step-by-step instructions to set up your development environment. After installing the required prerequisites, you will try out a quick example of how to develop an application with Spring MVC. Although the chapter doesn’t explain all the code in detail, you’ll pick up a few things intuitively.

Chapter 2, Spring MVC Architecture – Architecting Your Web Store, lays down the ground work for the sample application that we are going to build along the way, chapter by chapter. This chapter will introduce you to concepts such as Request mapping, the web application context, Spring MVC request flow, and the layered architecture of a typical web application. You will also learn about how to set up the in-memory database for our sample application.

Chapter 3, Control Your Store with Controllers, will take you into the concept of controller; you will learn in detail about defining a controller, how to use URI Template Patterns, Matrix variables and Request parameters.

Chapter 4, Working with Spring Tag Libraries, will show you how to use Spring and Spring-form tag libraries in web form handling. You will learn how to bind the domain objects with the views. You will also learn how to use message bundles to externalize label caption texts. At the end of this chapter you will see how to add a login form.

Chapter 5, Working with View Resolver, teaches you the inner mechanics of how InternalResourceViewResolver resolves a view and take you through how to use various view types such as redirect view and static view. You will also learn about Mutipart resolver and content negotiation view resolver. Finally, you will learn how to use Exception handler resolvers.

Chapter 6, Internalize Your Store with Interceptor, presents the concept of the interceptor to you; you will learn how to leverage the interceptor to handle or transform requests and responses flexibly. This chapter will teach you how to make your webpage to support internalization with the help of LocaleChangeInterceptor. This chapter also introduces how to do audit logging in a log file using interceptor concept.

Chapter 7, Incorporating Spring Security, gives you an overview of how to incorporate Spring Security framework with Spring MVC. You will learn how to do simple authentication and authorization on a Spring MVC-based web application. 

Chapter 8, Validate Your Products with a Validator, gives you an overview of validation concept. You will learn about bean validation, and you will learn how to perform custom validation along with the standard bean validation that bean validation. You will also learn about classic Spring validation and how to combine it with bean validation.

Chapter 9, Give REST to Your Application with Ajax, teaches you the basic principles of REST and Ajax, And you will learn how to develop application in RESTful services. The basic concept of HTTP verbs and how it is related to standard CRUD operations will be explained, and you will learn how to do fire Ajax requests and how to handle them from a web page.

Chapter 10, Float Your Application with Web Flow, will show you how to use Spring web flow to develop work flow-based web pages. You will learn more about states and transitions in web flow and how to define a flow definition.

Chapter 11, Template with Tiles, teaches you how to decompose a page using Apache tiles; you will learn more about TileViewResolver and how to define reusable Apache tile templates.

Chapter 12, Testing Your Application, introduces how to leverage the Spring testing capability to test your controllers. You will learn how to load the test context and how to mock the service and repository layers. This chapter also introduces you to the Spring MVC test module and how to use it.

Appendix A, Using the Gradle Build Tool, introduces you to using the Gradle build tool for our sample application. You will learn about the Gradle script that is required to build our project using Gradle build tool.

Appendix B, Pop Quiz Answers, will provide you with the answers to the Pop quizsections in the book.

What you need for this book

To run the examples in the book the following softwares will be required:

  1. Java SE Development Kit

  2. Maven

  3. Apache Tomcat

  4. Spring Tool Suite

Who this book is for

The book is for Java developers who want to exploit Spring MVC and its features to build web applications. Some familiarity with basic servlet programming concepts would be a plus, but is not a prerequisite.

Sections

In this book, you will find several headings that appear frequently (Time for action, What just happened?, Pop quiz, and Have a go hero).

To give clear instructions on how to complete a procedure or task, we use these sections as follows:

Time for action

  1. Action 1

  2. Action 2

  3. Action 3

Instructions often need some extra explanation to ensure they make sense, so they are followed by these sections.

What just happened?

This section explains the working of the tasks or instructions that you have just completed.

You will also find some other learning aids in the book.

Pop quiz

These are short multiple-choice questions intended to help you test your own understanding.

Have a go hero

These are practical challenges that give you ideas to experiment with what you have learned.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Enter the installed JDK directory path as the variable value; in our case, this would be C:\Program Files\Java\jdk1.8.0_91. "

A block of code is set as follows:

<%@ taglib prefix="c" 
      uri="http://java.sun.com/jsp/jstl/core"%> 
 
      <!DOCTYPE html> 
      <html lang="en"> 
           <head> 
                <meta charset="utf-8"> 
                <meta http-equiv="X-UA-Compatible" 
                 content="IE=edge"> 
                <meta name="viewport" content="width=device-width, 
                 initial-scale=1">

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

<dependency> 
   <groupId>org.springframework</groupId> 
   <artifactId>spring-webmvc</artifactId> 
   <version>4.2.2.RELEASE</version> 
</dependency>

Any command-line input or output is written as follows:

Java(TM) SE Runtime Environment (build 1.8.0_91-b15)

 Java HotSpot(TM) 64-Bit Server VM (build 25.91-b15, mixed mode)

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Click on the Java Platform (JDK) 8u91/8u92 download link"

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Spring-MVC-Beginners-Guide-Second-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Downloading the color images of this book 

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/SpringMVCBeginnersGuideSecondEdition_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.