spring整合freemarker (模拟CMS系统)

什么是freemarker
freemarker是一个模板框架,模板就是类似jsp的页面,但是模板没有jsp重,jsp开发难度很大,复杂程度很高,所以很多web项目都在使用我们的模板(freemarker、thymeleaf)来替换jsp完成视图层的开发,里面基本是纯html代码,开发难度相对会低一下,
1、导入maven依赖

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

        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.23</version>
        </dependency>

2、编写配置文件: 在application.properties里面增加freemarker的配置:

#freemarker
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:templates

3、编写contoller

package com.xingxue.controller;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.io.*;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/html")
public class MainController {

    @Resource
    Configuration cfg;

    @RequestMapping("/html.do")
    public String main(Model model){
        String info="Welcome FreeMarker!";
        Map root = new HashMap();
        root.put("info",info);
        freeMarkerContent(root);
        model.addAttribute("w","Welcome FreeMarker!");
        return "test";
    }

    private void freeMarkerContent(Map<String,Object> root){
        try {
            Template temp = cfg.getTemplate("index.ftl");
            //以classpath下面的static目录作为静态页面的存储目录,同时命名生成的静态html文件名称
            String path=this.getClass().getResource("/").toURI().getPath()+"static/"+System.currentTimeMillis()+".html";
            Writer file = new FileWriter(new File(path.substring(path.indexOf("/"))));
            temp.process(root, file);
            file.flush();
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}

注意:freemarker是一个静态模板技术, 支持java语言来操作,我们可以通过freemarker找到模板,然后根据需求生成对应的静态html文件, 这个文件就是未来我们的资源文件,我们以后想访问该资源就不需要去数据从新查询改资源的内容,而是直接访问该静态的html文件,从而减轻服务器的压力。

4、新建模板 index.ftl

<html>
<head>
    <title>Welcome!</title>

</head>
<body>
<h1>Hello12${info}</h1>
</body>
</html>

5、启动项目访问http://locahost:8888/html/html.do
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值