Preface
During the development process, it is necessary to use the template engine. JSP has obviously not kept up with the development of the times. Is it enough for freemarkers? Try changing to thymeleaf.
Springboot officially recommends freemarker and thymeleaf. What makes thymeleaf feel more powerful than freemarker is that it can dynamically replace static content in the tag, so that the front-end can write pages with peace of mind, and the back-end can play interfaces with peace of mind, just replace variables. I don’t know if VUE plagiarized thymeleaf or thymeleaf plagiarized VUE, but it doesn’t matter. For us code slaves, it’s just practical.
After reviewing the information and configuring it, I will share the implementation process with you. I won’t say much about it below. Let’s take a look at the detailed introduction together.
1. Pom introduction
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2. Application.properties add thymeleaf configuration
spring.thymeleaf.cache=falsespring.thymeleaf.check-template=truespring.thymeleaf.check-template-location=truespring.thymeleaf.content-type=text/htmlspring.thymeleaf.enabled=truespring.thymeleaf.encoding=utf-8spring.thymeleaf.mode=HTML5spring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.html
3. Write html
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><head> <title>demo</title></head><body><p>This is the first paragraph</p><p th:text="${textValue}">This is the second paragraph</p></body></html>生, test
package com.mos.easyboot.admin.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("demo")public class DemoController { @RequestMapping("index")public String index(Model model){ String textValue = "The superior man can only do it when he hears the truth; the sergeant hears the truth, if he exists or dies; the inferior man laughs at it." + "No laughter is not enough to be a Tao." + "Therefore, there are suggestions: to be clear about the way, to advance the way, to retreat; to be a barbaric way, to be a (lei); to be the best virtue, to be a valley, to be a great white like humiliation, to be a broad virtue, to be a steal, to be a real person like a flying; to be generous without a corner; to be great and not a great product; to be great and not a great sound; to be invisible." + "The Tao is hidden and not named." + "Only to the Tao, to be good at beginning and be good at accomplishing."; model.addAttribute("textValue",textValue); return "demo/demo"; }}5, page effect
Land, data rendering
VUE has a problem with SSR (server-side rendering). Although there are solutions (see my previous article "Separating Nuxt.js to Solve SEO Problems with Front and Backends", I always feel that it is better to let appropriate technologies do business. Thymeleaf is equivalent to rendering on the server. Check the source code of the page as follows:
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.