Nick Carroll Software Professional

Using StringTemplate as the view engine for your Spring MVC application

I wish I never discovered Django templates because I shudder every time I have to use Freemarker or Velocity for my Spring MVC applications. Fortunately there is an alternative. You can use StringTemplate to generate your views in Spring MVC quite easily. StringTemplate enforces a strict separation of concerns, which therefore minimises the amount of logic that is allowed in the view, thus forcing you to do more in your controllers. Therefore your view remains purely for presentation purposes.

First things first is that you need to download StringTemplate and Antlr, and make those libraries available on your classpath. Next, extend Spring’s InternalResourceView.

import org.springframework.web.servlet.view.InternalResourceView;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.StringTemplate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class StringTemplateView extends InternalResourceView {

    protected void renderMergedOutputModel(Map model, HttpServletRequest request,
                HttpServletResponse response) throws Exception {

        Resource templateFile = getApplicationContext().getResource(getUrl());

        StringTemplateGroup group = new StringTemplateGroup("webpages", templateFile.getFile().getParent());
        StringTemplate template = group.getInstanceOf(getBeanName());

        PrintWriter writer = response.getWriter();

Finally, you need to configure your Spring application context to use StringTemplate to render your views.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="com.agilecognition.bookshelf.web.view.StringTemplateView"/>
        <property name="prefix" value="/WEB-INF/templates/"/>
        <property name="suffix" value=".st"/>
        <property name="requestContextAttribute" value="rc"/>

Now you can organise your StringTemplate files that have the “.st” suffix in /WEB-INF/templates. For example, you might want to organise your templates as follows.

WEB-INF + templates + layout + partials

The template might look like the following template.

    <div class="header">$partials/header()$</div>
    <div class="content">$body$</div>
    <div class="footer">$partials/footer()$</div>

The above contains the basic structure for an HTML page on your site. You can use this one template to keep a consistent look and feel across your entire site. The template depends on the and templates that exist in the partials directory.

To insert the feedback form in into the $body$ placeholder attribute for your contacts page you simply create a template with the following line.

$layout/layout(body=partials/feedbackform())$ When your controller handles the request to display the contact us page, the controller will return a ModelAndView with the view name set to “contactus”. The view name maps to the file in your templates directory. StringTemplate will render the contactus page using the layout template with the header, footer and feedback form templates.

comments powered by Disqus