thymeleaf实现页面静态化

每次浏览器访问某些类似页面,比如商品的详情页。如果都要查库,查搜索再生成的话,面对高并发、大流量的情况性能不佳。所以直接把商品页静态话,查商品详情页时,首先查是否有该商品的已经生成的详情页,有的话直接反馈。没有再查库生成。
类似于所有页面套用一个通用模板生成。

jar包

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

Controller层静态化和非Controller层静态化

Controller层静态化:

导入TemplateEngine、配置静态化地址

	@Resource
    private TemplateEngine templateEngine;

    private String localDir="D:/IDEALocation/shopHtml/item/";
//数据静态化
    private void htmlStatic(HttpServletRequest request, HttpServletResponse response,ItemDocum itemDocum) {
    
    
        WebContext ctx = new WebContext(request, response, request.getServletContext(), request.getLocale());
		
		//这里item是页面渲染时的变量名
        ctx.setVariable("item",itemDocum);
            String fileName=itemDocum.getId()+".html";
        FileWriter fileWriter=null;
        try {
    
    
            fileWriter = new FileWriter(localDir+fileName);
            
	        //这里的item为template模板名
            templateEngine.process("item", ctx, fileWriter );
            System.out.println("静态化文件成功");
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if (fileWriter != null) {
    
    
                try {
    
    
                    fileWriter.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }

template类似如下:当然你可以搞得更精致

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="http://res.shop.com/web-js/libs/bootstrap-table-1.15.3/dist/bootstrap-table.min.css">
    <script src="http://res.shop.com/web-js/libs/jquery/jquery-3.4.1.min.js"></script>
    <script src="http://res.shop.com/web-js/libs/bootstrap/js/bootstrap.min.js"></script>
    <script src="http://res.shop.com/web-js/libs/layer/layer.js"></script>
    <script src="http://res.shop.com/web-js/js/ajax.js"></script>
    <script src="http://res.shop.com/web-js/js/cart.js"></script>
</head>
<body>
<div th:text="${item.name}"></div>
<div th:text="${item.price}"></div>
<div th:utext="${item.img}"></div>
</body>
</html>

非Controller层静态化:

代码不变,仅context上下文生成方式变化。

导入TemplateEngine、配置静态化地址

	@Resource
    private TemplateEngine templateEngine;

    private String localDir="D:/IDEALocation/shopHtml/item/";
//数据静态化
    private void htmlStatic(ItemDocum itemDocum) {
    
    
    	//变化
        Context ctx = new Context();
        ctx.setVariable("item",itemDocum);
        String fileName=itemDocum.getId()+".html";
        FileWriter fileWriter=null;
        try {
    
    
            fileWriter = new FileWriter(localDir+fileName);
            templateEngine.process("item", ctx, fileWriter );
            System.out.println("静态化文件成功");
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if (fileWriter != null) {
    
    
                try {
    
    
                    fileWriter.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_38205881/article/details/104830752