美文网首页
Spring Boot - 模板引擎

Spring Boot - 模板引擎

作者: yuanzicheng | 来源:发表于2017-09-26 14:00 被阅读126次

在移动端大行其道的现在,前后端分离成为大势所趋,jsp这种传统的动态页面也开始逐渐退出历史舞台了,但Spring Boot还是提供了动态页面支持。

Spring Boot建议使用模板引擎(如:Thymeleaf、FreeMarker、Groovy)而不是jsp来渲染页面,Sping Boot使用模板引擎时默认的模板配置路径为src/main/resources/templates

1.Thymeleaf

1.1 添加依赖

dependencies {
    compile group: 'com.alibaba', name: 'fastjson', version: '1.2.38'
    compile('org.springframework.boot:spring-boot-starter')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

1.2 后端Controller层代码:WebController.java

@SuppressWarnings("unused")
@Controller
public class WebController {

    @RequestMapping("/test")
    public String index(ModelMap modelMap){
        modelMap.addAttribute("test","thymeleaf");
        return "thymeleaf";
    }

}

还有一种常见用法是不通过SpringMVC直接返回页面内容,而是将模板页面内容处理后以邮件发送

// kotlin代码片段
val context = Context()
context.setVariable("email", email)
context.setVariable("link", "${link}/register?key=$key")
val content = springTemplateEngine.process("invite", context)
mailHelper.sendHtml(email!!, "邀请注册", content)

1.3 前端页面:./src/main/resources/templates/thymeleaf.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
<div th:text="${test}">Hello World<div>
</body>
</html>

1.4 配置文件
如果要修改Thymeleaf默认的配置,需要在application.properties中修改(以下内容来自官网https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle的Part X部分)

# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.cache=true # Enable template caching.
spring.thymeleaf.check-template=true # Check that the template exists before rendering it.
spring.thymeleaf.check-template-location=true # Check that the templates location exists.
spring.thymeleaf.enabled=true # Enable Thymeleaf view resolution for Web frameworks.
spring.thymeleaf.encoding=UTF-8 # Template files encoding.
spring.thymeleaf.excluded-view-names= # Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.mode=HTML5 # Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.reactive.max-chunk-size= # Maximum size of data buffers used for writing to the response, in bytes.
spring.thymeleaf.reactive.media-types= # Media types supported by the view technology.
spring.thymeleaf.servlet.content-type=text/html # Content-Type value written to HTTP responses.
spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain.
spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.

2.FreeMarker

3.Groovy

4.Velocity

各种模板引擎的使用其实比较类似,而且目前的趋势是前后端分离,后端只提供API,所以模板引擎的使用并不是很多,其它模板引擎的具体用法不一一赘述了。

相关文章

网友评论

      本文标题:Spring Boot - 模板引擎

      本文链接:https://www.haomeiwen.com/subject/tybfsxtx.html