springboot如何使用thymeleaf完成頁(yè)面緩存
使用thymeleaf完成頁(yè)面緩存
直接看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);
?
? ? ? ?// 無(wú)法導(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ù)
FreeMarkerGrooryThymeleafMustache- 等等
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主要語(yǔ)法
- 變量表達(dá)式
? ${...} ? //獲取上下文中的變量值 ?- 選擇變量表達(dá)式
? *{...} ? //用于從被選定的對(duì)象獲取屬性值- 消息表達(dá)式
? #{...} ?//用于Thymeleaf模板頁(yè)面國(guó)際化內(nèi)容的動(dòng)態(tài)替換和展示- 鏈接URL表達(dá)式
? @{...} ?//用于頁(yè)面跳轉(zhuǎn)或者資源的引入- 片段表達(dá)式
? ~{...} ?//用來(lái)標(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è)置模板緩存、模板編碼、模板樣式、指定模板頁(yè)面存放路徑、指定模板頁(yè)面名稱的后綴)
#模板緩存開(kāi)啟 spring.thymeleaf.cache=true #模板編碼 spring.thymeleaf.encoding=UTF-8 #模板樣式 spring.thymeleaf.mode=HTML5 #指定模板頁(yè)面存放路徑 spring.thymeleaf.prefix=classpath:/templates/ #指定模板頁(yè)面名稱的后綴 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)文章
微服務(wù)之間如何通過(guò)feign調(diào)用接口上傳文件
這篇文章主要介紹了微服務(wù)之間如何通過(guò)feign調(diào)用接口上傳文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Java實(shí)現(xiàn)輕松處理日期和時(shí)間的API小結(jié)
這篇文章主要為大家詳細(xì)介紹了Java中的日期和時(shí)間API,可以輕松處理日期和時(shí)間,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
MyBatis編寫(xiě)一個(gè)簡(jiǎn)單的SQL生成工具
MyBatis 是一個(gè)強(qiáng)大的數(shù)據(jù)持久化框架,它提供了一種半自動(dòng)化的 ORM 實(shí)現(xiàn)方式,本文將為大家介紹如何使用MyBatis編寫(xiě)一個(gè)簡(jiǎn)單的SQL生成工具,需要的可以了解下2025-03-03
java中List刪除時(shí)需要的注意事項(xiàng)
最近在利用java中的LIST在刪除時(shí)發(fā)現(xiàn)了一個(gè)錯(cuò)我,通過(guò)查找相關(guān)的資料終于解決了,覺(jué)著有必要分享處理給同樣遇到這個(gè)問(wèn)題的朋友參考,下面這篇文章主要介紹了java中List刪除時(shí)需要的注意事項(xiàng),需要的朋友可以一起來(lái)看看吧。2017-01-01
IDEA在SpringBoot項(xiàng)目使用Maven打包后jar包太小問(wèn)題及解決
這篇文章主要介紹了IDEA在SpringBoot項(xiàng)目使用Maven打包后jar包太小問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
java實(shí)現(xiàn)優(yōu)酷視頻地址解析示例代碼分享
最近做了一個(gè)在線視頻的下載器,需要解析youku的視頻,獲得真正的視頻地址,現(xiàn)在把解析過(guò)程記錄下來(lái)以供參考2014-01-01
spring?boot配置dubbo方式(properties)
這篇文章主要介紹了spring?boot配置dubbo方式(properties),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Mybatis-plus數(shù)據(jù)權(quán)限D(zhuǎn)ataPermissionInterceptor實(shí)現(xiàn)
本文主要介紹了Mybatis-plus數(shù)據(jù)權(quán)限D(zhuǎn)ataPermissionInterceptor實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

