使用Thymeleaf模板生成静态页面

引入gradle依赖

build.gradle文件中,添加

dependencies {

compile( 'org.springframework.boot:spring-boot-starter-thymeleaf')

        compile('net.sourceforge.nekohtml:nekohtml:1.9.22')

}

如果能保证网页完全是复合html5规范,nekohtml可以不使用。

application.yml中配置thymeleaf

spring:

  thymeleaf:

    prefix: classpath:/templates/

    suffix: .html

    mode: LEGACYHTML5

    encoding: UTF-8

    content-type: text/html

    cache: false  


创建模板

templates/index.html

DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"

xmlns:th="http://www.thymeleaf.org"

xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

<head>

<meta charset="UTF-8">

<title>Titletitle>

head>

<body>

<h1 th:text="${name}">列表名称h1>

<ul>

<li th:each="item: ${array}" th:text="${item}">条目li>

ul>

body>

html>



使用Thymeleaf生成静态页面

//构造模板引擎
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        resolver.setPrefix("templates/");//模板所在目录,相对于当前classloader的classpath。
        resolver.setSuffix(".html");//模板文件后缀
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(resolver);

        
        List list = new ArrayList<>();
        list.add("需求");
        list.add("成果");
        list.add("人力");
        list.add("进度");

        //构造上下文(Model)
        Context context = new Context();
        context.setVariable("name", "列表数据");
        context.setVariable("array", list);



        //渲染模板
        FileWriter write = new FileWriter("result.html");
        templateEngine.process("example", context, write);

注意:这里必须是resolver.setPrefix("templates/");不能是resolver.setPrefix("/templates/");

不然就会出现运行时错误:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "example", template might not exist or might not be accessible by any of the configured Template Resolvers

文件名命名规定:

商品分类:/category/159-0-0-0-0-0-1.htm

大类id-品牌id-

商品详情页:直接使用产品id来命名网页,例如“product/132476500.htm”。

1、使用Redis来建立生产队列。

创建generate redis队列


注入

	@Resource
	private RedisTemplate redisTemplate;

TaskModel task = new TaskModel();String key = "generate:" + "version:" + "v1.0.0";redisTemplate.opsForList().leftPush(key, task);

使用Disruptor来作为消费者队列。


使用此机制,来处理所有的详情页。不处理列表页。



你可能感兴趣的:(redis,thymeleaf,disruptor)