Book Image

Spring MVC Blueprints

By : Sherwin John C. Tragura
Book Image

Spring MVC Blueprints

By: Sherwin John C. Tragura

Overview of this book

Spring MVC is the ideal tool to build modern web applications on the server side. With the arrival of Spring Boot, developers can really focus on the code and deliver great value, leveraging the rich Spring ecosystem with minimal configuration. Spring makes it simple to create RESTful applications, interact with social services, communicate with modern databases, secure your system, and make your code modular and easy to test. It is also easy to deploy the result on different cloud providers. This book starts all the necessary topics in starting a Spring MVC-based application. Moving ahead it explains how to design model objects to handle file objects. save files into a data store and how Spring MVC behaves when an application deals with uploading and downloading files. Further it highlights form transactions and the user of Validation Framework as the tool in validating data input. It shows how to create a customer feedback system which does not require a username or password to log in. It will show you the soft side of Spring MVC where layout and presentation are given importance. Later it will discuss how to use Spring Web Flow on top of Spring MVC to create better web applications. Moving ahead, it will teach you how create an Invoice Module that receives and transport data using Web Services By the end of the book you will be able to create efficient and flexible real-time web applications using all the frameworks in Spring MVC.
Table of Contents (16 chapters)
Spring MVC Blueprints
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

The project development


All the nuts and bolts involved in developing the PWP from scratch will be discussed in this topic. Each of the four web pages, including their internal processes, will be scrutinized using the codes of the project. The Spring MVC concepts will focus on the following areas:

  • Configuration of DispatcherServlet

  • Configuration of Spring container

  • Creating controllers

  • Types of attributes

  • Validation

  • Type conversion and transformation

  • E-mail support configuration

  • Views and ViewResolvers

Configuring the DispatcherServlet

We start creating the Spring MVC project by configuring the DispatcherServlet API class. The Spring MVC framework has the DispatcherServlet at the center of all request and response transactions as illustrated in the preceding figure.

From the point of view of the PWP, the DispatcherServlet starts receiving requests when the user starts running pages on the web browser. The processes are enumerated as follows:

  • When the container receives a request from a path, the DispatcherServlet checks whose controller is mapped to the path name.

  • Then, the controller acknowledges the request with the appropriate service methods (for example, GET, POST, PUT, HEAD), executes the appropriate transaction method with the given model(s), and then returns the view name to the DispatcherServlet.

  • Then, the DispatcherServlet checks which type of view resolver has been configured from its container. Through the view resolver, the DispatcherServlet will know the appropriate view that matches the given request.

  • Finally, the DispatcherServlet will process the transport of model data to the view for presentation or rendition.

The PWP has the following configuration for the DispatcherServlet:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://java.sun.com/xml/ns/javaee"  
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"    id="WebApp_ID"  
  version="3.0"> 
  <display-name>ChapterOne</display-name> 
 
  <!-- Declare Spring DispatcherServlet --> 
  <servlet> 
    <servlet-name>pwp</servlet-name> 
    <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>pwp</servlet-name> 
    <url-pattern>*.html</url-pattern> 
  </servlet-mapping> 
 
  <!-- Spring accepted extension declared here below --> 
  <mime-mapping> 
    <extension>png</extension> 
    <mime-type>image/png</mime-type> 
  </mime-mapping> 
 
</web-app> 

Just like any typical JEE servlet, the tags <servlet> and <servlet-mapping> are used to declare the dispatcher servlet DispatcherServlet:

<servlet> 
  <servlet-name>pwp</servlet-name> 
  <servlet-class> 
    org.springframework.web.servlet.DispatcherServlet 
  </servlet-class> 
</servlet> 
<servlet-mapping> 
  <servlet-name>pwp</servlet-name> 
  <url-pattern>*.html</url-pattern> 
</servlet-mapping> 

The <servlet-name> tag does not only stand for the name of the servlet, but is also related to the name of a Spring container which will be tackled later. The  <url-pattern> indicates which type of valid URLs will be recognized by the DispatcherServlet during request-response transactions. In our preceding configuration, it shows that all URL must have an extension .html in order for the requests to be processed by the servlet.

When it comes to file types, the DispatcherServlet only considers content types declared with the <mime-mapping> tag. In this project, we only have PNG files needed by the portal.

  <mime-mapping> 
    <extension>png</extension> 
    <mime-type>image/png</mime-type> 
  </mime-mapping>