SpringMVC with Embedded Jetty and Thymeleaf

December 13 2012

As mentioned in my previous post on using embedded Jetty with SpringMVC, I was going to look at simplifying the application by using Thymeleaf instead of JSPs as a view technology.

Well, it turns out that it is much more straightforward.

First we can remove the JSPC plugin from our pom.xml. Second we can completely remove the web.xml file.

Bootstrapping Jetty is much simpler now. We do not have to hook into the Jetty startup process with a lifecycle listener, instead we can directly create the dispatcher servlet and add it to a ServletContextHandler like we would any other servlet:

From JettyConfiguration.java:

public ServletHolder dispatcherServlet() {  
    AnnotationConfigWebApplicationContext ctx =   
        new AnnotationConfigWebApplicationContext();  
    DispatcherServlet servlet = new DispatcherServlet(ctx);  
    ServletHolder holder = new ServletHolder("dispatcher-servlet", servlet);  
    return holder;  

public ServletContextHandler servletContext() throws IOException {  
    ServletContextHandler handler = new ServletContextHandler();  
        new ClassPathResource("webapp").getURI().toString());  
    handler.addServlet(AdminServlet.class, "/metrics/*");  
    handler.addServlet(dispatcherServlet(), "/");  
    return handler;  

@Bean(initMethod = "start", destroyMethod = "stop")  
public Server jettyServer() throws IOException {  
    Server server = new Server();  
    return server;  

This is much more straightforward, everything wired up with Spring without any lifecycle callback hooks.

The setup for Spring to render the Thymeleaf views, is a little more complex, but not fussy. What we do is replace:

public InternalResourceViewResolver configureInternalResourceViewResolver() {  
    InternalResourceViewResolver resolver =   
        new InternalResourceViewResolver();  
    return resolver;  


public ServletContextTemplateResolver thymeleafTemplateResolver() {  
    ServletContextTemplateResolver resolver =   
        new ServletContextTemplateResolver();  
    return resolver;  

public SpringTemplateEngine thymeleafTemplateEngine() {  
    SpringTemplateEngine engine = new SpringTemplateEngine();  
    return engine;  

public ThymeleafViewResolver thymeleafViewResolver() {  
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();  
    return resolver;  

in MvcConfiguration.java.

Our templates now look a lot more like plain HTML, and will render better when loaded directly in a browser:

<!DOCTYPE html>  
<html xmlns:th="http://www.thymeleaf.org">  
<h1>Hello World!</h1>  

<p>Server time: <span th:text="${serverTime}"></span></p>  

<p>Here are some items:</p>  
    <li th:each="item : ${someItems}" th:text="${item}"></li>  

<p>Do we have a message from the dummy service:</p>  

<div th:if="${dummyService == null}">  
    <p>No, dummy service is null.</p>  
<div th:if="${dummyService != null}">  
    <p>Yes: <span th:text="${dummyService.getMessage()}"></span></p>  

<p><a href="resources/static.txt">A static file.</a></p>  

<p><a href="metrics">Yammer Metrics</a></p>  


With the build process being simple (no dependence on maven plugins) we can switch to a much less verbose Gradle build file:

apply plugin:'java'  
apply plugin:'application'  

version = '0.0.1-SNAPSHOT'  

mainClassName = "ca.unx.template.Main"  
applicationName = "jetty-springmvc-thymeleaf-template"  

repositories {  

dependencies {  
    compile("org.springframework:spring-webmvc:3.1.3.RELEASE") {  
        // Commons-logging excluded in favour of SLF4j.  
        exclude module: 'commons-logging'  

    compile "cglib:cglib:2.2.2"  
    compile "org.thymeleaf:thymeleaf-spring3:2.0.14"  
    compile "org.eclipse.jetty:jetty-webapp:8.1.8.v20121106"  
    compile "com.yammer.metrics:metrics-servlet:2.2.0"  

    /* Logging. */  
    def slf4jVersion = "1.7.1"  
    compile "ch.qos.logback:logback-classic:1.0.9"  
    compile "org.slf4j:slf4j-api:$slf4jVersion"  
    compile "org.slf4j:jcl-over-slf4j:$slf4jVersion"  
    compile "org.codehaus.groovy:groovy:1.8.6"  

A complete template project can be found here. Note that this links to tag which is the state of the code at the time of this writing.