美文网首页
Spring boot 集成FreeMarker

Spring boot 集成FreeMarker

作者: 离别刀 | 来源:发表于2018-06-07 11:34 被阅读0次
  • 概念
    FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写。
    FreeMarker被设计用来生成HTML Web页面,特别是基于MVC模式的应用程序。
    虽然FreeMarker具有一些编程的能力,但通常由Java程序准备要显示的数据,由FreeMarker生成页面,通过模板显示准备的数据,简单来讲就是模板加数据模型,然后输出页面。
    想学习更多请参考:https://freemarker.apache.org/

1.依赖jar包

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

2.配置application.properties

spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=classpath:/view/

3.FreeMarker使用

  • 后台代码
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import freemarker.template.Configuration;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("marker/")
@Controller
public class FreeMarkerCtrl {

    @Autowired
    private Configuration configuration;

    /**
     * 实现方式1
     * @param map
     * @return
     */
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public String freeMarker(Map<String,Object> map){
        map.put("users",parseUsers());
        map.put("title","用户列表");
        return "users";
    }

    /**
     * 实现方式2
     * @param response
     * @throws IOException
     * @throws TemplateException
     */
    @RequestMapping(value = "/users1",method = RequestMethod.GET)
    public void freeMarker1(HttpServletResponse response) throws IOException, TemplateException {
        Map<String,Object> map= new HashMap<>();
        map.put("users",parseUsers());
        map.put("title","用户列表");
        Template t = configuration.getTemplate("users.html"); 
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(t, map);
        System.out.println(content);
        response.getOutputStream().write(content.getBytes("utf-8"));
    }

    private List<Map> parseUsers(){
        List<Map> list= new ArrayList<>();
        for(int i=0;i<10;i++){
            Map map= new HashMap();
            map.put("name","kevin_"+i);
            map.put("age",10+i);
            map.put("phone","1860291105"+i);
            list.add(map);
        }
        return list;
    }
}
  • html
<html lang="zh-CN">
<head>
    <meta charset="UTF-8"/>
    <title>${title}</title>
    <style>
        table {
            width: 50%;
            font-size: .938em;
            border-collapse: collapse;/*边框合并*/
        }
        th {
            text-align: left;
            padding: .5em .5em;
            font-weight: bold;
            background: #66677c;color: #fff;
        }

        td {
            padding: .5em .5em;
            border-bottom: solid 1px #ccc;
        }

        table,table tr th, table tr td { border:1px solid #0094ff; }/*设置边框*/
    </style>
</head>
<body>
    <table>
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Phone</th>
        </tr>
        <#list users as user>
            <tr>
                <td>${user.name}</td>
                <td>${user.age}</td>
                <td>${user.phone}</td>
            </tr>
        </#list>
    </table>
</body>
</html>

运行结果:


20180607111214.png

更多语法请参考:https://blog.csdn.net/fhx007/article/details/7902040

相关文章

网友评论

      本文标题:Spring boot 集成FreeMarker

      本文链接:https://www.haomeiwen.com/subject/okowsftx.html