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 ,主要两个作用,用户认证和授权.即我们常说的,用户只有登录了才能进行其他操作,没有登录的话就重定向到登录界面.有的用户有权限执行某一操作,而有的用户不能 ...
随机推荐
- k8s dockerk个人学习(2)
部署nginx和应用 部署nginx时我们需要把nginx的配置文件那出来便于以后修改nginx的配置,放置nginx配置文件时我们用k8s的pv的存储方式来存放这些文件 1.安装nfs服务,在kub ...
- SVN命令行使用总结
1.上传项目到SVN服务器上svn import project_dir(本地项目全路径) http://192.168.1.242:8080/svn/IOS/Ben/remote_dir(svn项目 ...
- Boosting学习笔记(Adboost、GBDT、Xgboost)
转载请注明出处:http://www.cnblogs.com/willnote/p/6801496.html 前言 本文为学习boosting时整理的笔记,全文主要包括以下几个部分: 对集成学习进行了 ...
- ES6必知必会 (二)—— 字符串和函数的拓展
字符串的拓展 1.ES6为字符串添加了遍历器接口,因此可以使用for...of循环遍历字符串 2.字符串新增的 includes().startsWith().endsWidth() 三个方法用于判断 ...
- VS2010 C++编译报错LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
查找是否有两个cvtres.exe.一个是C:\Program Files(x86)\Microsoft Visual Studio 10.0\vc\bin\cvtres.exe, 另一个是C:\Wi ...
- vmware 安装 ios 苹果系统
我用的系统是win10... 一.所需软件: 1.下载并安装VMware Workstation Pro 12 密码:7ybc和序列号 密码是:bwm0 2.下载unlocker 203(for OS ...
- 组件之间的通讯:vuex状态管理,state,getters,mutations,actons的简单使用(一)
之前的文章中讲过,组件之间的通讯我们可以用$children.$parent.$refs.props.data... 但问题来了,假如项目特别大,组件之间的通讯可能会变得十分复杂... 这个时候了我们 ...
- 【python】正则表达式-常用函数
m = re.search(pattern, string) # 搜索整个字符串,直到发现符合的子字符串. m = re.match(pattern, string) # 从头开始检查字符串是否符合正 ...
- Eclipse中不让.svn文件夹一同编译到class中
开发过程中一直在用SVN做版本控制,使用Eclipse编译文件后,classes文件中总是有.svn的文件夹,在做提交时有时会报错,这些文件没有什么用,而且影响build的速度. 更恼火的是, 如果一 ...
- Redis内存数据库操作命令详解
一.连接操作相关的命令 quit:关闭连接(connection) auth:简单密码认证 二.对value操作的命令 exists(key):确认一个key是否存在 ...