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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Springboot使用thymeleaf进行页面跳转

發(fā)布時(shí)間:2023/12/31 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springboot使用thymeleaf进行页面跳转 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目前Java Web開發(fā)推薦使用模板引擎,不建議使用JSP頁面
JSP缺點(diǎn):本質(zhì)上就是Servlet,需要后臺(tái)編譯,耗時(shí),效率低
模板引擎:不需要編譯,速度快
常見的模板引擎:
FreemarkerVelocityThymeleaf
SpringBoot推薦使用
Thymeleaf(C母賴夫),且默認(rèn)不支持JSP,因?yàn)镴SP必須要打包war包才行

現(xiàn)階段項(xiàng)目中更多使用MVVM框架,前后段分離如:Vue.js、Angular、React。

添加thymeleaf模板引擎需要修改兩處文件(其實(shí)添加一個(gè)pom依賴就ok)。

1.pom文件, <!-- 引入 thymeleaf 模板依賴 -->

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

2..application.properties文件,新增下面配置。

spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.cache=false

3.java代碼 如下方式,templates目錄下的HTML頁面默認(rèn)不能被直接訪問,需要通過controller來訪問,由thymeleaf來渲染

@ControllerAdvice @RequestMapping("index") public class SpringBootController {@RequestMapping("/thymeleaf")public String thymeleaf(Model model) {model.addAttribute("name", "qushen");System.out.println("從Controller跳轉(zhuǎn)thymeleaf");return "user/thymeleaf";} }

HTML寫法如下:具體Thymeleaf寫法可以參考官網(wǎng):https://www.thymeleaf.org/documentation.html

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body><div>hello Spring boot!</div><p th:text="${name}">Welcome to our grocery store!</p></body> </html>s

?顯示效果如下:

?

?

?

總結(jié)

以上是生活随笔為你收集整理的Springboot使用thymeleaf进行页面跳转的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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