springboot如何使用thymeleaf模板訪問html頁(yè)面
引言
在傳統(tǒng)的web開發(fā)中通常使用jsp頁(yè)面,首先需要在pom文件中引入springmvc相關(guān)的包,然后寫springmvc的配置文件(包括訪問資源的路徑解析),之后還需再web.xml中配置訪問路由。這無(wú)疑太麻煩了,每次開發(fā)前都需要編寫大量的配置文件。
springboot為此提供了高效便捷的解決方案,只需再pom.xml中添加web開發(fā)的依賴,便可進(jìn)行web開發(fā),省去了繁瑣的配置步驟。
下面為web開發(fā)引入的依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
正文
那么在springboot中如果需要使用頁(yè)面該怎么做呢?springboot不推薦使用jsp,因?yàn)閖sp在springboot中有諸多限制,具體限制這里就不展開說了,大家感興趣可以去網(wǎng)上查閱。springboot中推薦使用thymeleaf模板,使用html作為頁(yè)面展示。那么如何通過Controller來(lái)訪問來(lái)訪問html頁(yè)面呢?
1.在pom.xml文件中添加thymeleaf依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
2.在application.yml中添加訪問請(qǐng)求配置
##thymeleaf頁(yè)面模板配置 spring: mvc: view: prefix: / suffix: .html
springboot中默認(rèn)resources中static文件夾存放靜態(tài)資源,如js文件、css文件、圖片等等。templates文件夾中存放html頁(yè)面。
3.在templates文件夾中創(chuàng)建hello.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <title>Title</title> </head> <body> hello world </body> </html>
4.編寫Controller
/**
* Created by Tomthy on 2018/5/10
*/
@Controller
public class ContentController {
@GetMapping("/hello")
private String helloWorld(){
return "hello";
}
}
注意:不要使用@RestController注解,@RestController注解是@ResponseBody和@Controller的集合體,使用@RestController注解會(huì)默認(rèn)返回?cái)?shù)據(jù),而不會(huì)請(qǐng)求到頁(yè)面。
5.在瀏覽器中輸入請(qǐng)求地址
輸入地址:http://localhost:8080/hello便可請(qǐng)求到hello.html頁(yè)面。

6.靜態(tài)資源的訪問
html頁(yè)面中使用到靜態(tài)資源時(shí)(如圖片),直接使用<script type="text/javascript" src="/js/wangEditor.js"></script>。js為static下的文件夾。
7.項(xiàng)目目錄

總結(jié)
以上所述是小編給大家介紹的springboot使用thymeleaf模板訪問html頁(yè)面,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Springboot詳解如何整合使用Thymeleaf
- springboot+thymeleaf整合阿里云OOS對(duì)象存儲(chǔ)圖片的實(shí)現(xiàn)
- springboot整合shiro之thymeleaf使用shiro標(biāo)簽的方法
- SpringBoot整合thymeleaf 報(bào)錯(cuò)的解決方案
- SpringBoot使用thymeleaf實(shí)現(xiàn)一個(gè)前端表格方法詳解
- Springboot使用thymeleaf動(dòng)態(tài)模板實(shí)現(xiàn)刷新
- 淺析SpringBoot中使用thymeleaf找不到.HTML文件的原因
- springboot中thymeleaf模板使用詳解
- SpringBoot?整合Thymeleaf教程及使用方法
相關(guān)文章
在Springboot中Mybatis與Mybatis-plus的區(qū)別詳解
MyBatis是一個(gè)優(yōu)秀的持久層框架,它對(duì)JDBC的操作數(shù)據(jù)庫(kù)的過程進(jìn)行封裝,MyBatisPlus (簡(jiǎn)稱 MP)是一個(gè) MyBatis的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生,本文將給大家介紹了在Springboot中Mybatis與Mybatis-plus的區(qū)別2023-12-12
MyBatis 參數(shù)映射機(jī)制實(shí)踐記錄
這篇文章主要介紹了MyBatis 參數(shù)映射機(jī)制實(shí)踐記錄,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-12-12
springcloud 熔斷監(jiān)控Hystrix Dashboard和Turbine
這篇文章主要介紹了springcloud 熔斷監(jiān)控Hystrix Dashboard和Turbine,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Java使用正則表達(dá)式驗(yàn)證手機(jī)號(hào)和電話號(hào)碼的方法
今天小編就為大家分享一篇關(guān)于Java使用正則表達(dá)式驗(yàn)證手機(jī)號(hào)和電話號(hào)碼的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Java多線程 兩階段終止模式Two-Phase Termination Patter
這篇文章主要介紹了Java多線程 兩階段終止模式Two-Phase Termination Patter,該模式有兩個(gè)角色,分別是Terminator,終止者,負(fù)責(zé)接收終止請(qǐng)求,執(zhí)行終止處理,處理完成后再終止自己。TerminationRequester終止請(qǐng)求發(fā)出者,用來(lái)向Terminator發(fā)出終止請(qǐng)求,需要的朋友可以參考一下2021-10-10

