springboot-9- springboot整合模板引擎Freemarker、thymeleaf

1. springboot集成Freemarker

pom添加freemarker依赖:

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

application.properteis中配置模板引擎freemarker:

#模板引擎freemarker配置
#是否开启freemarker缓存,本地为false,生产环境 建议为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.allow-request-override=false
spring.freemarker.check-template-location=true
# 类型
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
# 文件后缀, 必须是 .ftl,这个点号不能少!!!
spring.freemarker.suffix=.ftl
# 模板引擎存放路径
spring.freemarker.template-loader-path=classpath:/templates

编写controller, 这里的person是读取person.properties配置文件中的配置项,并映射成Person类的对应属性

package com.example.lchtest.springbootdemo1.controller;

import com.example.lchtest.springbootdemo1.domain.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {
    
    
    @Autowired
    private Person person;
    @GetMapping("/hello")
    public String index (ModelMap modelMap){
    
    
        System.out.println("跳转到freemarker页面,person = " + person);
        modelMap.addAttribute("person", person);
        return "fm/index";
    }
}

在src/main/resources/templates/fm路径下,创建index.ftl :
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>freemarker模板引擎</title>
</head>
<body>
<p>freemarker整合,index.ftl页面</p>
<p>controller返回页面的对象信息为:${person.name}, ${person.hobby}, ${person.age}</p>
</body>
</html>

访问测试:
在这里插入图片描述

2. springboot集成thymeleaf

spring官方推荐使用thymeleaf作为模板引擎,因为thymeleaf提供了完美的SpringMVC的支持;thymeleaf 是一个Java类库,它是一个xml/xhtml/html5的模板引擎,可以作为MVC的web应用的view层,thymeleaf 还提供了额外的模块与springMVC集成;
同样的,要使用thymeleaf ,也要加入对应的依赖:

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

thymeleaf 模板引擎配置

spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.suffix=.html

编写controller:

package com.example.lchtest.springbootdemo1.controller;

import com.example.lchtest.springbootdemo1.domain.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
    
    

    /**
     * 单独测试thymeleaf模板引擎,将配置文件thymeleaf相关配置项打开
     */
    @Autowired
    private Person person;
    @GetMapping("/hello")
    public String index (ModelMap modelMap){
    
    
        System.out.println("跳转到templates/tl/info.html页面,person = " + person);
        modelMap.addAttribute("person", person);
        return "tl/info";
    }
}

模板引擎文件:默认放在classpath:/templates路径下; 使用 ${} 访问接口返回的model中的属性

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>thymeleaf模板引擎</title>
</head>
<body>
<p th:text="'controller返回信息为:' + ${person.name}"></p>
</body>
</html>

测试:
在这里插入图片描述

3.springboot集成thymeleaf原理

在spring-boot-autoconfigure-2.2.2.RELEASE.jar中,可以看到,springboot对thymeleaf已经做了默认配置,
(1)ThymeleafProperties 对thymeleaf的常用属性做了默认配置:
在这里插入图片描述
(2)ThymeleafAutoConfiguration 对thymeleaf 需要使用到的一些Bean做了自动装配:
在这里插入图片描述
(3)ThymeleafTemplateAvailabilityProvider 这个辅助类


package org.springframework.boot.autoconfigure.thymeleaf;

import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ClassUtils;

public class ThymeleafTemplateAvailabilityProvider implements TemplateAvailabilityProvider {
    
    
    public ThymeleafTemplateAvailabilityProvider() {
    
    
    }

    public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader, ResourceLoader resourceLoader) {
    
    
        if (ClassUtils.isPresent("org.thymeleaf.spring5.SpringTemplateEngine", classLoader)) {
    
    
            String prefix = environment.getProperty("spring.thymeleaf.prefix", "classpath:/templates/");
            String suffix = environment.getProperty("spring.thymeleaf.suffix", ".html");
            return resourceLoader.getResource(prefix + view + suffix).exists();
        } else {
    
    
            return false;
        }
    }
}

这个类的逻辑很简单,判断如果SpringTemplateEngine这个类能够被classLoader加载到,那么就尝试从classpath路径下的templates 中读取html格式的视图文件,如果读取到了,返回true,否则返回false, 这个返回结果,会在ThymeleafAutoConfiguration 中使用

猜你喜欢

转载自blog.csdn.net/weixin_41300437/article/details/107746874