【SpringBoot 实战】之整合 freemarker

本贴最后更新于 2326 天前,其中的信息可能已经时移世易

今天给大家分享下 SpringBoot 和 freemarker 的整合。

如果不知道怎么创建 SpringBoot,点这里

第一步:引入 jar 包

pom.xml 文件中加入

<!-- 引入freemarker包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>

第二步:编写后台类

创建 FreemarkerController 类:

package com.cimu.freemarker.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.servlet.ModelAndView; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Title: FreemarkerController * Copyright: Copyright (c) 2017 * * @author cimu * date 2018年11月20日 19:02 */ @Controller public class FreemarkerController { @GetMapping("/freemarker") public ModelAndView index(ModelAndView modelAndView) { //你的freemarker文件名称 modelAndView.setViewName("freemarker"); modelAndView.addObject("data", "我是数据"); List<String> strList = new ArrayList<>(); strList.add("1"); strList.add("2"); modelAndView.addObject("list", strList); Map<String,String> map = new HashMap<>(2); map.put("name","张三"); map.put("age","24"); modelAndView.addObject("map", map); return modelAndView; } }

FreemarkerApplication 类:

package com.cimu.freemarker; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class FreemarkerApplication { public static void main(String[] args) { SpringApplication.run(FreemarkerApplication.class, args); } }

第三步:编写 freemarker 模板

在 templates 下面创建文件 freemarker.ftl

<!DOCTYPE html> <html> <head lang="en"> <title>FreeMarker</title> </head> <body> <h2>FreeMarker-${data}<h2> <hr> list取值 <h3> <#list list as item> ${item!}<br/> </#list> </h3> <hr> map取值 <h3> 名称:${map.name}<br> 年龄:${map.age} </h3> </body> </html>

第四步:修改配置

application.properties 可以使用默认的,不进行修改。

spring.freemarker.allow-request-override = false #设置是否允许HttpServletRequest属性覆盖(隐藏)控制器生成的同名模型属性。 spring.freemarker.allow-session-override = false #设置是否允许HttpSession属性覆盖(隐藏)同名控制器生成的模型属性。 spring.freemarker.cache = false #启用模板缓存。 spring.freemarker.charset = UTF-8 #模板编码。 spring.freemarker.check-template-location = true #检查模板位置是否存在。 spring.freemarker.content-type = text/html #Content-Type值。 spring.freemarker.enabled = true #启用此技术的MVC视图分辨率。 spring.freemarker.expose-request-attributes = false #设置是否所有的请求属性都应该在与模板合并之前添加到模型中。 spring.freemarker.expose-session-attributes = false #设置是否所有的HttpSession属性都应该在与模板合并之前添加到模型中。 spring.freemarker.expose-spring-macro-helpers = true #设置是否公开一个供Spring的宏库使用的名为“springMacroRequestContext”的RequestContext。 spring.freemarker.prefer-file-system-access = true #优先选择模板加载的文件系统访问权限。文件系统访问启用模板更改的热检测。 spring.freemarker.prefix = #构建URL时预先查看名称的前缀。 spring.freemarker.request-context-attribute = #所有视图的RequestContext属性的名称。 spring.freemarker.settings.* = #众所周知的FreeMarker键将被传递给FreeMarker的配置。 spring.freemarker.suffix = .ftl #在构建URL时被附加到查看名称的后缀。 spring.freemarker.template-loader-path = classpath:/templates/ #逗号分隔的模板路径列表。 spring.freemarker.view-names = #可以解析的视图名称的白名单。

页面效果:

imagepng

项目整体结构:
imagepng

源码地址

  • B3log

    B3log 是一个开源组织,名字来源于“Bulletin Board Blog”缩写,目标是将独立博客与论坛结合,形成一种新的网络社区体验,详细请看 B3log 构思。目前 B3log 已经开源了多款产品:SymSoloVditor思源笔记

    1063 引用 • 3455 回帖 • 160 关注
  • FreeMarker

    FreeMarker 是一款好用且功能强大的 Java 模版引擎。

    23 引用 • 20 回帖 • 468 关注
  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    948 引用 • 1460 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
chaigx
欢迎关注我的公众号:程序之声。有些文章没办法同步过来,访问个人博客:http://www.chaiguanxin.com 杭州