Thymeleaf说白了,为了解决访问量过大,解决抗并发,为一些无需经常去数据库查询的东西生成 纯html静态的页面,如:登录/注册/商品详情
需要的依赖过多,openfeign,阿里的json等等,在报错过程中,可自行推算需要什么依赖,文章仅供参考
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1.在resources新建templates文件夹,里面新建一个item用于存放生成的静态页面 2.item里面不需要放任何东西,外层的item.html系统会根据他生成页面,可以称为板子
```
路径要换成你的路径 这个变量一会要获取一个值,他的作用是:告诉系统生成的静态文件放哪里
pagePath: C:\Users\yy\Desktop\Th\th_test\src\main\resources\templates\item
1.将需要放在页面的数据,封装成Map类型,然后将方法放入FeignClient里面
FeignClient里面
2.新建一个Service接口,放入两条方法 crehtml:生成html用的,我的crehtml是根据id查出数据 mp:根据id查的数据,存入Map里面
void crehtml(String skuid) throws Exception;
Map<String,Object> mp(String skuid);
3.新建一个impl类,继承刚才创建的类 具体说明看注释
package com.zb.service.impl;
import com.alibaba.fastjson.JSON;
import com.zb.client.TbSkuClient;
import com.zb.service.TyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import java.io.File;
import java.io.PrintWriter;
import java.util.Map;
@Service
public class Ty implements TyService {
//生成页面必须用
@Autowired
private TemplateEngine template;
//自已FeignClient
@Autowired
private TbSkuClient fff;
//获取yml写好的静态页面存储的地址
@Value("${pagePath}")
private String pagePath;
@Override
public Map<String, Object> mp(String skuid) {
//如果你的数据库没有json数据类型,只需要写下面一行即可
Map<String, Object> okinfo = fff.okinfo(skuid);
//我的图片在数据库存储是json类型
//所以需要获取父节点-子节点,然后转换一下,然后在存入okinfo(上面代码)的Map里面
Map<String, Object> sku = (Map<String, Object>) okinfo.get("sku");
String images = sku.get("images").toString();
Map map666 = JSON.parseObject(images, Map.class);
okinfo.put("map666",map666);
return okinfo;
}
@Override
public void crehtml(String skuid) throws Exception {
Context cc = new Context();
//将查询的方法放入程序上下文
cc.setVariables(this.mp(skuid));
File file = new File(pagePath);
//判断文件夹是否存在
if(!file.exists()){
file.mkdirs();
}
//设置生成静态文件的格式,我的是传入的id+.html
File ff2 = new File(file,skuid+".html");
//数字成静态文件的编码
PrintWriter printWriter = new PrintWriter(ff2,"UTF-8");
//执行操作
template.process("item",cc,printWriter);
}
}
package com.zb;
import com.zb.service.TyService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
@EnableFeignClients
public class GoodsApplication {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext run = SpringApplication.run(GoodsApplication.class, args);
TyService pageService = run.getBean(TyService.class);
//调取创建html方法,传入id进行生成,因为生成的名字是id+.html
//程序一启动就会在resources/templates/item/里面生成静态文件
pageService.crehtml("100000003145");
}
}
具体使用方法可以去网上找下 这里提供一个地址:https://www.cnblogs.com/jnba/p/10832878.html
1.只需要在板子的html里面填写相对应的变量,即可
2.变量值是封装成Map类型返回json数据里面的一些东西
恭喜你,一切都已完成