springboot如何使用thymeleaf完成頁面緩存
更新時(shí)間:2022年06月10日 16:05:12 作者:武大大不吃糖
這篇文章主要介紹了springboot如何使用thymeleaf完成頁面緩存,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
使用thymeleaf完成頁面緩存
直接看Demo
注入redisservice以及其余兩個(gè)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模板引擎手動(dòng)渲染視圖 ? ? ? ? List<MiaoshaGoods> goodsList = miaoshaGoodsService.selectAllMiaoshaGoods(); ? ? ? ? model.addAttribute("user",user); ? ? ? ? model.addAttribute("goodsList",goodsList); ? ? ? ? ?// 無法導(dǎo)入SpringWebContext的包 ? ? ? ? SpringWebContext context = new SpringWebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap(),applicationContext); ? ? ? ? String html = thymeleafViewResolver.getTemplateEngine().process("goods_list",context); ? ? ? ? ? //3.將手動(dòng)渲染后的html存入redis緩存 ? ? ? ? if(StringUtils.isNotEmpty(html)){ ? ? ? ? ? ? redisService.set("goosList",html); ? ? ? ? } ? ? ? ? ? return html;? ? ? }
核心點(diǎn)是
SpringWebContext context = new SpringWebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap(),applicationContext); String html = thymeleafViewResolver.getTemplateEngine().process("goods_list",context);
thymeleaf簡(jiǎn)要基礎(chǔ)知識(shí)
SpringBoot支持的視圖技術(shù)之Thymeleaf
1.SpringBoot可整合的模板引擎技術(shù)
FreeMarker
Groory
Thymeleaf
Mustache
- 等等
2.Thymeleaf常用標(biāo)簽(示例代碼)
<!DOCTYPE html> <html lang = "en" xmlns:th="http://www.thymeleaf.org"> #引入thymeleaf標(biāo)簽 <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" /> #引入外聯(lián)樣式文件 <title>Title</title> </head> <body> <p th:text="#{hello}">Hello world</p> </body> </html>
3.Thymeleaf主要語法
- 變量表達(dá)式
? ${...} ? //獲取上下文中的變量值 ?
- 選擇變量表達(dá)式
? *{...} ? //用于從被選定的對(duì)象獲取屬性值
- 消息表達(dá)式
? #{...} ?//用于Thymeleaf模板頁面國際化內(nèi)容的動(dòng)態(tài)替換和展示
- 鏈接URL表達(dá)式
? @{...} ?//用于頁面跳轉(zhuǎn)或者資源的引入
- 片段表達(dá)式
? ~{...} ?//用來標(biāo)記一個(gè)片段模板,并根據(jù)需要移動(dòng)或傳遞給其他模板
4.Thymeleaf基本使用
- 4.1 在SpringBoot項(xiàng)目中使用Thymeleaf模板,必須保證引入Thymeleaf依賴。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf<artifactId> </dependency>
- 4.2 其次在全局配置文件中配置Thymeleaf模板的一些參數(shù)。(如設(shè)置模板緩存、模板編碼、模板樣式、指定模板頁面存放路徑、指定模板頁面名稱的后綴)
#模板緩存開啟 spring.thymeleaf.cache=true #模板編碼 spring.thymeleaf.encoding=UTF-8 #模板樣式 spring.thymeleaf.mode=HTML5 #指定模板頁面存放路徑 spring.thymeleaf.prefix=classpath:/templates/ #指定模板頁面名稱的后綴 spring.thymeleaf.suffix=.html
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- SpringBoot+thymeleaf+ajax實(shí)現(xiàn)局部刷新詳情
- SpringBoot+Thymeleaf實(shí)現(xiàn)生成PDF文檔
- 在SpringBoot中配置Thymeleaf的模板路徑方式
- SpringBoot整合Thymeleaf與FreeMarker視圖層技術(shù)
- SpringBoot超詳細(xì)講解Thymeleaf模板引擎
- SpringBoot詳細(xì)講解視圖整合引擎thymeleaf
- Springboot詳解如何整合使用Thymeleaf
- SpringBoot中使用Thymeleaf模板詳情
- SpringBoot使用thymeleaf實(shí)現(xiàn)一個(gè)前端表格方法詳解
相關(guān)文章
Java使用Jsoup解析html網(wǎng)頁的實(shí)現(xiàn)步驟
Jsoup是一個(gè)用于解析HTML文檔的Java庫,本文主要介紹了Java使用Jsoup解析html網(wǎng)頁的實(shí)現(xiàn)步驟,可以提取文本、鏈接、圖片等,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02java中l(wèi)ambda表達(dá)式簡(jiǎn)單用例
讓我們從最簡(jiǎn)單的例子開始,來學(xué)習(xí)如何對(duì)一個(gè)string列表進(jìn)行排序。我們首先使用Java 8之前的方法來實(shí)現(xiàn)2016-09-09SpringBoot使用jsr303校驗(yàn)的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot使用jsr303校驗(yàn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10String字符串拼接方法concat和+的效率對(duì)比
這篇文章主要介紹了String字符串拼接方法concat和+的效率對(duì)比,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Java并發(fā)編程之詳解CyclicBarrier線程同步
在之前的文章中已經(jīng)為大家介紹了java并發(fā)編程的工具:BlockingQueue接口,ArrayBlockingQueue,DelayQueue,LinkedBlockingQueue,PriorityBlockingQueue,SynchronousQueue,BlockingDeque接口,ConcurrentHashMap,CountDownLatch,本文為系列文章第十篇,需要的朋友可以參考下2021-06-06