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)文章
Java Serializable和Parcelable詳解及實(shí)例代碼
這篇文章主要介紹了Java Serializable和Parcelable詳解,并附實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09
Java設(shè)置token有效期的5個(gè)應(yīng)用場(chǎng)景(雙token實(shí)現(xiàn))
Token最常見的應(yīng)用場(chǎng)景之一就是身份驗(yàn)證,本文主要介紹了Java設(shè)置token有效期的5個(gè)應(yīng)用場(chǎng)景(雙token實(shí)現(xiàn)),具有一定的參考價(jià)值,感興趣的可以來(lái)了解一下2024-04-04
java實(shí)現(xiàn)統(tǒng)計(jì)字符串中大寫字母,小寫字母及數(shù)字出現(xiàn)次數(shù)的方法示例
這篇文章主要介紹了java實(shí)現(xiàn)統(tǒng)計(jì)字符串中大寫字母,小寫字母及數(shù)字出現(xiàn)次數(shù)的方法,涉及java針對(duì)字符串的遍歷、判斷、運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
Java如何將int型數(shù)組轉(zhuǎn)為String型數(shù)組
這篇文章主要介紹了Java如何將int型數(shù)組轉(zhuǎn)為String型數(shù)組,本文給大家分享具體實(shí)現(xiàn)思路結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-03-03
Java集合框架迭代器Iterator實(shí)現(xiàn)原理解析
這篇文章主要介紹了Java集合框架迭代器Iterator實(shí)現(xiàn)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Spring boot項(xiàng)目打包成jar運(yùn)行的二種方法
這篇文章主要給大家介紹了關(guān)于Spring boot項(xiàng)目打包成jar運(yùn)行的二種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Java導(dǎo)出多個(gè)excel表打包到zip文件中供客戶端另存為窗口下載實(shí)現(xiàn)方法
最近的項(xiàng)目有一個(gè)導(dǎo)出匯總數(shù)據(jù)的要求,考慮到用戶軟件的差異,所以要分別導(dǎo)出xls以及xlsx并且打包提供下載,下面這篇文章主要給大家介紹了關(guān)于Java導(dǎo)出多個(gè)excel表打包到zip文件中供客戶端另存為窗口下載的實(shí)現(xiàn)方法,需要的朋友可以參考下2023-12-12
SpringBoot使用布隆過(guò)濾器解決緩存穿透問(wèn)題
緩存穿透是指當(dāng)緩存系統(tǒng)中無(wú)法命中需要的數(shù)據(jù)時(shí),會(huì)直接請(qǐng)求底層存儲(chǔ)系統(tǒng)(如數(shù)據(jù)庫(kù)),但是如果請(qǐng)求的數(shù)據(jù)根本不存在,那么大量的請(qǐng)求就會(huì)直接穿透緩存層,本文將給大家介紹一下SpringBoot使用布隆過(guò)濾器解決緩存穿透問(wèn)題,需要的朋友可以參考下2023-10-10

