spring整合freemarker (模拟CMS系统)

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

        <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-freemarkerartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-devtoolsartifactId>
        <optional>trueoptional>
    dependency>

        <dependency>
            <groupId>org.freemarkergroupId>
            <artifactId>freemarkerartifactId>
            <version>2.3.23version>
        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 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
spring整合freemarker (模拟CMS系统)_第1张图片

你可能感兴趣的:(springBoot)