Book Image

Spring MVC Beginner's Guide

By : Amuthan Ganeshan
Book Image

Spring MVC Beginner's Guide

By: Amuthan Ganeshan

Overview of this book

Table of Contents (19 chapters)
Spring MVC Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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.

This book follows a theme of developing a simple e-commerce site step-by-step. In every successive chapter, you will learn a new concept of Spring MVC. Obviously, the aim is to teach you how you can use Spring MVC effectively. Developing a full-blown, production-ready e-commerce site is not the purpose of this book.

What this book covers

Chapter 1, Configuring a Spring Development Environment, will give you a quick overview of Spring MVC and its architecture and guide you through detailed notes and 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, will lay 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, web application context, Spring MVC request flow, and the layered architecture of a typical web application.

Chapter 3, Control Your Store with Controllers, will take you through the concept of a controller; you will learn more about how to define a controller, and use URI template patterns, matrix variables, and request parameters.

Chapter 4, Working with Spring Tag Libraries, will teach you how to use Spring and Spring form tag libraries in web form handling. You will learn how to bind domain objects with views and 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, will present the inner mechanics of how InternalResourceViewResolver resolves a view and takes you through how to use various view types, such as redirect view and static view. You will also learn about the multipart resolver and content negotiation view resolver. Finally, you will learn how to use exception handler resolvers.

Chapter 6, Intercept Your Store with Interceptor, will present the concept of an 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 web page support internalization with the help of LocaleChangeInterceptor. This chapter also introduces how to perform audit logging in a log file using the interceptor concept.

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

Chapter 8, Give REST to Your Application with Ajax, will teach you the basic principles of REST and Ajax. You will learn how to develop an application in RESTful services. The basic concept of HTTP verbs and how they are related to standard CRUD operations will be explained, and you will learn how to fire an Ajax request and handle it from a web page.

Chapter 9, Apache Tiles and Spring Web Flow in Action, will teach you how to use the Spring web flow to develop workflow-based web pages. You will learn more about states and transitions in web flow and how to define a flow definition. This chapter also teaches you how to decompose a page using Apache tiles. You will also learn more about TileViewResolver and how to define reusable Apache tiles templates.

Chapter 10, Testing your Application, will teach you 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 teaches you how to use that.

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.

What you need for this book

To run the examples in the book, the following software will be required:

  • Java SE Development Kit 7u45 or newer

  • Maven 3.1.0

  • Apache Tomcat 7.0

  • STS 3.4.0 release (Spring Tool Suite)

Who this book is for

This book is designed to be followed from beginning to end, although those with existing knowledge of Spring MVC will be able to jump in to the later chapters and pick out things that are important to them. You are not expected to be experienced with the Spring framework. Some knowledge of servlet programming and dependency injection will be helpful but not essential. In a nutshell, the book provides clear pictures, illustrations, concepts, and is ideally suited for beginners and intermediate developers.

Conventions

In this book, you will find several headings appearing frequently.

To give clear instructions of how to complete a procedure or task, we use:

Time for action – heading

  1. Action 1

  2. Action 2

  3. Action 3

Instructions often need some extra explanation so that they make sense, so they are followed with:

What just happened?

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

You will also find some other learning aids in the book, including:

Pop quiz – heading

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

Have a go hero – heading

These practical challenges give you ideas for experimenting with what you have learned.

You will also find a number of styles of text 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: "Once the download is finished, go to the downloaded directory and extract the .zip file into a convenient directory of your choice."

A block of code is set as follows:

<body>
  <section>
    <div class="jumbotron">
      <div class="container">
        <h1> ${greeting} </h1>
        <p> ${tagline} </p>
      </div>
    </div>
  </section>
</body>

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

  <servlet>
    <servlet-name>DefaultServlet</servlet-name>
    <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
  </servlet>

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

C:\>mvn -version
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T12:37:52-05:00)
Maven home: C:\Program Files\apache-maven-3.2.1
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_51\jre
Default locale: en_SG, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "A System Properties window will appear; in this window, select the Advanced tab and click on the Environment Variables button to open the environment variables window."

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 may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through 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 on 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 all Packt books you have purchased 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.

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 would 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.

Piracy

Piracy of copyright 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 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

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.