SSM框架—Thymeleaf模板引擎 Spring5整合Thymeleaf(XML配置)
生活随笔
收集整理的這篇文章主要介紹了
SSM框架—Thymeleaf模板引擎 Spring5整合Thymeleaf(XML配置)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 依賴
? 在配置好SSM框架后,在pom.xml中添加如下依賴
?
<dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring5</artifactId><version>3.0.9.RELEASE</version> </dependency>2. 配置文件
? 在Sping_mvc.xml中加入如下配置,并且注釋掉jsp的viewResolver或freemarker的配置
?
<bean id="templateResolver"class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"><property name="prefix" value="/WEB-INF/templates/"/><property name="suffix" value=".html"/><property name="characterEncoding" value="UTF-8"/><property name="order" value="1"/><property name="templateMode" value="HTML5"/><property name="cacheable" value="false"/> </bean><bean id="templateEngine"class="org.thymeleaf.spring5.SpringTemplateEngine"><property name="templateResolver" ref="templateResolver"/> </bean><bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"><property name="templateEngine" ref="templateEngine"/><property name="characterEncoding" value="UTF-8"/> </bean>此配置需要注意以下幾點:
- templateResolver的prefix與suffix對應你的視圖層的文件位置
- templateResolver的characterEncoding和viewResolver的都要設置成UTF-8中文才不會亂碼。
- templateResolver的cacheable一定要在開發的時候設置成false不然無法看到實時的頁面數據
3. 測試
-
controller:
在ModelMap里面隨便設置一點值
@RequestMapping("/test") public String test(ModelMap map) {map.put("thText", "設置文本內容");map.put("thUText", "設置文本內容");map.put("thValue", "設置當前元素的value值");map.put("thEach", Arrays.asList("列表", "遍歷列表"));map.put("thIf", "msg is not null");map.put("thObject", new UserEntity("sadfa","asfasfd","asfsaf","asdfasf","saf","asfd","sadf",1));return "test"; } -
test.html
<!DOCTYPE html> <html lang="cn" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <h1>TEST</h1> <h2>Thymeleaf</h2> <!--th:text 設置當前元素的文本內容,常用,優先級不高--> <p th:text="${thText}" /> <p th:utext="${thUText}" /><!--th:value 設置當前元素的value值,常用,優先級僅比th:text高--> <input type="text" th:value="${thValue}" /><!--th:each 遍歷列表,常用,優先級很高,僅此于代碼塊的插入--> <!--th:each 修飾在div上,則div層重復出現,若只想p標簽遍歷,則修飾在p標簽上--> <div th:each="message : ${thEach}"> <!-- 遍歷整個div-p,不推薦--><p th:text="${message}" /> </div> <div> <!--只遍歷p,推薦使用--><p th:text="${message}" th:each="message : ${thEach}" /> </div><!--th:if 條件判斷,類似的有th:switch,th:case,優先級僅次于th:each, 其中#strings是變量表達式的內置方法--> <p th:text="${thIf}" th:if="${not #strings.isEmpty(thIf)}"></p><!--th:insert 把代碼塊插入當前div中,優先級最高,類似的有th:replace,th:include,~{} :代碼塊表達式 --> <div th:insert="~{grammar/common::thCommon}"></div><!--th:object 聲明變量,和*{} 一起使用--> <div th:object="${thObject}"><p>ID: <span th:text="*{id}" /></p><!--th:text="${thObject.id}"--><p>TH: <span th:text="*{username}" /></p><!--${thObject.thName}--><p>DE: <span th:text="*{password}" /></p><!--${thObject.desc}--> </div> </body> </html>
幾點注意:
-
在html首標簽里面加上xmlns
<html lang="cn" xmlns:th="http://www.thymeleaf.org"> -
同樣把head的meta設置一個charset=“UTF-8”
至此,你就可以去配置tomcat運行項目了。
總結
以上是生活随笔為你收集整理的SSM框架—Thymeleaf模板引擎 Spring5整合Thymeleaf(XML配置)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 嵌入式C语言switch语句
- 下一篇: APS.NET MVC + EF (01