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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot集成Thymeleaf

發(fā)布時間:2023/12/15 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot集成Thymeleaf 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

上一篇給大家介紹了springboot整合freemarker,這一片來繼續(xù)為大家介紹一種模板thymeleaf。

首先在項目中增添thymeleaf依賴spring-boot-starter-thymeleaf 同時為了解決html嚴(yán)格校驗報錯的問題,增添依賴nekohtml pom文件代碼如下:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.dalaoyang</groupId><artifactId>springboot_thymeleaf</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot_thymeleaf</name><description>springboot_thymeleaf</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.10.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>net.sourceforge.nekohtml</groupId><artifactId>nekohtml</artifactId><version>1.9.15</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>復(fù)制代碼

controller代碼大致與freemarker相同,代碼如下:

package com.dalaoyang.controller;import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping;/*** @author dalaoyang* @Description* @project springboot_learn* @package com.dalaoyang.controller* @email 397600342@qq.com* @date 2018/3/14*/ @Controller public class TestController {@RequestMapping("/")public String testThymeleaf(ModelMap modelMap){modelMap.addAttribute("msg", "Hello dalaoyang , this is thymeleaf");return "thymeleaf";} } 復(fù)制代碼

application.properties如下:

##端口號 server.port=8888##去除thymeleaf的html嚴(yán)格校驗 spring.thymeleaf.mode=LEGACYHTML5#設(shè)定thymeleaf文件路徑 默認(rèn)為src/main/resources/templates spring.thymeleaf.prefix=classpath:/templates/ #設(shè)定靜態(tài)文件路徑,js,css等 spring.mvc.static-path-pattern=/static/** # 是否開啟模板緩存,默認(rèn)true # 建議在開發(fā)時關(guān)閉緩存,不然沒法看到實時頁面 spring.thymeleaf.cache=false # 模板編碼 spring.thymeleaf.encoding=UTF-8 復(fù)制代碼

html代碼如下

<!DOCTYPE html> <!--解決th報錯 --> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head><meta charset="UTF-8"><title>thymeleaf</title> </head> <body> <h1 th:text="${msg}"></h1> </body> </html> 復(fù)制代碼

啟動項目,訪問http://localhost:8888/ 即可看到以下頁面,

源碼下載 :大老楊碼云

總結(jié)

以上是生活随笔為你收集整理的SpringBoot集成Thymeleaf的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。