Spring boot 集成FreeMarker

首先在bootstrap.yml里面配置freemarker信息

spring:
  freemarker:
    request-context-attribute: req #req访问request
    suffix: .html  #后缀名
    content-type: text/html
    enabled: true
    cache: false #缓存配置
    template-loader-path: classpath:/file/html/ #模板加载路径 
    charset: UTF-8 #编码格式
    settings:
      number_format: '0.##'   #数字格式化,无小数点

然后再类里面添加

(@Resource默认按照名称方式进行bean匹配,@Autowired默认按照类型方式进行bean匹配)

public class ShareController {

    @Resource
    FreeMarkerConfigurer freeMarkerConfigurer;


    @RequestMapping(value = "/demo",method = {RequestMethod.POST})
    public @ResponseBody String demo(@RequestBody String json) {
        try {
            a();
        } catch (Exception e) {
           
        }
        return result;
    }


    private String a()throws Exception{
        //获取连接
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        //获取模板
        Template template = configuration.getTemplate("BTC.html");//yml文件里已经配置了文件路径,这里只写文件名几号
        Writer wr = new FileWriter("C:\\work\\"+"测试"+".html");
        //写入
        Map map = new HashMap<>();
        map.put("a", "哎呀");
        template.process(map, wr);

        return null;
    }

 

html文件里${a}的内容就会被替换为“哎呀”两个字。

 

 

 

你可能感兴趣的:(java,技术)