springboot如何使用thymeleaf完成页面缓存

目录
  • 使用thymeleaf完成页面缓存
    • 直接看Demo
    • 控制层
    • 核心点是
  • thymeleaf简要基础知识
    • 1.SpringBoot可整合的模板引擎技术
    • 2.Thymeleaf常用标签(示例代码)
    • 3.Thymeleaf主要语法
    • 4.Thymeleaf基本使用

使用thymeleaf完成页面缓存

直接看Demo

注入redisservice以及其余两个bean.

 @Autowired
    private RedisService redisService;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
    @Autowired
    private WebApplicationContext applicationContext;

控制层

 @RequestMapping(value="/list",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String showGoods(Model model, MiaoshaUser user, HttpServletRequest request, HttpServletResponse response){
 
        //1.从redis缓存中查询
        String listHtml = redisService.get("goosList",String.class);
        if(StringUtils.isNotEmpty(listHtml)){
            return  listHtml;
        } 
 
        //2.使用thymeleaf模板引擎手动渲染视图
        List<MiaoshaGoods> goodsList = miaoshaGoodsService.selectAllMiaoshaGoods();
        model.addAttribute("user",user);
        model.addAttribute("goodsList",goodsList);
 
       // 无法导入SpringWebContext的包
        SpringWebContext context = new SpringWebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap(),applicationContext);
        String html = thymeleafViewResolver.getTemplateEngine().process("goods_list",context);
 
        //3.将手动渲染后的html存入redis缓存
        if(StringUtils.isNotEmpty(html)){
            redisService.set("goosList",html);
        }
 
        return html; 
    }

核心点是

SpringWebContext context = new SpringWebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap(),applicationContext);
String html = thymeleafViewResolver.getTemplateEngine().process("goods_list",context);

thymeleaf简要基础知识

SpringBoot支持的视图技术之Thymeleaf

1.SpringBoot可整合的模板引擎技术

  • FreeMarker
  • Groory
  • Thymeleaf
  • Mustache
  • 等等

2.Thymeleaf常用标签(示例代码)

<!DOCTYPE html>
<html lang = "en" xmlns:th="http://www.thymeleaf.org">  #引入thymeleaf标签
<head>
    <meta charset = "UTF-8">
    <meta name = "viewport" content = "width = device - width, initial - scale = 1.0">
    <meta http-equiv = "X-UA-Compatible" content = "ie-edge">
    <link rel="stylesheet" type="text/css" media="all" href="../../css/gtvg.css" rel="external nofollow"  th:href="@{/css/gtvg.css}" rel="external nofollow" />  #引入外联样式文件
    <title>Title</title>
</head>
<body>
     <p th:text="#{hello}">Hello world</p>
</body>
</html>

3.Thymeleaf主要语法

  • 变量表达式
  ${...}   //获取上下文中的变量值  
  • 选择变量表达式
  *{...}   //用于从被选定的对象获取属性值
  • 消息表达式
  #{...}  //用于Thymeleaf模板页面国际化内容的动态替换和展示
  • 链接URL表达式
  @{...}  //用于页面跳转或者资源的引入
  • 片段表达式
  ~{...}  //用来标记一个片段模板,并根据需要移动或传递给其他模板

4.Thymeleaf基本使用

  • 4.1 在SpringBoot项目中使用Thymeleaf模板,必须保证引入Thymeleaf依赖。
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf<artifactId>
</dependency>
  • 4.2 其次在全局配置文件中配置Thymeleaf模板的一些参数。(如设置模板缓存、模板编码、模板样式、指定模板页面存放路径、指定模板页面名称的后缀)
