DEMO: springboot 与 freemarker 集成
直接在 DEMO: springboot 与 mybatis 集成 基础上,进行修改。
1、pom.xml 中引用 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
2、src/main/resources/application.properties 中添加 freemarker 配置
关键是模板的位置:spring.freemarker.template-loader-path=classpath:/static/ftl
########################################################
###freemarker
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.allow-session-override=false
spring.freemarker.cache=true
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.prefer-file-system-access=true
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/static/ftl
spring.freemarker.settings.template_update_delay=0
spring.freemarker.settings.default_encoding=UTF-8
spring.freemarker.settings.classic_compatible=true
spring.freemarker.order=1
3、添加模板文件 src/main/resources/static/ftl/student.ftl
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body> <#list students as student >
${student.id} - ${student.name} - ${student.sex} - ${student.no} <br/>
</#list>
</body>
</html>
4、修改 StudentController.java
1、方法返回,void 改成 String,返回模板名字 “student”
2、加个map入参,传数据给前端模板
package demo.springboot.controller; import java.util.ArrayList;
import java.util.List;
import java.util.Map; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import demo.springboot.model.Student;
import demo.springboot.service.StudentService; @Controller
@RequestMapping("/student")
public class StudentController { private static final Logger LOGGER = LoggerFactory.getLogger(StudentController.class); @Autowired
private StudentService studentService; @RequestMapping("/list")
public String listStudent(Map<String, Object> map){
List<Student> students = new ArrayList<>();
students = studentService.listStudents(); for (Student student : students){
LOGGER.info(student.getId() + " - " + student.getName() + " - " + student.getNo() + " - " + student.getSex());
} map.put("students", students); return "student";
} }
5、启动应用,浏览器访问 http://localhost:8080/student/list,大功告成

DEMO: springboot 与 freemarker 集成的更多相关文章
- DEMO: springboot 与 mybatis 集成
之前一直在用springMVC,接触到springboot之后,感觉使用起来方便多了,没那多xml需要配置. 先来看看整个项目结构,当然是maven项目. 1.测试数据 DROP TABLE IF E ...
- SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL
1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...
- SpringBoot(七):集成DataSource 与 Druid监控配置
绑定DataSource:Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource,Druid是Java语言中最好的数据库连接池,并且能够提供 ...
- Springboot 和 Mybatis集成开发
Springboot 和 Mybatis集成开发 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 jdk:1.7.0_79 maven:3.3.9 额外功能 PageHel ...
- 记springboot+mybatis+freemarker+bootstrap的使用(1)
一..springboot的配置 1.安装并配置maven maven是项目管理工具,可以自动下载并管理jar包之间的依赖关系,可通过maven自动配置springboot 参照百度经验https:/ ...
- springboot整合freemarker(转)
添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- SpringBoot系列之集成jsp模板引擎
目录 1.模板引擎简介 2.环境准备 4.源码原理简介 SpringBoot系列之集成jsp模板引擎 @ 1.模板引擎简介 引用百度百科的模板引擎解释: 模板引擎(这里特指用于Web开发的模板引擎)是 ...
- SpringBoot整合freemarker 引用基础
原 ElasticSearch学习笔记Ⅲ - SpringBoot整合ES 新建一个SpringBoot项目.添加es的maven坐标如下: <dependency> <groupI ...
- 十二、SpringBoot 优雅的集成Spring Security
前言 至于什么是Spring security ,主要两个作用,用户认证和授权.即我们常说的,用户只有登录了才能进行其他操作,没有登录的话就重定向到登录界面.有的用户有权限执行某一操作,而有的用户不能 ...
随机推荐
- Servlet基本操作
一.Servlet的请求流程 web项目中的web.xml文件配置为: <servlet> <!--别名--> <servlet-name>Hello</se ...
- uml建模工具介绍
应用最广的由两种种1. Rational Rose,它是ibm的 .2.Microsoft的 Microsoft Office Visio® 2003 3.Enterprise Architect.还 ...
- JS 中 this 的用法
this是JavaScript语言中的一个关键字 他是函数运行时,在函数体内部自动生成的一个对象, 只能在函数体内部使用. 在不同function中, this有不同的值. 1. 纯粹的函数调用. f ...
- sql 关键字解析
sql 关键字解析 关键字 解析 union 注意:每个 SELECT 语句,必须列的数量.顺序相同,列的数据类型相似.即:1. UNION 内部的每个 SELECT 语句必须拥有相同数量的列:2. ...
- HTTP报头:通用报头,请求报头,响应报头和实体报头
缓存控制优先级从高到低分别是Pragma -> Cache-Control -> Expires 报头 每一个报头都是由 [名称 + ":" + 空格 + 值 + ] ...
- FineUI导出Excel
1.[经验分享]导出Excel的乱码问题http://www.fineui.com/bbs/forum.php?mod=viewthread&tid=6326&highlight=Ex ...
- ML(4): 决策树分类
决策树(Decision Tree)是用于分类和预测的主要技术,它着眼于从一组无规则的事例推理出决策树表示形式的分类规则,采用自顶向下的递归方式,在决策树的内部节点进行属性值的比较,并根据不同属性判断 ...
- JS 获取时间
function CurentTime() { var now = new Date(); var year = now.getFullYear(); //年 var month = now.getM ...
- Flask请求处理流程(request)[待续]
WSGI简介 WSGI(全称Web Server Gateway Interface),是为 Python 语言定义的Web服务器和Web应用程序之间的一种简单而通用的接口,它封装了接受HTTP请求. ...
- 惠普(HP)战66 Pro G1 - 批量GHOST[Win10专业版 ] (UEFI)
笔记本型号:惠普(HP)战66 Pro G1 14英寸轻薄笔记本电脑(i5-8250U 8G 256G PCIe SSD+500G 标压MX150 2G独显)银色 需求: 公司一共采购10台笔记本,需 ...