spring_reference/IV. Spring Boot features/26.1.5. Template engines.md

17 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 26.1.5. 模板引擎
正如REST web服务你也可以使用Spring MVC提供动态HTML内容。Spring MVC支持各种各样的模板技术包括Velocity, FreeMarker和JSPs。很多其他的模板引擎也提供它们自己的Spring MVC集成。
Spring Boot为以下的模板引擎提供自动配置支持
1. [FreeMarker](http://freemarker.org/docs/)
2. [Groovy](http://beta.groovy-lang.org/docs/groovy-2.3.0/html/documentation/markup-template-engine.html)
3. [Thymeleaf](http://www.thymeleaf.org/)
4. [Velocity](http://velocity.apache.org/)
**注**如果可能的话应该忽略JSPs因为在内嵌的servlet容器使用它们时存在一些[已知的限制](http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-jsp-limitations)。
当你使用这些引擎的任何一种并采用默认的配置你的模板将会从src/main/resources/templates目录下自动加载。
**注**IntelliJ IDEA根据你运行应用的方式会对classpath进行不同的整理。在IDE里通过main方法运行你的应用跟从Maven或Gradle或打包好的jar中运行相比会导致不同的顺序。这可能导致Spring Boot不能从classpath下成功地找到模板。如果遇到这个问题你可以在IDE里重新对classpath进行排序将模块的类和资源放到第一位。或者你可以配置模块的前缀为classpath*:/templates/这样会查找classpath下的所有模板目录。