Although front-end and back-end separation is now the dominant development approach, it is important to understand the template engine. Although the default template engine for Spring-MVC is JSP, who still uses JSP after 9102 years? The official SpringBoot template engine is Thymeleaf, which also supports FreeMarker.

SpringBoot projects can be generated at start.spring. IO /, or they can be created directly from the IDE.

SpringBoot integration FreeMarker

  • The project structure

    Project │ pom. XML / / maven rely on │ ├ ─. Idea │ ├ ─. MVN │ └ ─ SRC ├ ─ the main │ ├ ─ Java │ │ └ ─ com │ │ └ ─ cisbest │ │ │ CisbestApplication. Java / / project entry │ │ │ │ │ └ ─ controller │ │ CisBestController. Java / / implementation of controller │ │ │ └ ─ resources │ │ Application. The properties / / configuration file │ │ │ ├ ─ static │ └ ─ templates │ user. The FTL / / a FreeMarker template │ └ ─ test └ ─ Java └ ─ com └ ─ cisbest CisbestApplicationTests.javaCopy the code
  • Pem. XML requires a spring-boot-starter-freemarker dependency

    
            
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.6. RELEASE</version>
            <relativePath/> <! -- lookup parent from repository -->
        </parent>
        <groupId>com.cisbest</groupId>
        <artifactId>cisbest</artifactId>
        <version>0.0.1 - the SNAPSHOT</version>
        <name>cisbest</name>
        <description>FreeMarker, Thymeleaf SpringBoot consolidation, https://www.cnblogs.com/cisbest/p/13509870.html</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </repository>
            <repository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </pluginRepository>
            <pluginRepository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    
    </project>
    Copy the code
  • CisbestApplication.java

    package com.cisbest;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class CisbestApplication {
    
        public static void main(String[] args) { SpringApplication.run(CisbestApplication.class, args); }}Copy the code
  • CisBestController.java

    package com.cisbest.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping("/f")
    public class CisBestController {
    
        / / test address http://localhost:8080/f/user? Name = little
    
        @GetMapping("/user")
        public ModelAndView view(String name){
    
            System.out.println("Data from the front end :"+name);
    
            ModelAndView mav = new ModelAndView("/user");
            mav.addObject("name",name);
            returnmav; }}Copy the code
  • application.properties

    Can a property in #HttpServletRequest override a property of the same name in the model in controller
    spring.freemarker.allow-request-override=false
    Can #HttpSession override the model attribute in controller
    spring.freemarker.allow-session-override=true
    Whether to add attributes from HttpServletRequest to the Model
    spring.freemarker.expose-request-attributes=false
    Whether to add attributes from HttpSession to Model
    spring.freemarker.expose-session-attributes=true
    The template file name extension
    spring.freemarker.suffix=.ftl
    Check whether the template location is checked
    spring.freemarker.check-template-location=true
    # the location of the template file
    spring.freemarker.template-loader-path=classpath:/templates/
    # Template file encoding
    spring.freemarker.charset=UTF-8
    spring.freemarker.content-type=text/html
    Whether to enable caching
    spring.freemarker.cache=false
    Copy the code
  • user.ftl

    <! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>user</title> </head> <body> Response data returned by the < p > > > > > > > > > > > > > > > > > ${name} < / p > < / body > < / HTML >Copy the code



SpringBoot integration Thymeleaf

  • The project structure

    Project │ pom. XML │ ├ ─. Idea │ └ ─ SRC ├ ─ the main │ ├ ─ Java │ │ └ ─ com │ │ └ ─ cisbest │ │ │ CisbestApplication. Java │ │ │ │ │ └ ─ controller │ │ CisBestController. Java │ │ │ └ ─ resources │ │ application. The properties │ │ │ ├ ─ static │ └ ─ templates │ User. HTML │ └ ─ test └ ─ Java └ ─ com └ ─ cisbest CisbestApplicationTests. JavaCopy the code
  • Pam. XML requires the spring-boot-starter-thymeleaf dependency

    
            
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.6. RELEASE</version>
            <relativePath/> <! -- lookup parent from repository -->
        </parent>
        <groupId>com.cisbest</groupId>
        <artifactId>cisbest</artifactId>
        <version>0.0.1 - the SNAPSHOT</version>
        <name>cisbest</name>
        <description>FreeMarker, Thymeleaf SpringBoot consolidation, https://www.cnblogs.com/cisbest/p/13509870.html</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </repository>
            <repository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </pluginRepository>
            <pluginRepository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    
    </project>
    Copy the code
  • CisbestApplication.java

    package com.cisbest;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class CisbestApplication {
    
        public static void main(String[] args) { SpringApplication.run(CisbestApplication.class, args); }}Copy the code
  • CisBestController.java

    package com.cisbest.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping("/t")
    public class CisBestController {
    
        / / test address http://localhost:8080/t/user? Name = the sable cicada
    
        @GetMapping("/user")
        public ModelAndView view(String name){
    
            System.out.println("Data from the front end :"+name);
    
            ModelAndView mav = new ModelAndView("user.html");
            mav.addObject("name",name);
            returnmav; }}Copy the code
  • application.properties

    # Check whether the template exists. Default is true
    spring.thymeleaf.check-template=true
    # Check whether the template location exists. Default is true
    spring.thymeleaf.check-template-location=true
    The default value is true
    spring.thymeleaf.cache=true
    # template file location
    spring.thymeleaf.prefix=classpath:/templates/
    # template file suffix
    spring.thymeleaf.suffix=.html
    # template file encoding
    spring.thymeleaf.encoding=UTF-8
    spring.thymeleaf.servlet.content-type=text/html
    Copy the code
  • user.html

    <! DOCTYPEhtml>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>user</title>
    </head>
    <body>
        <p>The returned data is >>>>>>>>>>>>>>>><span th:text="${name}"></span>   </p>
    </body>
    </html>
    Copy the code