spring-boot-thymeleaf生成静态模板页面

本文介绍如何使用Spring Boot和Thymeleaf生成静态HTML页面,包括依赖配置、模板创建及工具类实现。通过实例演示了从引入所需依赖到最终生成静态文件的全过程。
  1. 在页面高访问量并且页面数据不会经常变化的情况下我们可以生成静态页面,下面我们来实战一波spring-boot-thymeleaf生成静态文件
  2. 首先引入jar包
  3. <dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-thymeleaf</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context-support</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<!-- https://mvnrepository.com/artifact/ognl/ognl -->
    		<dependency>
    			<groupId>ognl</groupId>
    			<artifactId>ognl</artifactId>
    			<version>3.0.8</version>
    		</dependency>

    准备一份测试的模板文件到项目中index.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8" />
    <title>Insert title here</title>
    </head>
    <body>
    <span th:text="hello + ${name}"></span>
    </body>
    </html>

     

  4. 配置相关参数

  5. import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
    
    @Configuration
    public class FreemarkerConfig {
    	
    	@Bean
    	public TemplateEngine templateEngine() {
    		ClassLoaderTemplateResolver classLoaderTemplateResolver = new ClassLoaderTemplateResolver();
    		classLoaderTemplateResolver.setPrefix("/templates/");
    		classLoaderTemplateResolver.setSuffix(".html");
    		classLoaderTemplateResolver.setCacheable(false);
    		classLoaderTemplateResolver.setCharacterEncoding("utf-8");
    		TemplateEngine engine = new TemplateEngine();
    		engine.setTemplateResolver(classLoaderTemplateResolver);
    		return engine;
    	}
    }

    增加工具类

  6. package com.example.demo;
    
    import java.io.FileWriter;
    import java.io.IOException;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.Context;
    
    @Component
    public class FreemarkerUtils {
    	@Autowired
    	private TemplateEngine engine;
    	/**
    	 * 生成静态文件
    	 * @param freeTempName 模板名称
    	 * @param context 数据内容
    	 * @param outFilePath 输出路径
    	 * @return
    	 */
    	public boolean process(String freeTempName,Context context,String outFilePath) {
    		FileWriter fileWriter = null;
    		try {
    			fileWriter = new FileWriter(outFilePath);
    			engine.process(freeTempName, context,fileWriter);
    		} catch (IOException e) {
    			e.printStackTrace();
    			return false;
    		} finally {
    			try {
    				fileWriter.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    				return false;
    			}
    		}
    		return true;
    	}
    }
    

    注入工具类进行测试

  7. import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.thymeleaf.context.Context;
    
    @RestController
    public class IndexController {
    	@Autowired
    	private FreemarkerUtils freeUtrils;
    	@RequestMapping("/")
    	public boolean testFreemarker() throws Exception {
    		Context context = new Context();
    		context.setVariable("name", "张三");
    		return freeUtrils.process("index", context, "D:\\workutil\\workfile\\aaa3.html");
    		
    	}
    }

    测试结果

    1. 4390ebfdd7dc5583d46347dbb2ff106a46c.jpg

转载于:https://my.oschina.net/xpx/blog/1845829

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值