Spring Boot支持很多模板引擎,但嵌入式容器JSP有限制,2010年后Velocity停止更新,所以这两个不建议使用。
(1)Thymeleaf
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
src/main/java/com/rensanning/springboot/PageController.java
@Controller
public class PageController {
@RequestMapping("/testTH")
public String testTH(ModelMap map) {
map.addAttribute("msg", "Hello, rensanning! @Thymeleaf");
return "test_th";
}
}
src/main/resources/templates/test_th.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Thymeleaf Sample</title>
</head>
<body>
<h1 th:text="${msg}"></h1>
</body>
</html>
引用
2017-02-09 14:59:16.586 INFO 6596 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/testTH]}" onto public java.lang.String
com.rensanning.springboot.PageController.testTH(org.springframework.ui.ModelMap)
访问 http://localhost:8080/testTH
Thymeleaf的默认设置
application.properties
引用
spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
spring.thymeleaf.mode=HTML5 # Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.encoding=UTF-8 # Template encoding.
spring.thymeleaf.content-type=text/html # Content-Type value.
LEGACYHTML5
spring.thymeleaf.mode=LEGACYHTML5
模板将不会按xhtml输出,html错误将被忽略。比如<br>、<link rel="" href="">、<meta charset="UTF-8">等这些没有闭合的标签,以默认mode是无法访问的。不过需要依赖nekohtml:
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.15</version>
</dependency>
Thymeleaf以标签的属性形式出现,以下是常用的属性:
引用
1)th:text 标签内显示数据 <p th:text="${text}">test</p>
2)th:href 链接的URL <link th:href="@{bootstrap/css/bootstrap.min.css}" rel="stylesheet" />
3)th:if、th:unless 简单判断 <p th:if="${errorMsg} != null">error</p>
4)th:each 循环输出 *注意不是<tbody>而是<tr>循环输出
<tbody th:each="list : ${beans}">
<tr>...</tr>
</tbody>
5)th:switch、th:case 分支判断
<td th:switch="${num}">
<p th:case="0">ZERO</p>
<p th:case="1">ONE</p>
<p th:case="*">NUM</p>
</td>
6)数据访问模式:链接(@{...})、变量(${...})、国际化文字(#{...})、选择表达式(*{...})
模板重用
src/main/resources/templates/base.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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>thymeleaf base</title>
</head>
<body>
<!-- header -->
<header>
<div align="center" >header</div>
<hr />
</header>
<!-- contents -->
<div layout:fragment="content">Contents is here!</div>
<!-- footer -->
<footer>
<hr />
<div align="center">footer</div>
</footer>
</body>
</html>
src/main/resources/templates/contents.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="base">
<head>
<title>Contents</title>
</head>
<body>
<div layout:fragment="content">
<table class="table">
<thead class="sunflower">
<tr>
<td>ID</td>
<td>NAME</td>
</tr>
</thead>
<tbody th:each="list : ${beans}">
<tr>
<td class="text-str" th:text="${list.id}"></td>
<td class="text-str" th:text="${list.name}"></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
(2)FreeMarker
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
src/main/java/com/rensanning/springboot/PageController.java
@Controller
public class PageController {
@RequestMapping("/testFM")
public String testFM(ModelMap map) {
map.addAttribute("msg", "Hello, rensanning! @FreeMarker");
return "test_fm";
}
}
src/main/resources/templates/test_fm.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>FreeMarker Sample</title>
</head>
<body>
<h1>${msg}</h1>
</body>
</html>
引用
2017-02-09 14:59:16.585 INFO 6596 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/testFM]}" onto public java.lang.String
com.rensanning.springboot.PageController.testFM(org.springframework.ui.ModelMap)
访问 http://localhost:8080/testFM
- 大小: 30.9 KB
- 大小: 29.3 KB
分享到:
相关推荐
赠送jar包:spring-boot-actuator-autoconfigure-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-actuator-autoconfigure-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-actuator-autoconfigure-2.3.12....
赠送jar包:seata-spring-boot-starter-1.3.0.jar; 赠送原API文档:seata-spring-boot-starter-1.3.0-javadoc.jar; 赠送源代码:seata-spring-boot-starter-1.3.0-sources.jar; 赠送Maven依赖信息文件:seata-...
赠送jar包:spring-boot-configuration-processor-2.6.3.jar; 赠送原API文档:spring-boot-configuration-processor-2.6.3-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-2.6.3-sources.jar; ...
赠送jar包:spring-boot-configuration-processor-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-configuration-processor-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-...
赠送jar包:druid-spring-boot-starter-1.1.9.jar; 赠送原API文档:druid-spring-boot-starter-1.1.9-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.1.9-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:mybatis-spring-boot-autoconfigure-1.3.2.jar; 赠送原API文档:mybatis-spring-boot-autoconfigure-1.3.2-javadoc.jar; 赠送源代码:mybatis-spring-boot-autoconfigure-1.3.2-sources.jar; 赠送...
赠送jar包:spring-boot-configuration-processor-2.5.6.jar; 赠送原API文档:spring-boot-configuration-processor-2.5.6-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-2.5.6-sources.jar; ...
赠送jar包:spring-boot-autoconfigure-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-autoconfigure-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-autoconfigure-2.3.12.RELEASE-sources.jar; 赠送...
赠送jar包:druid-spring-boot-starter-1.1.10.jar; 赠送原API文档:druid-spring-boot-starter-1.1.10-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.1.10-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:aliyun-sms-spring-boot-starter-2.0.2.jar 赠送原API文档:aliyun-sms-spring-boot-starter-2.0.2-javadoc.jar 赠送源代码:aliyun-sms-spring-boot-starter-2.0.2-sources.jar 包含翻译后的API文档...
赠送jar包:oss-spring-boot-starter-1.0.3.jar; 赠送原API文档:oss-spring-boot-starter-1.0.3-javadoc.jar; 赠送源代码:oss-spring-boot-starter-1.0.3-sources.jar; 赠送Maven依赖信息文件:oss-spring-boot...
赠送jar包:nacos-config-spring-boot-autoconfigure-0.2.7.jar; 赠送原API文档:nacos-config-spring-boot-autoconfigure-0.2.7-javadoc.jar; 赠送源代码:nacos-config-spring-boot-autoconfigure-0.2.7-sources...
赠送jar包:dynamic-datasource-spring-boot-starter-3.4.1.jar; 赠送原API文档:dynamic-datasource-spring-boot-starter-3.4.1-javadoc.jar; 赠送源代码:dynamic-datasource-spring-boot-starter-3.4.1-sources...
赠送jar包:dynamic-datasource-spring-boot-starter-3.4.1.jar; 赠送原API文档:dynamic-datasource-spring-boot-starter-3.4.1-javadoc.jar; 赠送源代码:dynamic-datasource-spring-boot-starter-3.4.1-sources...
赠送jar包:druid-spring-boot-starter-1.2.8.jar; 赠送原API文档:druid-spring-boot-starter-1.2.8-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.2.8-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:druid-spring-boot-starter-1.1.9.jar; 赠送原API文档:druid-spring-boot-starter-1.1.9-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.1.9-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:spring-boot-admin-server-ui-2.5.2.jar; 赠送原API文档:spring-boot-admin-server-ui-2.5.2-javadoc.jar; 赠送源代码:spring-boot-admin-server-ui-2.5.2-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:oss-spring-boot-starter-1.0.3.jar 赠送原API文档:oss-spring-boot-starter-1.0.3-javadoc.jar 赠送源代码:oss-spring-boot-starter-1.0.3-sources.jar 包含翻译后的API文档:oss-spring-boot-...
mybatis-spring-boot-starter-2.1.3.jarmybatis-spring-boot-starter-2.1.3.jarmybatis-spring-boot-starter-2.1.3.jar
spring-boot项目pom.xm中parent依赖文件spring-boot-starter-parent-1.5.13.RELEASE.pom