Spring Book – Chapter 14 – Spring MVC and Spring Web Flow

Spring MVC Annotations – Explained

The various annotations which can be used in the Spring MVC controllers can be summarized as below, with basic brief explanation of each:

  • @Controller – stereotype annotation telling the Spring container that it is a valid Spring bean and a Spring MVC controller class.
  • @RequestMapping – Can be used in class and method level and defines the URL mapping. Available properties are: method (supported HTTP methods like GET, POST etc.), produces (Content type produced), consumes (Content type which will be consumed), params (the URL need to have these query parameters) and headers (the request header should have these header information).
  • @PathVariable – allows using part of the URI to be used in the Java coding. It supports regular expression and can be only used for simple types, java.lang.String and Date variants.
  • @ModelAttribute – can be used to define new model attributes and can also be used for accessing existing model attributes.
  • @CookieValue – provides access to client side cookies.
  • @HeaderValue – provides access to HTTP request header values.
  • @DateTimeFormat – allows conversion of String to date type. Capable of supporting various conventions.
  • @RequestBody – provides access to HTTP request body.
  • @ResponseBody – capable of converting return value to HTTP response body.

Having understood the options which can be used with @RequestMapping annotation, the method signature to be used as the request handler need to follow some patterns. In general Spring MVC does allow having very flexible method signature but arguments can be supplied in any order, some of which are as shown below:

  • HttpServletRequest, HttpServletResponse
  • HttpSession
  • Locale
  • Model
  • Principal etc.

View and ViewResolver

A View is the final output which the client sees on the browser. It renders the data available in the Model object in a formatted and presentable manner in a browser to the requesting user. The views can be classified based on various criteria’s. In one such classification, it can be broadly divided into two types based on: content type and rendering technology. Examples of content type based views are HTML, Excel, PDF etc. and according to rendering type based views are JSP, Velocity, Facelets etc. There are built-in views in Spring MVC which can be summarized as shown in Table 14-2 below.

Table 14-2. Built-in views in Spring MVC

View Classes Description
InternalResourceView Resides in package org.springframework.web.servlet.view.View which is actually a wrapper for JSP’s and other resources within the same web application.
JstlView Resides in package org.springframework.web.servlet.view.Maps to JSTL pages, which are nothing but JSP page containing JSP standard tag libraries (JSTL).
RedirectView Resides in package org.springframework.web.servlet.view .View which redirects to absolute, context relative or current context relative URL’s. The HTTP redirect happens to the specified URL.
TilesView, TilesJstlView Resides in package org.springframework.web.servlet.view.tiles.Both are deprecated as of 3.0. TilesView is used for retrieving Tiles definition and TilesJstlView is specialization of TilesView for JSTL pages.
VelocityView, VelocityLayoutView, VelocityToolboxView Resides in package org.springframework.web.servlet.view.velocity.VelocityView uses Velocity template engine. Spring3.0’s VelocityTemplate requires Velocity 1.4 or higher.Velocity’s VelocityLayoutServlet is used to compose various page templates together, VelocityLayoutView imitates this.VelocityToolboxView extends VelocityView by supporting Velocity Tools toolboxes.
FreeMarkerView Resides in package org.springframework.web.servlet.view.freemarker.View using FreeMarker template engine.
JasperReportsMultiFormatView, JasperReportsPdfView, JasperReportsXlsView, JasperReportsCsvView, JasperReportsHtmlView Resides in package org.springframework.web.servlet.view.jasperreports.Support for Jasper reports.

All the handler methods in the controller class should finally resolve to a logical view name either explicitly by returning String (view URL), View or ModelAndView object or implicitly based on various conventions available in Spring MVC. In Spring MVC the views are resolved using a ViewResolver and the framework does provide various view resolvers as shown in Table 14-3 below which could be configured and used as is.

Table 14-3. Built-in view resolvers

View resolver Description
BeanNameViewResolver Simple implementation of ViewResolver which resolves view by mapping the name to the bean name as declared in the Spring configuration file.
UrlBasedViewResolver Simple implementation in which symbolic view names translates into the exact URL or using augmented prefix and suffix translates into final URL where it is to be navigated.
InternalResourceViewResolver Subclass of UrlBasedViewResolver which supports InternalResourceView such as JSP’s and other web application resources as well as view subclasses like JstlView.
VelocityViewResolver Subclass of UrlBasedViewResolver which supports VelocityView and its various subclasses.
VelocityLayoutViewResolver Subclass of VelocityViewResolver specialized in handling VelocityLayoutView and its properties.
FreeMarkerViewResolver Subclass of UrlBasedViewResolver which supports FreeMarkerView and its various subclasses.
ResourceBundleViewResolver ViewResolver implementation which resolves views defined in a ResourceBundle. These are typically properties files located in the classpath and by default take the name views.properties.
XmlViewResolver ViewResolver implementation which resolves views defined in an XML file. The default path being “/WEB-INF/views.xml”.
XsltViewResolver It takes in the symbolic view names and tries to derive at the URL and finds the XsltView associated with it.
JasperReportsViewResolver Resolves supplied view name into the final URL pointing to the report file.
ContentNegotiatingViewResolver Resolves the view based on the request file name or the “Accept” header (HTML header, META tags). It either resolves the view by itself or in-turn uses other view resolvers for doing this.

ViewResolver’s decouples the controller from the view implementation giving capability to the application developer to switch the view from one to another by mere changing of the view resolver. Listing 14-7 below shows the ViewResolver interface definition in Spring MVC. Custom view resolvers can be written by extending ViewResolver interface.

Listing 14-7. ViewResolver interface definition

Page Visitors: 8013

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *