日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

SSM框架—Thymeleaf模板引擎 Spring5整合Thymeleaf(XML配置)

發布時間:2023/12/20 asp.net 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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配置)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。