Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use thymeleaf as a template for springboot

Shulou Source: shulou.com Published: 2022-06-02 06:40:41 09月30日 Update

This article mainly explains "how springboot uses thymeleaf as a template". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how springboot uses thymeleaf as a template" together!

Jsp has obviously not kept up with the development of the times. Is freemarker used enough? Try thymeleaf instead. Springboot officially recommends freemarker and thymeleaf, and thymeleaf is more powerful than freemarker, it can dynamically replace the static content in the tag, so that the front end can write pages, the background can be peace of mind, just need to replace the variables, this idea, do not know whether VUE copied thymeleaf or thymeleaf copied VUE, but it does not matter, for our majority of code slaves, practical is good

01

POM introduction

org.springframework.boot

spring-boot-starter-thymeleaf

02

application.properties Add thymeleaf configuration

spring.thymeleaf.cache=false

spring.thymeleaf.check-template=true

spring.thymeleaf.check-template-location=true

spring.thymeleaf.content-type=text/html

spring.thymeleaf.enabled=true

spring.thymeleaf.encoding=utf-8

spring.thymeleaf.mode=HTML5

spring.thymeleaf.prefix=classpath:/templates/

spring.thymeleaf.suffix=.html

03

write html

demo

This is the first paragraph.

This is the second paragraph.

04

test class

@RequestMapping("index")

public String index(Model model){

String textValue = "Sergeant heard the Tao, only can do it; Sergeant heard the Tao, if alive; Corporal heard the Tao, laugh." " +

"Not laughing is not enough. " +

"Therefore, there are suggestions: if the way is clear, if it is ignorant; if it is advanced, if it is retreated; if it is barbarians, if it is lei; if it is virtuous, if it is valley, if it is white, if it is humiliated, if it is not virtuous, if it is virtuous, if it is stolen, if it is true; if it is generous, there is no corner; if it is big, it will not be formed; if it is loud, it will not be loud; if it is invisible, it will be invisible. " +

"Dao Yin nameless. " +

"Only Tao, good beginning and good completion. ";

model.addAttribute("textValue",textValue);

return "demo/demo";

}

05

page effect

6

VUE has a SSR (server-side rendering) problem, although there are solutions (see my previous article "front-end separation Nuxt.js to solve SEO problems"), but always feel that it is better to let the appropriate technology do the business at the time, and thymeleaf is equivalent to rendering on the server side, view the page source code as follows:

Thank you for reading, the above is "springboot how to use thymeleaf as a template" content, after the study of this article, I believe we have a deeper understanding of how springboot uses thymeleaf as a template, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Template content or question page study article this is plagiarism service generous headache powerful obvious if you retreat big voice it doesn't matter if you survive staff Sergeant Corporal. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno Apple Huawei Xiaomi