SpringBoot2.x(七)常用Starter介绍和整合模板引擎Freemarker、Thymeleaf

SpringBoot Starter

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.

starter是一个简洁的依赖描述,你可以通过它一步引入相关技术的一系列依赖而不必每次去找这样的一系列依赖复制粘贴到你的项目里。

  • 几个常用的starter
    • spring-boot-starter-activemq
    • spring-boot-starter-aop
    • spring-boot-starter-data-redis
    • spring-boot-starter-freemarker
    • spring-boot-starter-thymeleaf
    • spring-boot-starter-webflux

常见模板引擎和官方推荐

JSP(后端渲染,消耗性能)

Java Server Pages 动态网页技术,由应用服务器中的JSP引擎来编译和执行,再将生成的整个页面返回给客户端

优点

  • 可以写java代码
  • 支持表达式语言(eljstl)、内建函数
  • javaweb官方推荐

缺点

Freemarker

FreeMarker Template Language(FTL) 文件一般保存为 xxx.ftl

  • 严格依赖MVC模式,不依赖Servlet容器(不占用JVM内存)
  • 支持内建函数

Thymeleaf (springboot官方推荐)

  • *轻量级**的模板引擎(负责逻辑业务的不推荐,解析DOM或者XML会占用多的内存)
  • 可以直接在浏览器中打开且正确显示模板页面
  • 直接是html结尾,直接编辑

SpringBoot整合Freemarker

1.   引入 starter-freemarker依赖

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

 

2.   在application.properties中添加SpringBoot整合Freemarker属性配置

#是否开启thymeleaf缓存,本地开发为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

#模板文件后缀
spring.freemarker.suffix=.ftl
#模板存放路径
spring.freemarker.template-loader-path=classpath:/templates/ftl

 

 

3.   在 templates下新建 ftl文件,文件格式为 html,如 templates/ftl/index.ftl

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hello freemarker
</body>
</html>

 

4.   在 Controller中返回模板视图

package top.zhenganwen.springbootweb.controller;

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

@Controller
@RequestMapping("freemarker")
public class FreemarkerController {

    @RequestMapping("hello")
    public String hello() {
        return "index";
    }
}

 

5.   访问 localhost:8080/freemarker/hello

6.   向 ftl中添加 DataModel

 

 

    1)   准备数据,这里以读取配置文件(classpath:resources.properties)为例

server.name=spirngboot
server.domain=localhost:8080
package top.zhenganwen.springbootweb.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Data		//lombok插件
@Component
@PropertySource("classpath:resource.properties")
@ConfigurationProperties(prefix = "server")
public class ServerSetttings {

    //服务器名称
    private String name;
    //接口域名
    private String domain;

}

 

  2)   在 action中注入 Model保存数据集

package top.zhenganwen.springbootweb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import top.zhenganwen.springbootweb.properties.ServerSetttings;

@Controller
@RequestMapping("freemarker")
public class FreemarkerController {

    @Autowired
    private ServerSetttings serverSetttings;

    @RequestMapping("hello")
    public String hello(Model model) {
        model.addAttribute("server", serverSetttings);
        return "index";
    }
}

  3)  在 ftl中将 ModelMap中的数据集渲染到页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hello freemarker
server.name:${server.name}
server.domain:${server.domain}
</body>
</html>

SpringBoot整合Thymeleaf

1.   引入 starter-thymeleaf

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

 

2.  在 application.properties中添加整合配置,建议注释掉上一节中freemarker的配置

#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
#前缀
spring.thymeleaf.prefix=classpath:/templates/thymeleaf/
#编码
spring.thymeleaf.encoding=UTF-8
#类型
spring.thymeleaf.content-type=text/html
#模板文件名称的后缀
spring.thymeleaf.suffix=.html

 

 

3.   创建模板页面 templates/thymeleaf/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hello thymeleaf
</body>
</html>

 

 

4.   添加 action

package top.zhenganwen.springbootweb.controller;

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

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

    @RequestMapping("hello")
    public String hello() {
        return "index";
    }
}

 

 

无法解析视图名的常见原因:SpringBoot是通过 spring.thymeleaf.prefix+视图名+ spring.thymeleaf.suffix来查找文件的,如果你的classpath:/templates/thymeleaf/写成了classpath:/templates/thymeleaf,则查找的文件就变成了 /templates/thymeleafindex.html,自然就找不到了

5.    添加数据模型

 

 

  1)同样的你可以通过 Model将数据模型填充到视图中

@Controller
@RequestMapping("freemarker")
public class FreemarkerController {

    @Autowired
    private ServerSetttings serverSetttings;

    @RequestMapping("hello")
    public String hello(Model model) {
        model.addAttribute("server", serverSetttings);
        return "index";
    }
}

    2)通过thymeleaf驱动将数据渲染到视图

<!DOCTYPE html>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hello thymeleaf
<h1>这里没有用Thymeleaf驱动</h1>
<h1 th:text="${server.name}">这里使用thymeleaf驱动</h1>
</body>
</html>

   3)访问 http://localhost:8080/thymeleaf/hello ,响应结果如下

hello thymeleaf
这里没有用Thymeleaf驱动
spirngboot

 

6.     直接访问静态页面

 

   1)值得注意的是 thymeleaf模板页面是可以直接访问的(不经过controller)。但这要求你将模板文件的路径添加到springboot默认加载的静态路径中:

spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/templates/

    注意使用英文逗号分隔

    2)访问 http://localhost:8080/thymeleaf/index.html ,结果如下:

hello thymeleaf
这里没有用Thymeleaf驱动
这里使用thymeleaf驱动

可以发现直接访问 thymeleaf模板就会看到模板的原始内容。但因为没有经过controller,所以不会有动态数据渲染。

${}表达式只能写在 th表达式内

thymeleaf快速入门:官方教程

 

点击白玉搜一搜搜索

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值