springboot調(diào)用HTML文件注意事項(xiàng)及說明
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將只會返回字符串不會返回html頁面
@RequestMapping("/login") ?//此處的request。。。是下面整個模塊的地址,如果想要訪問下面的方法
需要在下面的方法上面單獨(dú)請求
public class LoginController {
? ? @RequestMapping("") ?//需要在此處單獨(dú)請求
? ? public String login(){
? ? ? ? return "/login";
? ? }
}springboot項(xiàng)目訪問HTML頁面
引入相關(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)頁面(thymeleaf 模版)

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

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

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot tomcat jdbc pool的屬性綁定
這篇文章主要介紹了spring boot tomcat jdbc pool的屬性綁定的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友參考下2018-01-01
Mybatis 實(shí)現(xiàn)動態(tài)組裝查詢條件,仿SQL模式
這篇文章主要介紹了Mybatis 實(shí)現(xiàn)動態(tài)組裝查詢條件,仿SQL模式的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
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ì)列,對你沒看錯,不是Kafka,也不是rabbitmq。Disruptor有個最大的優(yōu)點(diǎn)就是快,還有一點(diǎn)它是開源的哦,下面做個簡單的記錄2022-06-06
SpringData JPA審計(jì)功能(@CreatedDate與@LastModifiedDate)實(shí)現(xiàn)
Spring Data JPA的審計(jì)功能提供了一種強(qiáng)大而靈活的機(jī)制,用于自動跟蹤實(shí)體的創(chuàng)建和修改信息,通過使用@CreatedDate和@LastModifiedDate注解,開發(fā)者可以輕松地實(shí)現(xiàn)時間審計(jì),感興趣的可以了解一下2025-04-04

