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.訪問路徑,完成
http://localhost:8080/templates/common
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java Serializable和Parcelable詳解及實(shí)例代碼
這篇文章主要介紹了Java Serializable和Parcelable詳解,并附實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09Java設(shè)置token有效期的5個應(yīng)用場景(雙token實(shí)現(xiàn))
Token最常見的應(yīng)用場景之一就是身份驗(yàn)證,本文主要介紹了Java設(shè)置token有效期的5個應(yīng)用場景(雙token實(shí)現(xiàn)),具有一定的參考價值,感興趣的可以來了解一下2024-04-04java實(shí)現(xiàn)統(tǒng)計字符串中大寫字母,小寫字母及數(shù)字出現(xiàn)次數(shù)的方法示例
這篇文章主要介紹了java實(shí)現(xiàn)統(tǒng)計字符串中大寫字母,小寫字母及數(shù)字出現(xiàn)次數(shù)的方法,涉及java針對字符串的遍歷、判斷、運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2019-06-06Java如何將int型數(shù)組轉(zhuǎn)為String型數(shù)組
這篇文章主要介紹了Java如何將int型數(shù)組轉(zhuǎn)為String型數(shù)組,本文給大家分享具體實(shí)現(xiàn)思路結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-03-03Java集合框架迭代器Iterator實(shí)現(xiàn)原理解析
這篇文章主要介紹了Java集合框架迭代器Iterator實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08Spring boot項(xiàng)目打包成jar運(yùn)行的二種方法
這篇文章主要給大家介紹了關(guān)于Spring boot項(xiàng)目打包成jar運(yùn)行的二種方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11Java導(dǎo)出多個excel表打包到zip文件中供客戶端另存為窗口下載實(shí)現(xiàn)方法
最近的項(xiàng)目有一個導(dǎo)出匯總數(shù)據(jù)的要求,考慮到用戶軟件的差異,所以要分別導(dǎo)出xls以及xlsx并且打包提供下載,下面這篇文章主要給大家介紹了關(guān)于Java導(dǎo)出多個excel表打包到zip文件中供客戶端另存為窗口下載的實(shí)現(xiàn)方法,需要的朋友可以參考下2023-12-12