SpringBoot thymeleaf的使用方法解析
1.pom.xml添加相應(yīng)依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2.application.properties
#thymeleaf spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html; charset=utf-8 spring.thymeleaf.cache=false
3.common.xml文件,注意文件路徑
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Insert title here</title>
</head>
<body>
<h1>my first thymeleaf.</h1>
hello, <span th:text="${name}"></span>
</body>
</html>

4.添加TemplateController.java
package myshop.controller;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/*
* 這里使用@Controller而不是@RestController
* 還有模板文件中得去掉<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
* 所有標(biāo)簽得閉合
*
* */
@Controller
@RequestMapping("/templates")
public class TemplateController {
@RequestMapping("/common")
public String Common(Map<String, Object> map)
{
map.put("name", "天恒");
return "Common";
}
}
5.添加app.java
package myshop;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(App.class, args);
}
}
6.訪問(wèn)路徑,完成
http://localhost:8080/templates/common
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot+MySQL+Jpa實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的增刪改查和分頁(yè)詳解
- spring boot jpa寫原生sql報(bào)Cannot resolve table錯(cuò)誤解決方法
- 使用SpringBoot-JPA進(jìn)行自定義保存及批量保存功能
- Spring Boot中使用Spring-data-jpa的配置方法詳解
- SpringBoot整合Thymeleaf的方法
- springboot2.1.7整合thymeleaf代碼實(shí)例
- Spring boot2+jpa+thymeleaf實(shí)現(xiàn)增刪改查
相關(guān)文章
SpringBoot配置GlobalExceptionHandler全局異常處理器案例
這篇文章主要介紹了SpringBoot配置GlobalExceptionHandler全局異常處理器案例,通過(guò)簡(jiǎn)要的文章說(shuō)明如何去進(jìn)行配置以及使用,需要的朋友可以參考下2021-06-06
JVM默認(rèn)時(shí)區(qū)為:Asia/Shanghai與java程序中GMT+08不一致異常
這篇文章主要介紹了JVM默認(rèn)時(shí)區(qū)為:Asia/Shanghai與java程序中GMT+08不一致異常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
SpringBoot+Redisson自定義注解一次解決重復(fù)提交問(wèn)題
項(xiàng)目中經(jīng)常會(huì)出現(xiàn)重復(fù)提交的問(wèn)題,本文主要介紹了SpringBoot+Redisson自定義注解一次解決重復(fù)提交問(wèn)題,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
解決springboot 無(wú)法配置多個(gè)靜態(tài)路徑的問(wèn)題
這篇文章主要介紹了解決springboot 無(wú)法配置多個(gè)靜態(tài)路徑的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
@Async導(dǎo)致controller?404及失效原因解決分析
這篇文章主要為大家介紹了@Async導(dǎo)致controller?404失效問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
如何設(shè)置springboot禁止日志輸出到控制臺(tái)
文章總結(jié):本文主要介紹了SpringBoot項(xiàng)目中使用SLF4J記錄日志時(shí),日志默認(rèn)輸出到控制臺(tái)的原因及解決方法,日志框架如Logback默認(rèn)會(huì)將日志輸出到控制臺(tái),可以通過(guò)`logback-spring.xml`配置文件或配置類來(lái)禁止日志輸出到控制臺(tái),并設(shè)置日志輸出級(jí)別2025-01-01
springboot3整合SpringSecurity實(shí)現(xiàn)登錄校驗(yàn)與權(quán)限認(rèn)證
本文主要介紹了springboot3整合SpringSecurity實(shí)現(xiàn)登錄校驗(yàn)與權(quán)限認(rèn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04

