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