SpringBoot 2.x (8):模板引擎

SpringBoot中有很多的starter:本质是多个JAR包集合

比如我们常用的:

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

其实它包含的内容有:

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.1.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>2.1.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.1.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate.validator</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>6.0.16.Final</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.1.6.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.1.6.RELEASE</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

而每个依赖之下又包含有很多的JAR包,这里就不继续列举了

所以如果我们要用到的模板引擎不必去考虑需要什么JAR包

直接导入相对应的starter即可

模板引擎:

通常我们需要的是动态页面,动态页面就需要进行渲染

早期的项目使用JSP作为模板引擎,然后JSP是在后端进行,效率较低

JSP的优点:支持JSTL、El等方式,甚至可以直接写Java代码

JSP的缺点:效率问题,Undertow容器不支持,SpringBoot官网不推荐

Freemarker:严重依赖MVC模式,不依赖Servlet,不占用JVM

依赖:

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

配置:

#Freemarker配置
#本地测试不推荐使用缓存
spring.freemarker.cache=false
#编码
spring.freemarker.charset=UTF-8
#允许请求重写
spring.freemarker.allow-request-override=false
#进行路径检查
spring.freemarker.check-template-location=true
#格式为HTML
spring.freemarker.content-type=text/html
#暴漏request属性
spring.freemarker.expose-request-attributes=true
#暴漏session属性
spring.freemarker.expose-session-attributes=true
#文件后缀
spring.freemarker.suffix=.ftl
#路径
spring.freemarker.template-loader-path=classpath:/templates/

在templates下新建一个目录fm,新建一个ftl文件:index.ftl:

<!DOCTYPE html>
<html>
<head>
<title>Freemarker</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
Freemarker
</body>
</html>

注意:虽然页面在fm目录下,但配置的最后一项不可以写成classpath:/templates/fm

Controller:

package org.dreamtech.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {
    @RequestMapping("/hello")
    private String index() {
        return "fm/index";
    }
}

访问localhost:8080/freemarker/hello即可看到index.ftl页面

如果需要动态渲染呢?

package org.dreamtech.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {
    @RequestMapping("/hello")
    private String index(ModelMap modelMap) {
        modelMap.addAttribute("user", new User("admin", "password"));
        return "fm/index";
    }
}
package org.dreamtech.springboot.controller;

public class User {
    private String username;
    private String password;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
<!DOCTYPE html>
<html>
<head>
<title>Freemarker</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
Freemarker
Username:${user.username}
Password:${user.password}
</body>
</html>

Freemarker还有if else语法,循环语法等等,可以自行查找,这里就不介绍 

Thymeleaf:SpringBoot官方推荐,适用于通常的项目,不适用于逻辑过于复杂的项目

直接以html作为文件结尾

依赖:

        <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.suffix=.html
#编码
spring.thymeleaf.encoding=UTF-8

页面:

<!DOCTYPE html>
<html>
<head>
<title>Thymeleaf</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    Thymeleaf 
    <p>Username:</p>
    <h3 th:text="${user.username}" />
    <p>Password:</p>
    <h3 th:text="${user.password}" />
</body>
</html>

Controller:

package org.dreamtech.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
    @RequestMapping("/index")
    private String index(ModelMap modelMap) {
        modelMap.addAttribute("user", new User("admin", "password"));
        return "tl/index";
    }
}

同样地,Thymeleaf还有很多其他语法,可以自行查找

实际开发中通常是前后端分离(Ajax

如果不是前后端分离,那么推荐使用:Thymeleaf

原文地址:https://www.cnblogs.com/xuyiqing/p/10831241.html