The Groovy markup templating engine is primarily designed to generate XML-like markup ( XML, XHTML, HTML5 and others), but you can use it to generate any text content. The Spring Framework has built-in integration for using Spring MVC with Groovy markup.

The Groovy markup template engine requires Groovy 2.3.1+.
Configuration

The following example shows how to configure the Groovy markup template engine:

Java
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.groovy();
    }
    // Configure the markup template engine in Groovy...
    @Bean
    public GroovyMarkupConfigurer groovyMarkupConfigurer() {
        GroovyMarkupConfigurer configurer = new GroovyMarkupConfigurer();
        configurer.setResourceLoaderPath("/WEB-INF/");
        return configurer;
    }
}
Kotlin
@Configuration
@EnableWebMvc
class WebConfig : WebMvcConfigurer {
    override fun configureViewResolvers(registry: ViewResolverRegistry) {
        registry.groovy()
    }
    // Configure the markup template engine in Groovy...
    @Bean
    fun groovyMarkupConfigurer() = GroovyMarkupConfigurer().apply {
        resourceLoaderPath = "/WEB-INF/"
    }
}

The following example shows how to configure the same in XML:

<mvc:annotation-driven/>
<mvc:view-resolvers>
    <mvc:groovy/>
</mvc:view-resolvers>
<!-- Configuring the markup template engine in Groovy... -->
<mvc:groovy-configurer resource-loader-path="/WEB-INF/"/>

Example

Unlike traditional template engines, the Groovy markup language relies on a DSL that uses build tool syntax. The following example shows a sample template for an HTML page:

yieldUnescaped '<!DOCTYPE html>'
html(lang:'en') {
    head {
        meta('http-equiv':'"Content-Type" content="text/html; charset=utf-8"')
        title('My page')
    }
    body {
        p('This is an example of HTML contents')
    }
}