#模板缓存开启
spring.thymeleaf.cache=true
#模板编码
spring.thymeleaf.encoding=UTF-8
#模板样式
spring.thymeleaf.mode=HTML5
#指定模板页面存放路径
spring.thymeleaf.prefix=classpath:/templates/
#指定模板页面名称的后缀
spring.thymeleaf.suffix=.html

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • SpringBoot+Hutool+thymeleaf完成导出Excel的实现方法

    目录 1.引入依赖 2.创建实体类 3.创建导出接口 4.创建html 5.测试导出 导出Excel的框架有很多种,POI相对来说比较老了,很多Excel框架底层都是POI.有EasyPoi.EasyExcel.包括Hutool当中封装的也是POI.唯一不同的是Hutool工具包不局限与做Excel.他里面封装了大量的util,一般现在开发都会用到糊涂. 本篇示例当中不仅仅有后端,而且还提供了前端html,html当中利用js将后端 输出流直接下载为文件. 实现的效果如下:一点击导出文件直接下载

  • SpringBoot + thymeleaf 实现读取视频列表并播放视频功能

    目录 效果 实现过程 后端程序示例 前端程序示例 通过读取数据库video表获取当前视频的视频名.视频地址,展示至前端页面videorecord.html,通过点击播放按钮获取数据id进而得到所选视频地址,跳转播放视频显示页videoshow.html,播放所选视频.当然本案例只是为了展示主要的一些功能,其他比如跳转.页面布局美化等可以自行进行更改. 效果 Springboot播放视频 实现过程 后端程序示例 1. Controller层示例 返回数据库数据时,使用了pagehelp当中的Pag

  • springboot+thymeleaf整合阿里云OOS对象存储图片的实现

    目录 1.先引入pom依赖 2.编写前端thymleeaf代码tetsfile.html 3.service层编写 4.controller层编写 今天再进行创建项目时想使用阿里云oos进行存储图片 下面进行实操 1.先引入pom依赖 <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.9.1

  • SpringBoot中使用Thymeleaf模板详情

    目录 一.什么是Thymeleaf 二.SpringBoot中使用Thymeleaf模板 1.pom.xml中添加thymeleaf依赖 2.关闭thymeleaf缓存 3.创建thymeleaf模板页面 4.创建一个类(用于与上述html页面交互) 5.访问服务路径 一.什么是Thymeleaf 官网原话:Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本. Thymeleaf的主要目标是提供一种优雅且高度

  • SpringBoot整合Thymeleaf小项目及详细流程

    目录 1.项目简绍 2.设计流程 3.项目展示 4.主要代码 1.验证码的生成 2.userController的控制层设计 3.employeeController控制层的代码 4.前端控制配置类 5.过滤器 6.yml配置 7.文章添加页面展示 8.POM.xml配置类 9.employeeMapper配置类 10.UserMapper配置类 1.项目简绍 本项目使用SpringBoot开发,jdbc5.1.48 Mybatis 源码可下载 其中涉及功能有:Mybatis的使用,Thymel

  • SpringBoot+thymeleaf+Echarts+Mysql 实现数据可视化读取的示例

    目录 实现过程 1. pom.xml 2. 后端程序示例 3. 前端程序示例 通过从数据库获取数据转为JSON数据,返回前端界面实现数据可视化. 数据可视化测试 实现过程 1. pom.xml pom.xml引入(仅为本文示例需要,其他依赖自行导入) <!--Thymeleaf整合security--> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymele

  • springboot如何使用thymeleaf完成页面缓存

    目录 使用thymeleaf完成页面缓存 直接看Demo 控制层 核心点是 thymeleaf简要基础知识 1.SpringBoot可整合的模板引擎技术 2.Thymeleaf常用标签(示例代码) 3.Thymeleaf主要语法 4.Thymeleaf基本使用 使用thymeleaf完成页面缓存 直接看Demo 注入redisservice以及其余两个bean. @Autowired     private RedisService redisService;     @Autowired  

  • springboot如何使用thymeleaf模板访问html页面

    引言 在传统的web开发中通常使用jsp页面,首先需要在pom文件中引入springmvc相关的包,然后写springmvc的配置文件(包括访问资源的路径解析),之后还需再web.xml中配置访问路由.这无疑太麻烦了,每次开发前都需要编写大量的配置文件. springboot为此提供了高效便捷的解决方案,只需再pom.xml中添加web开发的依赖,便可进行web开发,省去了繁琐的配置步骤. 下面为web开发引入的依赖 <dependency> <groupId>org.spring

  • 解决Springboot项目打包后的页面丢失问题(thymeleaf报错)

    目录 Springboot项目打包后的页面丢失 遇到的问题目前找到两种 Springboot打包ThymeLeaf报错 原因 解决办法 Springboot项目打包后的页面丢失 遇到的问题目前找到两种 返回视图路径以/开头,例如 /test/hello 在thymeleaf页面中,引入的页面以/开头,例如:<footer th:replace="/index::footer"></footer> 代码书写规范: @GetMapping("/about-

  • SpringBoot中的Thymeleaf用法

    Thymeleaf Thymeleaf是最近SpringBoot推荐支持的模板框架,官网在thymeleaf.org这里. 我们为什么要用Thymeleaf来作为模板引擎呢?官网给了我们一个非常令人信服的解释: Thymeleaf is a modern server-side Java template engine for both web and standalone environments.> 基本写法就像下面这样: <table> <thead> <tr&g

  • 浅析SpringBoot中使用thymeleaf找不到.HTML文件的原因

    thymeleaf是目前最新的模板引擎,它和springboot完美搭配,让前后端不在疏远 首先先介绍一下市面上出现过的模板引擎 1, Verlocity 距今有10多年之久,是由Apache推出的 Struts2 底层使用2,Freemarker,距今有10多年之久, 第三方小公司推出,后来得到了Apache的认可,在Struts2这个框架中, 底层使用Freemarker. SpringBoot 默认使用Freemarker是以.ftl结尾的文件. 3,Thymeleaf是以.html结尾的

  • SpringBoot定制三种错误页面及错误数据方法示例

    目录 定制错误页面 自定义 error.html 自定义动态错误页面 自定义静态错误页面 定制错误数据 1. 自定义异常处理类 2. 自定义错误属性处理工具 我们知道 Spring Boot 已经提供了一套默认的异常处理机制,但是 Spring Boot 提供的默认异常处理机制却并不一定适合我们实际的业务场景,因此,我们通常会根据自身的需要对 Spring Boot 全局异常进行统一定制,例如定制错误页面,定制错误数据等. 定制错误页面 我们可以通过以下 3 种方式定制 Spring Boot

  • springboot学习之Thymeleaf模板引擎及原理介绍

    目录 模板引擎 什么是模板引擎? 模板引擎的原理 引入Thymeleaf Thymeleaf分析 Thymeleaf语法学习 模板引擎 springboot我们目前是以jar包的形式打包,实际上我们之前是打成war包,放到tomcat服务器里面,可以用JSP.但是jar包就导致不能用JSP,换一种方式就是springboot推荐的Thymeleaf模板引擎(JSP也是一种模板引擎,除此之外还有什么framework也是一种模板引擎), 什么是模板引擎? 模板引擎就是解决我们需要动态赋值给前端的一

  • SpringBoot结合Ajax实现登录页面实例

    目录 一. Ajax 1.1 Ajax介绍 1.2 异步的作用 二.SpringBoot应用Ajax 2.1 开发配置 2.2 创建user表 2.3 MVC三层架构 2.4 前端测试页面 2.5 测试结果 总结 一. Ajax 1.1 Ajax介绍 AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. AJAX = 异步 JavaScript 和 XML AJAX 是一种用于创建快速动态网页的技术.通过在后台与服务器进行少量数据交换,可以使网页实现异步更新.这意味着可以在不

  • 在SpringBoot中配置Thymeleaf的模板路径方式

    目录 配置Thymeleaf的模板路径 关于thymeleaf配置说明 配置Thymeleaf的模板路径 众所周知,Thymeleaf的模板文件默认是在项目文件夹的src\main\resources\templates目录下的. 不过出于特殊需要,要修改其路径怎么办呢? 在我们的项目配置文件application.properties中,添加如下配置: #Thymeleaf配置 spring.thymeleaf.prefix=自定义的Thymeleaf的模板位置,jar内部以classpath

随机推荐