Home  >  Article  >  Java  >  SpringBoot Thymeleaf template engine example analysis

SpringBoot Thymeleaf template engine example analysis

WBOY
WBOYforward
2023-05-12 17:28:061223browse

Jsp is the earliest template technology, used to process the view layer and use it as a template for data display

SpringBoot Thymeleaf template engine example analysis

B S structure:

B: Browser: used to display data and send requests, without processing capabilities

Send a request and access a.jsp. a.jsp becomes a Servlet on the server side and returns the output data to the browser. You can see the result data in the browser. The jsp is finally translated into an html page.

Template technology, you can use them as string replacements. For example: here {data} here is a string, you put It is replaced by a fixed value and other values, but this replacement has some additional functions, processing the content of the view layer through template technology

SpringBoot Thymeleaf template engine example analysis

First example:

SpringBoot Thymeleaf template engine example analysis

pom.xml: Thymeleaf dependency:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.1
         
    
    com.bjpowernode
    027-thymeleaf-first
    0.0.1-SNAPSHOT
    
        1.8
    
    
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

Create Controller: HelloThymeleafController:

package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class HelloThymeleafController {
    @RequestMapping("/hello")
    public String helloThymeleaf(HttpServletRequest request){
        //添加数据到request作用域,模板引擎可以从request中获取数据
        request.setAttribute("data","欢迎使用Thymeleaf模板引擎");
        //指定视图 模板引擎使用的页面(html)
        //逻辑名称
        return "hello";
    }
}

templates: used to place view files used in templates , the templates used by the template engine are placed under the template directory:

Create hello.html:




    
    hello html

使用Thymeleaf的例子

想显示数据

Run the main startup class Application:

package com.bjpowernode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Can be found in hello Add to .html: Unresolved th in the tag is very popular, and there is no prompt message when writing

xmlns:th="http://www.thymeleaf.org" In the tag, write th again There will be prompts to record




    
    hello html

使用Thymeleaf的例子

想显示数据

显示

SpringBoot Thymeleaf template engine example analysis

Use Model:

In Controller:

package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class HelloThymeleafController {
    @RequestMapping("/hello")
    public String helloThymeleaf(Model model, HttpServletRequest request){
        //添加数据到request作用域,模板引擎可以从request中获取数据
        request.setAttribute("data","欢迎使用Thymeleaf模板引擎");
        //使用model和request作用域是一样的 实际上model中的数据就是放到request作用域中的
        model.addAttribute("mydata","model中的数据");
        //指定视图 模板引擎使用的页面(html)
        //逻辑名称
        return "hello";
    }
}

hello.html:




    
    hello html

使用Thymeleaf的例子

想显示数据

显示

SpringBoot Thymeleaf template engine example analysis

speingboot configuration file application.properties: Commonly used related settings of the template engine. Basically, the settings are default:

# During the development stage, turn off template sending. Cache, let the modification take effect immediately. When set to true, the template cache is used. When accessed for the second time, the data in the memory is used and the template is no longer parsed.
spring.thymeleaf.cache=false
#Encoding Format
spring.thymeleaf.encoding=UTF-8
#Type of template (the default is html, the template is an html file which not only supports web pages as templates but also supports other categories)
spring.thymeleaf.model= HTML
#Template prefix: The default is the classpath of the classpath:/templates directory
spring.thymeleaf.prefix=classpath:/templates/
#Suffix
spring.thymeleaf.suffix=.html

The above is the detailed content of SpringBoot Thymeleaf template engine example analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete