Working with views

Spring MVC provides a very flexible view resolution mechanism that is fully decoupled from the other elements of the MVC framework. It does not force you to use a particular view technology; rather, it makes it easier to use your own favorite technology. It even allows you to mix and match multiple technologies at the view tier. Spring MVC provides out-of-the-box support for JPS, XSLT, and Velocity views.

Resolving views

In a typical Spring MVC application, the developer chooses a view technology of his choice and accordingly uses a ViewResolver that resolves views built using that technology.

The component responsible for resolving views in a Spring MVC application is org.springframework.web.servlet.ViewResolver. It maps logical view names with physical view resources and the chosen view technology.

All request-handling methods of controllers must resolve a logical view name by either returning a view name, a view object, or a ModelAndView object. The org.springframework.web.servlet.View object prepares HttpRequest for the consumption of the chosen view technology.

Spring MVC comes with a set of convenient view resolvers out of the box:

ViewResolver

Description

AbstractCachingViewResolver

This is a convenient base class for ViewResolver implementations. For better performance, it caches view objects once they are resolved.

XmlViewResolver

This uses bean definitions from a dedicated XML file to resolve view definitions. The file is specified by a resource location. By default, it is located at WEB-INF/views.xml.

ResourceBundleViewResolver

This uses bean definitions in ResourceBundle specified by the bundle basename in order to define views. The default basename is views.properties.

UrlBasedViewResolver

This resolves view names with physical resources in the matching URL. Its two supporting properties, prefix and suffix, help locate the resource.

InternalResourceViewResolver

This resolves Servlets and JSPs with JSTL support. It is a subclass of UrlBasedViewResolver.

VelocityViewResolver

This resolves Velocity templates and is a subclass of UrlBasedViewResolver.

FreeMarkerViewResolver

This resolves FreeMarker templates. It is a subclass of UrlBasedViewResolver.

JasperReportsViewResolver

This resolves JasperReport views for different formats, such as CSV, HTML, XLS, and XLSX.

TilesViewResolver

This resolves Tiles views for both version 2 and 3.

The sample application in this chapter uses UrlBasedViewResolver for resolving JSP views. When you use multiple view technologies in a web application, you may use ResourceBundleViewResolver.

Resolving JSP views

Java Server Pages (JSP), the primary web templating technology for Java EE, is a simple and easy tool for the rapid development of dynamic web content based on JVM. Built on top of Servlet technology, JSP has direct access to the entire Java API. JSP makes a web page author's life a lot easier by allowing him to design web pages in natural HTML format and then embed the required Java code inside scriptlet blocks.

Java Server Pages Tag Library (JSTL) is a set of standardized HTML-style tags highly useful for JSP pages. JSTL eliminates the need to mix Java code inside JSP pages, thus making JSP pages much cleaner and easier to author.

Spring MVC resolves JSP pages using InternalResourceViewResolver. In an earlier section, Your first Spring MVC application, we already configured the ViewResolver class for JSP, as follows:

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <beans:property name="prefix" value="/WEB-INF/views/" />
  <beans:property name="suffix" value=".jsp" />
</beans:bean>

Spring MVC recommends keeping your view files (JSP in this case) under the WEB-INF directory to avoid direct client access. ViewResolver discovers the view files from the physical location and caches them by default once resolved, which helps performance.

Binding Model attributes in JSP pages using JSTL

Views have access to Model attributes set from associated handler methods and controllers. These Model attributes can be displayed in JSP views with the help of JSTL. In the following example, the Model attribute tasks is listed using JSTL:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
...
<table class="table table-hover">
  <thead>
    <tr>
      <th>ID</th>
      <th>Task</th>
      <th>Status</th>
      <th>Priority</th>
      <th>Created By</th>
      <th>Assigned To</th>
    </tr>
  </thead>
  <tbody>
    <c:if test="${not empty tasks}">
      <c:forEach var="task" items="${tasks}">
        <tr>
          <td>
            <a href='<c:url value="/tasks/${task.id}"/>'>${task.id}</a>
          </td>
          <td>${task.name}</td>
          <td>${task.status}</td>
          <td>${task.priority}</td>
          <td>${task.createdBy.name}</td>
          <td>${task.assignee.name}</td>
        </tr>
      </c:forEach>
    </c:if>
  </tbody>
</table>
...

You may have noticed the declaration and usage of JSTL tags in the preceding JSP extract of the /tasks/list.jsp view. Here is how it would be rendered with proper styling in a browser:

Binding Model attributes in JSP pages using JSTL
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset