Freemarker(SpringBoot项目集成)

1.引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2.配置文件

spring:
  freemarker:
    #开启 freemarker 功能
    enabled: true
    #关闭模板缓存,方便测试
    cache: false
    settings:
      template_update_delay: 0
    #页面模板后缀名
    suffix: .ftl
    charset: UTF-8
    #页面模板位置(默认为 classpath:/templates/)
    template-loader-path: classpath:/templates/
  resources:
    #关闭项目中的静态资源映射(static、resources文件夹下的资源)
    add-mappings: false

3.模板文件

放在resources.templates目录下,文件以.ftl结尾

4.Controller

    @GetMapping("/{courseBaseId}")
    public ModelAndView preview(@PathVariable Long courseBaseId) {
        //创建返回结果
        ModelAndView mv = new ModelAndView();
        //指定模板
        mv.setViewName("learing_article");
        //指定数据
        //调用service查询页面所需的预览数据
        Map<String,Object> map = coursePubService.preview(courseBaseId);
        mv.addAllObjects(map);
        return mv;
    }

 5.生成静态html

    @Test
    public void testHtml() throws IOException, TemplateException {
        //创建配置类,指定版本信息
        Configuration configuration = new Configuration(Configuration.getVersion());
        //指定模板,字符编码
        String path = this.getClass().getResource("/templates").getPath();
        configuration.setDirectoryForTemplateLoading(new File(path));
        configuration.setDefaultEncoding("utf-8");
        //获取模板文件
        Template template = configuration.getTemplate("01-basic.ftl");
        //构造数据,必须是一个map集合
        Map data = getData();
        //根据数据 + 模板文件进行数据填充,得到字符串
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, data);
        //将静态化字符串写入指定文件下
        InputStream inputStream = IOUtils.toInputStream(content);
        FileOutputStream outputStream = new FileOutputStream(new File("d:/index.html"));
        IOUtils.copy(inputStream,outputStream);
    }

    private Map getData() {
        HashMap map = new HashMap();
        map.put("name","黑马179");
        Student student = new Student();
        student.setName("王者荣耀");
        student.setAge(12);
        map.put("student",student);
        return map;
    }
//项目中得到map集合后,把页面存入minio
        

//页面静态化
//获取模板文件
Template template = configuration.getTemplate("learing_article.ftl");
//根据数据 + 模板文件进行数据填充,得到字符串
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
//创建输入流
ByteArrayInputStream inputStream = new ByteArrayInputStream(html.getBytes());

//存入minio
//配置上传的请求地址
String filePath = "/pages/" + coursePub.getId() + ".html";
//文件上传
//文件上传对象
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket(properties.getBucket())
                .object(filePath)
                .stream(inputStream, inputStream.available(), -1)
                .contentType("text/html")
                .build();
//文件上传
client.putObject(putObjectArgs);

官方文档:freemarker

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值