springboot調(diào)用HTML文件注意事項(xiàng)及說(shuō)明
springboot調(diào)用HTML文件注意事項(xiàng)
1.配置thymeleaf
2.HTML代碼頭部需要添加以下代碼
<link rel="stylesheet" type="text/css" href="/static/css/style.css" rel="external nofollow" th:href="@{css/style.css}" rel="external nofollow" />其中th為
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3.關(guān)于controller和requestmapping
@Controller
//只能使用controller,如果使用RestController將只會(huì)返回字符串不會(huì)返回html頁(yè)面
@RequestMapping("/login") ?//此處的request。。。是下面整個(gè)模塊的地址,如果想要訪問(wèn)下面的方法
需要在下面的方法上面單獨(dú)請(qǐng)求
public class LoginController {
? ? @RequestMapping("") ?//需要在此處單獨(dú)請(qǐng)求
? ? public String login(){
? ? ? ? return "/login";
? ? }
}springboot項(xiàng)目訪問(wèn)HTML頁(yè)面
引入相關(guān)依賴
<!--支持跳轉(zhuǎn),springboot推薦使用thymeleaf模板引擎--> <dependency> ? ? <groupId>org.springframework.boot</groupId> ? ? <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
<properties> ? ? <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> ? ? <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> ? ? <java.version>1.8</java.version> ? ? <!--指定themleaft版本--> ? ? <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> ? ? <thymeleaf-layout-dialect.version>2.0.5</thymeleaf-layout-dialect.version> </properties>
增加springboot配置項(xiàng)
#thymeleaf模版前綴 spring.thymeleaf.prefix=classpath:/templates/
在 src/main/resources 目錄下新建 static 目錄和 templates 目錄。 static存放靜態(tài)文件,templates 存放靜態(tài)頁(yè)面(thymeleaf 模版)

在控制器中寫(xiě)明跳轉(zhuǎn)模版方法

Handler訪問(wèn)映射地址跳轉(zhuǎn)模版成功
總結(jié)這次遇到的問(wèn)題
1. Error resolving template template might not exist or might not be accessible
控制器方法返回的模版名稱沒(méi)有前綴/,可手動(dòng)添加/或添加springboot配置項(xiàng)
2. org.xml.sax.SAXParseException: 元素類型 “link” 必須由匹配的結(jié)束標(biāo)記 “” 終止,org.xml.sax.SAXParseException: 元素類型 “meta” 必須由匹配的結(jié)束標(biāo)記 “” 終止
開(kāi)發(fā)工具生成的html頁(yè)面元素有的沒(méi)有終止符/,thymeleaf模板引擎默認(rèn)是Template modes:HTML5解析的,解析比較嚴(yán)格。
需要手動(dòng)添加/或指定引入的thymeleaf版本號(hào)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot tomcat jdbc pool的屬性綁定
這篇文章主要介紹了spring boot tomcat jdbc pool的屬性綁定的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2018-01-01
一文詳解Java過(guò)濾器攔截器實(shí)例逐步掌握
這篇文章主要為大家介紹了Java過(guò)濾器攔截器實(shí)例詳解逐步掌握,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Mybatis 實(shí)現(xiàn)動(dòng)態(tài)組裝查詢條件,仿SQL模式
這篇文章主要介紹了Mybatis 實(shí)現(xiàn)動(dòng)態(tài)組裝查詢條件,仿SQL模式的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
jdbc連接數(shù)據(jù)庫(kù)實(shí)例詳解
在本篇內(nèi)容里小編給大家分享了關(guān)于jdbc如何連接數(shù)據(jù)庫(kù)的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-02-02
Java中使用fileupload組件實(shí)現(xiàn)文件上傳功能的實(shí)例代碼
這篇文章主要介紹了Java中使用fileupload組件實(shí)現(xiàn)文件上傳功能的實(shí)例代碼,需要的朋友可以參考下2017-05-05
Spring?Boot?使用?Disruptor?做內(nèi)部高性能消息隊(duì)列
這篇文章主要介紹了Spring?Boot?使用?Disruptor?做內(nèi)部高性能消息隊(duì)列,工作中遇到項(xiàng)目使用Disruptor做消息隊(duì)列,對(duì)你沒(méi)看錯(cuò),不是Kafka,也不是rabbitmq。Disruptor有個(gè)最大的優(yōu)點(diǎn)就是快,還有一點(diǎn)它是開(kāi)源的哦,下面做個(gè)簡(jiǎn)單的記錄2022-06-06
SpringData JPA審計(jì)功能(@CreatedDate與@LastModifiedDate)實(shí)現(xiàn)
Spring Data JPA的審計(jì)功能提供了一種強(qiáng)大而靈活的機(jī)制,用于自動(dòng)跟蹤實(shí)體的創(chuàng)建和修改信息,通過(guò)使用@CreatedDate和@LastModifiedDate注解,開(kāi)發(fā)者可以輕松地實(shí)現(xiàn)時(shí)間審計(jì),感興趣的可以了解一下2025-04-04

