今天给大家分享下 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 = #可以解析的视图名称的白名单。
页面效果:
项目整体结构:
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于