Spring Boot 入门 - 基础篇(6)- 页面模板

Spring Boot支持很多模板引擎,但嵌入式容器JSP有限制,2010年后Velocity停止更新,所以这两个不建议使用。

[b](1)Thymeleaf[/b]
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>


[quote]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)[/quote]

访问 http://localhost:8080/testTH
[img]http://dl2.iteye.com/upload/attachment/0122/9702/8a2f249e-24ff-3a76-b28f-c8b6bfaa13b5.png[/img]

[b]Thymeleaf的默认设置[/b]
application.properties
[quote]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.[/quote]

[b]LEGACYHTML5[/b]
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>


[b]Thymeleaf以标签的属性形式出现,以下是常用的属性:[/b]
[quote]1)th:text 标签内显示数据 [b]<p th:text="${text}">test</p>[/b]
2)th:href 链接的URL [b]<link th:href="@{bootstrap/css/bootstrap.min.css}" rel="stylesheet" />[/b]
3)th:if、th:unless 简单判断 [b]<p th:if="${errorMsg} != null">error</p>[/b]
4)th:each 循环输出 *注意不是<tbody>而是<tr>循环输出
[b] <tbody th:each="list : ${beans}">
<tr>...</tr>
</tbody> [/b]
5)th:switch、th:case 分支判断
[b] <td th:switch="${num}">
<p th:case="0">ZERO</p>
<p th:case="1">ONE</p>
<p th:case="*">NUM</p>
</td>[/b]
6)数据访问模式:链接(@{...})、变量(${...})、国际化文字(#{...})、选择表达式(*{...})
[/quote]

[b]模板重用[/b]
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>


[b](2)FreeMarker[/b]

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>


[quote]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)[/quote]

访问 http://localhost:8080/testFM
[img]http://dl2.iteye.com/upload/attachment/0122/9704/3e28d55d-dd08-3167-bb18-1e6981fc7ee9.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值