Posted on by & filed under Content - Highlights and Reviews, Web Development.

Spring MVC is an implementation of the Model-View-Controller pattern for the Web. The view layer in a Web application drives the user experience, and there is no shortage of view technologies to choose from. Spring MVC accommodates this choice with modular support for pluggable view template technologies. This tip will demonstrate the usage of JSP, a traditional Java-based view implementation, and Mustache, a modern logic-less view template system.

The source code for this article is available at https://github.com/JamesEarlDouglas/spring-mvc-view-templating.

Background

After a Spring MVC controller has completed its execution, it returns two key pieces of data to Spring: a model and a view name. Spring uses a configured ViewResolver to map the view name onto an actual view, bind the model to it, and return it as a rendered view to the user through the HTTP response.

For simple, direct view resolution, Spring can be configured with a single ViewResolver instance which knows where to locate view template files.

For a look at creating a simple web application using Spring MVC, see the Spring Recipes book.

View Templates

A common pattern for rendering Web pages is the View Template pattern. In this example, a view template is an HTML file with some syntactic placeholders sprinkled throughout. These placeholders are interpreted by a view template technology, which results in a complete HTML view ready to present to the user.

The tried-and-true view template implementation for Java Web applications is JSP, which Spring supports via its InternalResourceViewResolver class.

This particular InternalResourceViewResolver will look for JSP files named {view name}.jsp on the file system in the Web application’s WEB-INF/views/ directory. Spring will use any available JSP processor on the classpath to compile these files and handle rendering.

Displaying a collection of comments in JSP uses the ” element for iteration, and the ${ } pattern for field value lookup. This relies on the JavaBean pattern, where a named field has accessor and mutator methods.

A more modern view template technology is Mustache, which has implementations in many languages including Java. One of the attractions of Mustache is that designers can use it to build entire frontends in only HTML and JavaScript, with no need for a working backend or Spring MVC application. The frontend can be integrated into a working backend with no changes to the view templates.

Spring does not include Mustache support out of the box, but it can be added with a single Maven dependency.

<dependency>
<groupId>com.github.sps.mustache</groupId>
<artifactId>mustache-spring-view</artifactId>
<version>1.0</version>
</dependency>

Use of the Mustache ViewResolver is much the same as before. An important additional step is to inject a Spring ResourceLoader into a MustacheTemplateLoader. The ResourceLoader is provided by Spring; it just needs to be captured as an argument in the viewResolver method.

Displaying a collection of comments in Mustache is similar to in JSP, but the accessor and mutator methods can be done away with, in favor of direct access to the (public) fields.

Safari Books Online has the content you need

Below are some Spring books to help you develop applications, or you can check out all of the Spring books and training videos available from Safari Books Online. You can browse the content in preview mode or you can gain access to more information with a free trial or subscription to Safari Books Online.

Pro Spring MVC: With Web Flow provides in-depth coverage of Spring MVC and Spring Web Flow, two highly customizable and powerful web frameworks brought to you by the developers and community of the Spring Framework.
Totally revised for Spring 3.0, Spring in Action, Third Edition is a hands-on guide to the Spring Framework. It covers the latest features, tools, and practices including Spring MVC, REST, Security, Web Flow, and more.
The release of Spring Framework 3 has ushered in many improvements and new features. Spring Recipes: A Problem-Solution Approach, Second Edition continues upon the bestselling success of the previous edition but focuses on the latest Spring 3 features for building enterprise Java applications.
Spring Batch in Action is an in-depth guide to writing batch applications using Spring Batch. Written for developers who have basic knowledge of Java and the Spring lightweight container, the book provides both a best-practices approach to writing batch jobs and comprehensive coverage of the Spring Batch framework.
Spring Web Services 2 Cookbook includes a wide variety of recipes that covers most important topics used in real-world applications. It is a well-rounded guide covering a lot of ground in the Spring Web Services domain using systematic arranged chapters and focused recipes.

Start your FREE 10-day trial to Safari Books Online

About this author

James Earl Douglas is a software developer focusing on functional Web APIs on the JVM. He has based many Java systems on Spring MVC and its ecosystem, which lead to authoring the e-book Barebones Spring MVC. James now researches similar and dissimilar approaches in Scala and Clojure.

Tags: Frameworks, java, Java Web Applications, Mustache, RESTful web services, Rich Web Applications, Spring MVC,

Comments are closed.