thymeleaf with grails 3.0.1
build.gradle
dependencies { compile "org.springframework.boot:spring-boot-starter-logging" compile "org.springframework.boot:spring-boot-starter-actuator" compile "org.springframework.boot:spring-boot-autoconfigure" compile "org.springframework.boot:spring-boot-starter-tomcat" compile "org.springframework.boot:spring-boot-starter-thymeleaf" // add :
application.yml
environments: development: dataSource: dbCreate: create-drop url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE spring: thymeleaf: prefix: file:./grails-app/views suffix: .html templateMode: HTML5 cacheable: false : production: : spring: thymeleaf: prefix: classpath:/WEB-INF/grails-app/views suffix: .html templateMode: HTML5 cacheable: true
resources.groovy
beans = { grailsThymeleafViewResolver(sample.GrailsThymeleafViewResolver) { characterEncoding = 'utf8' templateEngine = ref('templateEngine') } springConfig.addAlias 'thymeleafViewResolver', 'grailsThymeleafViewResolver' springConfig.addAlias 'jspViewResolver', 'thymeleafViewResolver' }
sample.GrailsThymeleafViewResolver
package sample import org.grails.web.servlet.view.GrailsViewResolver import org.grails.web.util.WebUtils import org.springframework.web.servlet.View import org.thymeleaf.spring4.view.ThymeleafViewResolver class GrailsThymeleafViewResolver extends ThymeleafViewResolver implements GrailsViewResolver { @Override public View resolveViewName(String viewName, Locale locale) throws Exception { return super.resolveViewName(WebUtils.addViewPrefix(viewName), locale); } }
grails-app/views/layouts/main.html
<!doctype html> <html lang="en" class="no-js" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title layout:replace="title">Grails</title> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="stylesheet" href="../../assets/stylesheets/application.css" th:href="@{/assets/application.css}" /> <script type="text/javascript" src="../../assets/javascripts/application.js" th:src="@{/assets/application.js}" ></script> </head> <body> <div id="grailsLogo" role="banner"><a href="http://grails.org"><img src="../../assets/images/grails_logo.png" alt="Grails" th:src="@{/assets/grails_logo.png}" /></a></div> <div layout:fragment="content"></div> <div class="footer" role="contentinfo"></div> <div id="spinner" class="spinner" style="display:none;" th:text="#{spinner.alt}">Loading…</div> </body> </html>
grails-app/views/book/create.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="/layouts/main"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title layout:replace="title" th:text="#{default.create.label(#{book.label})}">Create Book</title> </head> <body> <div layout:fragment="content"> <p th:text="#{default.create.label(#{book.label})}">ppp</p> :