今天给大家分享下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 = #可以解析的视图名称的白名单。
页面效果:
项目整体结构:
本文为博主原创文章,未经博主允许不得转载。
更多内容请访问:IT源点
注意:本文归作者所有,未经作者允许,不得转载