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

为什么80%的码农都做不了架构师?>>>   hot3.png

  1. 在页面高访问量并且页面数据不会经常变化的情况下我们可以生成静态页面,下面我们来实战一波spring-boot-thymeleaf生成静态文件
  2. 首先引入jar包
  3. 
    			org.springframework.boot
    			spring-boot-starter-thymeleaf
    		
    		
    			org.springframework
    			spring-context-support
    		
    		
    			org.springframework.boot
    			spring-boot-starter-web
    		
    		
    		
    			ognl
    			ognl
    			3.0.8
    		

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

    
    
    
    
    Insert title here
    
    
    
    
    

     

  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

你可能感兴趣的:(spring-boot-thymeleaf生成静态模板页面)