Add FreeMarker support

This commit adds auto-configuration and a starter,
spring-boot-starter-freemarker, for using FreeMarker view templates in
a web application.

A new abstraction, TemplateAvailabilityProvider, has been introduced.
This decouples ErrorMvcAutoConfiguration from the various view
technologies that Spring Boot now supports, allowing it to determine
when a custom error template is provided without knowing the details of
each view technology.

Closes #679
This commit is contained in:
Andy Wilkinson
2014-04-29 17:46:55 +01:00
parent 1143f6dbb1
commit 7c91176186
39 changed files with 1038 additions and 55 deletions

View File

@@ -80,6 +80,22 @@ content into your application; rather pick only the properties that you need.
spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added
spring.thymeleaf.cache=true # set to false for hot refresh
# FREEMARKER ({sc-spring-boot-autoconfigure}}/freemarker/FreeMarkerAutoConfiguration.{sc-ext}[FreeMarkerAutoConfiguration])
spring.freemarker.allowRequestOverride=false
spring.freemarker.allowSessionOverride=false
spring.freemarker.cache=true
spring.freemarker.checkTemplateLocation=true
spring.freemarker.contentType=text/html
spring.freemarker.exposeRequestAttributes=false
spring.freemarker.exposeSessionAttributes=false
spring.freemarker.exposeSpringMacroHelpers=false
spring.freemarker.prefix=
spring.freemarker.requestContextAttribute=
spring.freemarker.suffix=
spring.freemarker.templateEncoding=UTF-8
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.viewNames=
# INTERNATIONALIZATION ({sc-spring-boot-autoconfigure}/MessageSourceAutoConfiguration.{sc-ext}[MessageSourceAutoConfiguration])
spring.messages.basename=messages
spring.messages.encoding=UTF-8

View File

