SpringBoot整合FreeMarker、Thymeleaf

虽然现在前后端分离的开发方式是主流,但还是有必要了解下模板引擎。虽然spring-mvc的默认模板引擎是JSP,都9102年了,谁还用JSP啊。SpringBoot官方推荐的模板引擎是Thymeleaf,也支持FreeMarker。

可以在 https://start.spring.io/ 网站上生成SpringBoot项目,当然也可以直接用IDE创建。

SpringBoot 整合 FreeMarker

  • 项目结构

        project
        │  pom.xml     //maven依赖
        │  
        ├─.idea
        │          
        ├─.mvn
        │          
        └─src
            ├─main
            │  ├─java
            │  │  └─com
            │  │      └─cisbest
            │  │          │  CisbestApplication.java    //项目入口
            │  │          │  
            │  │          └─controller
            │  │                  CisBestController.java   //实现的controller
            │  │                  
            │  └─resources
            │      │  application.properties   //配置文件
            │      │  
            │      ├─static
            │      └─templates
            │              user.ftl   //FreeMarker模板
            │              
            └─test
                └─java
                    └─com
                        └─cisbest
                                CisbestApplicationTests.java
    
  • pom.xml 需要添加 spring-boot-starter-freemarker 依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <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-SNAPSHOT</version>
        <name>cisbest</name>
        <description>SpringBoot整合FreeMarker、Thymeleaf,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>
    
  • 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);
        }
    }
    
  • 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 {
    
        // 测试地址  http://localhost:8080/f/user?name=小乔
    
        @GetMapping("/user")
        public ModelAndView view(String name){
    
            System.out.println("从前端传过来的数据:"+name);
    
            ModelAndView mav = new ModelAndView("/user");
            mav.addObject("name",name);
            return mav;
        }
    }
    
  • application.properties

    #HttpServletRequest中的属性能否覆盖controller中model的同名属性
    spring.freemarker.allow-request-override=false
    #HttpSession的属性能否覆盖controller中model的同名属性
    spring.freemarker.allow-session-override=true
    #是否将HttpServletRequest中的属性添加到Model中
    spring.freemarker.expose-request-attributes=false
    #是否将HttpSession中的属性添加到Model中
    spring.freemarker.expose-session-attributes=true
    
    #模板文件后缀名
    spring.freemarker.suffix=.ftl
    #是否检查模板位置
    spring.freemarker.check-template-location=true
    #模板文件所在的位置
    spring.freemarker.template-loader-path=classpath:/templates/
    
    #模板文件的编码
    spring.freemarker.charset=UTF-8
    spring.freemarker.content-type=text/html
            
    #是否开启缓存
    spring.freemarker.cache=false
    
  • user.ftl

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>user</title>
    </head>
    <body>
        <p>响应返回的数据>>>>>>>>>>>>>>>>   ${name} </p>
    </body>
    </html>
    


SpringBoot 整合 Thymeleaf

  • 项目结构

    project
    │  pom.xml
    │  
    ├─.idea
    │          
    └─src
        ├─main
        │  ├─java
        │  │  └─com
        │  │      └─cisbest
        │  │          │  CisbestApplication.java
        │  │          │  
        │  │          └─controller
        │  │                  CisBestController.java
        │  │                  
        │  └─resources
        │      │  application.properties
        │      │  
        │      ├─static
        │      └─templates
        │              user.html
        │              
        └─test
            └─java
                └─com
                    └─cisbest
                            CisbestApplicationTests.java
    
  • pom.xml 需要添加 spring-boot-starter-thymeleaf 依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <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-SNAPSHOT</version>
        <name>cisbest</name>
        <description>SpringBoot整合FreeMarker、Thymeleaf,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>
    
  • 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);
        }
    }
    
  • 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 {
    
        // 测试地址  http://localhost:8080/t/user?name=貂蝉
    
        @GetMapping("/user")
        public ModelAndView view(String name){
    
            System.out.println("从前端传过来的数据:"+name);
    
            ModelAndView mav = new ModelAndView("user.html");
            mav.addObject("name",name);
            return mav;
        }
    }
    
  • application.properties

    #是否检查模板是否存在,默认为true
    spring.thymeleaf.check-template=true
    #是否检查模板位置是否存在,默认为true
    spring.thymeleaf.check-template-location=true
    
    #是否开启缓存默认为true
    spring.thymeleaf.cache=true
    
    #模板文件位置
    spring.thymeleaf.prefix=classpath:/templates/
    #模板文件后缀
    spring.thymeleaf.suffix=.html
    
    #模板文件编码
    spring.thymeleaf.encoding=UTF-8
    spring.thymeleaf.servlet.content-type=text/html
    
  • user.html

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>user</title>
    </head>
    <body>
        <p>响应返回的数据>>>>>>>>>>>>>>>>    <span th:text="${name}"></span>   </p>
    </body>
    </html>
    
posted @ 2019-08-16 20:59  氵灬  阅读(135)  评论(0编辑  收藏  举报