文章目录
- 常用的模板引擎:
- Thymeleaf实现静态化用法
- Thymeleaf具体用法
- 使用rabbitmq实现修改 删除静态页面
常用的模板引擎:
- Freemarker
- Velocity
- Thymeleaf
Thymeleaf实现静态化用法
- Context:运行上下文 (保存数据)
- TemplateResolver:模板解析器(用来读取模板相关的配置,例如:模板存放的位置信息,模板文件名称,模板文件的类型等等)
- TemplateEngine:模板引擎 (模板)
templateEngine.process(“模板名”, context(数据), writer(输出目的地的流));
Thymeleaf具体用法
- 在application中添加配置
ly:
thymeleaf:
destPath: D:/nginx-1.14.2/html/item #本地的静态文件保存地址 真实情况下地址在liunx文件夹下
- 在resources–》templates–>引入静态文件
- 编写代码
我的controller
@Autowired //正常实现类
private GoodsServiceImpl goodsService;
@Autowired //静态页面生成类
private FileService fileService;
@GetMapping(value = "{id}.html",produces = "text/html")
public String toItemPage(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable("id")Long id) throws InterruptedException, ExecutionException {
//加载数据 调用实现类 返回map形式的数据
Map<String, Object> modelMap = this.goodsService.loadModel(id);
//把数据放入模型中 在前端页面直接用
model.addAllAttributes(modelMap);
//页面静态化
if(!fileService.exists(id)){//判断D:/nginx-1.14.2/html/item/11.html 有没有这个文件
//当没有这个文件的时候 就创建这个静态文件118.html
this.fileService.syncCreateHtml(id);
}
return "item";
}
我的实现类(fileService) 创建静态页面
@Service
public class FileService {
@Autowired
private GoodsServiceImpl pageService;
@Autowired //注入模板类
private TemplateEngine templateEngine;
@Value("${ly.thymeleaf.destPath}") //注入配置文件的值
private String destPath;//D:/nginx-1.14.2/html/item/
public boolean exists(Long spuId) {//118
File file = new File(destPath);
if(!file.exists()){
file.mkdirs();//如果不存在D:/nginx-1.14.2/html/item/ 文件夹,就创建
}
File f=new File(file,spuId+".html");//D:/nginx1.14.2/html/item/118.html
return f.exists();// 判断这个文件夹下有没有这个文件 返回false
}
public void syncCreateHtml(Long spuId) {
createHtml(spuId);
}
//创建静态的spuid.html 118.html
public void createHtml(Long spuId) {
//创建上下文
Context context = new Context();
//调用loadModel方法 获取页面所需要的数据 并将数据放入上下文中
context.setVariables(pageService.loadModel(spuId));
File file = new File(destPath, spuId + ".html");
try {
//准备输出流
PrintWriter printWriter = new PrintWriter(file, "utf-8");
//输出文件 页面名 数据 输出流 把这个文件输出到指定地方
templateEngine.process("item",context,printWriter);
} catch (FileNotFoundException e) {e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
生成静态页面的重点方法:
context.setVariables()将数据放进去
templateEngine.process("文件名”,context,输出目的地的流)
- 配置 nginx代理静态页面
- 将生成的静态文件手动拷贝到 nginx/html/item文件夹下
- 配置nginx.conf文件
listen 80;
server_name www.leyou.com(项目访问地址);
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location /item { # 配置转发 当地址带/item 就转发到这个路径
# 先找本地
root html;
if (!-f $request_filename) { #请求的文件不存在,就反向代理
proxy_pass http://192.168.228.100:8084; # 我的项目路径
break;
}
}
location / { #我的前端路径
proxy_pass http://192.168.228.100:9002;
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}
```
静态页面创建完成
前:
配置后:
使用rabbitmq实现修改 删除静态页面
- 导包
//rabbitmq导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
- 在application中配置redis地址
spring:
rabbitmq:
username: leyou #账号姓名
password: leyou #账号密码
virtual-host: /leyou #虚拟主机
host: 192.168.228.100 #redis所在虚拟机地址
- 在实现层 添加消息发送者 当用户更改或者删除时去调用这个方法
/**
*
* 发送消息的方法,id就是spuId,type就是操作类型(增删改查)
* @param id
* @param type
*/
private void sendMessage(String type,Long id) {
// 发送消息 就是消息类型和id
try {
this.amqpTemplate.convertAndSend("item." + type, id);
} catch (Exception e) {
e.printStackTrace();
}
}
- 编写监听器 当交换器中有消息时 对应的方法就会执行
@Component
public class GoodsListener {
@Autowired
private FileService fileService;
//增加和修改的方法放一起 修改就是重新生成一个同名的Html 会自动覆盖
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "ly.create.page.queue", durable = "true"),
exchange = @Exchange(
value = "ly.item.exchange",
type = ExchangeTypes.TOPIC),
key = {"item.insert", "item.update"}))
public void listenCreate(Long id) throws Exception {
if (id == null) {
return;
}
// 创建页面
fileService.createHtml(id);
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "ly.delete.page.queue", durable = "true"),
exchange = @Exchange(
value = "ly.item.exchange",
type = ExchangeTypes.TOPIC),
key = "item.delete"))
public void listenDelete(Long id) {
if (id == null) {
return;
}
// 删除页面
fileService.deleteHtml(id);
}
}
- 在FileService中添加 删除的静态页面的方法
public void deleteHtml(Long id) {
File file = new File(destPath, id + ".html");
file.deleteOnExit();
}