@@ -38,6 +38,9 @@ The following auto-configuration classes are from the `spring-boot-autoconfigure
|{sc-spring-boot-autoconfigure}/web/EmbeddedServletContainerAutoConfiguration.{sc-ext}[EmbeddedServletContainerAutoConfiguration]
|{dc-spring-boot-autoconfigure}/web/EmbeddedServletContainerAutoConfiguration.{dc-ext}[javadoc]
|{sc-spring-boot-autoconfigure}/freemarker/FreeMarkerAutoConfiguration.{sc-ext}[FreeMarkerAutoConfiguration]
|{dc-spring-boot-autoconfigure}/freemarker/FreeMarkerAutoConfiguration.{dc-ext}[javadoc]
|{sc-spring-boot-autoconfigure}/orm/jpa/HibernateJpaAutoConfiguration.{sc-ext}[HibernateJpaAutoConfiguration]
|{dc-spring-boot-autoconfigure}/orm/jpa/HibernateJpaAutoConfiguration.{dc-ext}[javadoc]

View File

@@ -809,8 +809,16 @@ added.
`spring.thymeleaf.suffix`, defaults ``classpath:/templates/'' and ``.html''
respectively). It can be overridden by providing a bean of the same name.
Checkout {sc-spring-boot-autoconfigure}/web/WebMvcAutoConfiguration.{sc-ext}[`WebMvcAutoConfiguration`]
and {sc-spring-boot-autoconfigure}/thymeleaf/ThymeleafAutoConfiguration.{sc-ext}[`ThymeleafAutoConfiguration`]
* If you use FreeMarker you will also have a `FreeMarkerViewResolver` with id
``freeMarkerViewResolver''. It looks for resources in a loader path (externalized to
`spring.freemarker.templateLoaderPath`, default ``classpath:/templates/'') by
surrounding the view name with a prefix and suffix (externalized to `spring.freemarker.prefix`
and `spring.freemarker.suffix`, defaults ``'' and ``.ftl'' respectively). It can be overriden
by providing a bean of the same name.
Check out {sc-spring-boot-autoconfigure}/web/WebMvcAutoConfiguration.{sc-ext}[`WebMvcAutoConfiguration`],
{sc-spring-boot-autoconfigure}/thymeleaf/ThymeleafAutoConfiguration.{sc-ext}[`ThymeleafAutoConfiguration`], and
{sc-spring-boot-autoconfigure}/freemarker/FreeMarkerAutoConfiguration.{sc-ext}['FreeMarkerAutoConfiguration']
@@ -1145,11 +1153,11 @@ you encounter a server error (machine clients consuming JSON and other media typ
see a sensible response with the right error code). To switch it off you can set
`error.whitelabel.enabled=false`, but normally in addition or alternatively to that you
will want to add your own error page replacing the whitelabel one. If you are using
Thymeleaf you can do this by adding an `error.html` template. In general what you need is
a `View` that resolves with a name of `error`, and/or a `@Controller` that handles the
`/error` path. Unless you replaced some of the default configuration you should find a
`BeanNameViewResolver` in your `ApplicationContext` so a `@Bean` with id `error` would be
a simple way of doing that.
Thymeleaf or FreeMarker you can do this by adding an `error.html` or `error.ftl` template
respectively. In general what you need is a `View` that resolves with a name of `error`,
and/or a `@Controller` that handles the `/error` path. Unless you replaced some of the
default configuration you should find a `BeanNameViewResolver` in your `ApplicationContext`
so a `@Bean` with id `error` would be a simple way of doing that.
Look at {sc-spring-boot-autoconfigure}/web/ErrorMvcAutoConfiguration.{sc-ext}[`ErrorMvcAutoConfiguration`] for more options.
@@ -1251,7 +1259,15 @@ tools.
=== Reload Thymeleaf templates without restarting the container
If you are using Thymeleaf, then set `spring.thymeleaf.cache` to `false`. See
{sc-spring-boot-autoconfigure}/thymeleaf/ThymeleafAutoConfiguration.{sc-ext}[`ThymeleafAutoConfiguration`]
for other template customization options.
for other Thymeleaf customization options.
[[howto-reload-freemarker-content]]
=== Reload FreeMarker templates without restarting the container
If you are using FreeMarker, then set `spring.freemarker.cache` to `false`. See
{sc-spring-boot-autoconfigure}/freemarker/FreeMarkerAutoConfiguration.{sc-ext}[`FreeMarkerAutoConfiguration`]
for other FreeMarker customization options.

View File

@@ -1,5 +1,5 @@
= Spring Boot Reference Guide
Phillip Webb; Dave Syer; Josh Long; Stéphane Nicoll; Rob Winch;
Phillip Webb; Dave Syer; Josh Long; Stéphane Nicoll; Rob Winch; Andy Wilkinson;
:doctype: book
:toc:
:toclevels: 4

View File

@@ -892,14 +892,16 @@ and it will be silently ignored by most build tools if you generate a jar.
[[boot-features-spring-mvc-template-engines]]
==== Template engines
As well as REST web services, you can also use Spring MVC to serve dynamic HTML content.
Spring MVC supports a variety of templating technologies including: velocity, freemarker,
Spring MVC supports a variety of templating technologies including: Velocity, FreeMarker,
and JSPs. Many other templating engines also ship their own Spring MVC integrations.
Spring Boot includes auto-configuration support for the Thymeleaf templating engine.
Thymeleaf is an XML/XHTML/HTML5 template engine that can work both in web and non-web
environments. It allows you to create natural templates that can be correctly displayed
by browsers and therefore work also as static prototypes. Thymeleaf templates will be
picked up automatically from `src/main/resources/templates`.
Spring Boot includes auto-configuration support for the Thymeleaf and FreeMarker
templating engines. Thymeleaf is an XML/XHTML/HTML5 template engine that can work both in
web and non-web environments. If allows you to create natural templates that can be
correctly displayed by browsers and therefore work also as static prototypes. Both
FreeMarker and Thymeleaf templates will be picked up automatically from
`src/main/resources/templates`.
TIP: JSPs should be avoided if possible, there are several
<<boot-features-jsp-limitations, known limitations>> when using them with embedded

View File

@@ -229,6 +229,9 @@ and Hibernate.
|`spring-boot-starter-data-rest`
|Support for exposing Spring Data repositories over REST via `spring-data-rest-webmvc`.
|`spring-boot-starter-freemarker`
|Support for the FreeMarker templating engine
|`spring-boot-starter-integration`
|Support for common `spring-integration` modules.