Thymeleaf
- 作为XML/XHTML/HTML5模板引擎,thymeleaf可以替代 Framemarker/JSP等
- Springboo对thymeleaf的支持也是非常好
- 其他的标签什么的参考官网
build.gradle中引入 thymeleaf
compile ('org.springframework.boot:spring-boot-starter-thymeleaf')
compile ('org.springframework.boot:spring-boot-starter-web')
application.yml 设置 thyemleaf
- 文件path:resources下的web
- 文件后缀: .html
spring:
thymeleaf:
cache: false
mode: LEGACYHTML5
prefix: classpath:/web/
suffix: .html
Controller 返回内容
public String testRibbon(String content, ModelMap modelMap) {
System.out.println(content);
String resultStr = restTemplate.getForEntity(serverURI+"testRealRibbon?content="+content,String.class).getBody();
modelMap.addAttribute("result",resultStr);
return "index";
}
index.html内容
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Test the Thymeleaf</h1>
<span th:text = "${result}"></span>
</body>
</html>
可能的错误
- 引入的jar错误
- 默认的path为 teamplates
- path多层目录可能出现的错误
集成FreeMarker
引入 spring-boot-starter-freemarker
compile ('org.springframework.boot:spring-boot-starter-freemarker')
集成JSP
引入 spring-boot-starter-freemarker
compile ('org.springframework.boot:spring-boot-starter-tomcat')
compile ('org.apache.tomcat.embed:tomcat-embed-jasper')
compile ('javax.servlet:jstl')
application.yml中加入jsp配置
spring:
mvc:
view:
prefix: /jsp
suffix: .jsp
更多集成参考springboot github
代码
代码请移步 Github参考地址
如有疑问请加公众号(K171),如果觉得对您有帮助请 github start