SpringBoot 整合模板引擎
一 SpringBoot整合freemarker
1.引入freemarker依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
二 SpringBoot整合thymeleaf
1.引入thymeleaf依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Thymeleaf 支持渲染HTML,因此通常我們使用的頁面模板也就是HTML,同時它需要遵循一定的規則:
-
比如在spring boot項目中,通常我們將Thymeleaf渲染的頁面放在 resources/templates目錄下,這樣Thymeleaf會默認識別。
-
若想要在HTML頁面中使用Thymeleaf,需要修改 <htmllang=“en”>爲 <htmllang="en"xmlns:th=“http://www.thymeleaf.org”>
-
在spring boot項目中 resources/static目錄下的文件可通過瀏覽器直接訪問,而 resources/templates下的HTML不能通過瀏覽器直接訪問,而需要 SpringMvc這樣的框架映射到那個頁面地址。
2.修改 application.properties
#thymeleaf靜態資源配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.suffix=.html
#開發時關閉緩存,不然沒法看到實時頁面
spring.thymeleaf.cache=false
3.Demo
在 main/java目錄下新建 RouterController.java
@RequestMapping("/index")
public String index(ModelMap map) {
map.addAttribute("name", "thymeleaf-imooc");
return "index";
}
這個 return"index"中 index是 templates根目錄下的 index.html,如果是 templates/common/main.html頁面,就應該 return"/common/main.html"
在 main/resources/templates目錄下新建 index.html
4.常用表達式
變量表達式
變量表達式級即OGNL表達式或Spring EL表達式(在Spring術語中也叫做model attributes),例如:
${user.username}
1.修改 RouterController.java
@RequestMapping("/index")
public String index(ModelMap map) {
map.addAttribute("name", "thymeleaf-imooc");
return "thymeleaf/index";
}
2.修改 index.html
<body>
Thymeleaf模板引擎
<h1 th:text="${name}">hello world~~~~~~~</h1>
</body>
選擇*號表達式
如果 model.addAttribute()的是一個對象而不是字符串,就可以用 *表達式分別獲取 model對象中的元素,如:
`*{...}`表達式:
<b th:object="${user}">
<span th:text="*{username}"></span>
<span th:text="*{password}"></span>
</b>
如果頁面需要不在 th:object包裹下獲取對象中的元素,可以使用如下:
<span th:text="${user.username}"></span>
URL表達式
例如:
<script th:src="@{/css/base.css}"></script>
會自動引入 resources/statis/css/下的 base.css文件,這和JSP頁面中使用:
<script src="${pageContext.request.contextPath}/css/base.css"></script>
是一個道理。
同理:
<link rel="stylesheet" th:href="@{/css/base.css}"/>
表達式支持的語法
字面(Literals)
文本文字(Text literals): ‘one text’, ‘Another one!’,…
數字文本(Number literals): 0, 34, 3.0, 12.3,…
布爾文本(Boolean literals): true, false
空(Null literal): null
文字標記(Literal tokens): one, sometext, main,…
文本操作(Text operations)
字符串連接(String concatenation): +
文本替換(Literal substitutions): |The name is ${name}|
算術運算(Arithmetic operations)
二元運算符(Binary operators): +, -, *, /, %
減號(單目運算符)Minus sign (unary operator): -
布爾操作(Boolean operations)
二元運算符(Binary operators):and, or
布爾否定(一元運算符)Boolean negation (unary operator):!, not
比較和等價(Comparisons and equality)
比較(Comparators): >, <, >=, <= (gt, lt, ge, le)
等值運算符(Equality operators):==, != (eq, ne)
條件運算符(Conditional operators)
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
所有這些特徵可以被組合並嵌套:
'User is of type '+(${user.isAdmin()}?'Administrator': (${user.type} ?:'Unknown'))