1、创建SpringBoot项目时,勾选freemarker依赖(web,dev)
2、Controller中向model中存放数据

package com.ziv.springbootbygralde.controller;

import java.util.ArrayList;
import java.util.List; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import com.ziv.springbootbygralde.entity.UserEntity; @Controller
@RequestMapping("/temp")
public class FreeMarkerController { private static final Logger log = LoggerFactory.getLogger(FreeMarkerController.class); @RequestMapping("/show")
public String show(Model model) { model.addAttribute("str", "Hello Spring Boot freemarker");
UserEntity userEntity = new UserEntity();
userEntity.setId(100);
userEntity.setName("zhong ziv");
model.addAttribute("stu", userEntity); List<UserEntity> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
UserEntity usertemp = new UserEntity();
usertemp.setId(i);
usertemp.setName("user" + i);
list.add(usertemp);
}
model.addAttribute("user", list); log.info(model.toString());
return "show";
} @RequestMapping("/Demofreemarker")
public String freemarker(Model model) { model.addAttribute("str", "Hello Spring Boot freemarker");
///返回string必须和templates中的文件名称一致,否则报错
return "freemarker";
}
}

3、freemarker模板默认在templates/*.ftl可以在配置文件中改变设置

#freemarker configuration
#freemarker *.ftl
spring.freemarker.suffix=.html
#freemarker文件的目录,默认目录为templates下面的文件,web文件目录下的文件
spring.freemarker.template-loader-path=classpath:/web

4、freemarker模板上设置

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/js/jquery-3.4.1.min.js"></script>
</head>
<body>
<h3>show:${str}</h3>
<hr>
<h3>用户</h3>
<!-- ??判断stu是否存在,是否为null -->
<#if stu??>
<!-- id==1001才进行显示 -->
<#if stu.id ==1001>
编码:${stu.id}<br>
名称:${stu.name}<br>
<#else>
学生信息不为1001
</#if>
<#else>
没有学生信息
</#if>
<hr>
<h3>遍历学生</h3>
<#list user as u>
编码:${u.id}<br>
名称:${u.name}<br>
<hr>
</#list>
<button id="btn" >点击</button>
<script type="text/javascript">
$("#btn").click(function(){
alert("click");
})
</script>
</body>
</html>

SpringBoot中FreeMarker创建的更多相关文章

  1. SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用

    1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...

  2. 基于springboot的freemarker创建指定格式的word文档

    在web或其他应用中,经常我们需要导出或者预览word文档,比较实际的例子有招聘网站上预览或者导出个人简历,使用POI导出excel会非常的方便,但是如果想导出word,由于其格式控制非常复杂,故而使 ...

  3. IDEA中SpringBoot项目快速创建单元测试

    如何在IDEA中对于SpringBoot项目快速创建单元测试 创建测试用例 右键需要进行测试的方法,选择GO TO然后选择Test 点击Create New Test 勾选需要创建单元测试的方法 然后 ...

  4. SpringBoot中使用@Scheduled创建定时任务

    SpringBoot中使用@Scheduled创建定时任务 定时任务一般会在很多项目中都会用到,我们往往会间隔性的的去完成某些特定任务来减少服务器和数据库的压力.比较常见的就是金融服务系统推送回调,一 ...

  5. 使用docker创建MySQL容器,并在springboot中使用

    最近在看网上找了个springboot的项目试试,在项目中需要的MySQL版本是5.7,但是本机的MySQL只有5.5.因此想着在我的服务器上跑一个MySQL:5.7的容器解决这一问题,但是在实际操作 ...

  6. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别

    SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...

  7. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍

    原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...

  8. springboot使用Freemarker继承

    最近需要用到Freemarker的继承.但是发现没有关于springboot配置Freemarker的继承的.所以趁现在有时间写个博客. 1. Freemarker继承介绍 Freemarker 通过 ...

  9. springboot整合freemarker(转)

    添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

随机推荐

  1. 与Swing的初见

    ---------------------------参考菜鸟教程的swing课程学习-------------------- Swing 是一个为Java设计的GUI工具包. Swing是JAVA基 ...

  2. django的自定义约束

    准备阶段 在settings中的INSTALLED_APPS配置当前app,不然django无法找到自定义的simple_tag. 在app中创建templatetags模块(模块名只能是templa ...

  3. NOIP2016 D1T1 玩具谜题

    洛谷P1563 看完了noip2017觉得noip2016是真的简单……2017第一题就卡住2016第一题10分钟AC 思路: m<=100000很明显暴力模拟就可以 唯一有一点点难度的地方就是 ...

  4. 计算两个GPS坐标的距离

    场景:已知两个GPS点的经纬度坐标信息.计算两点的距离. 1. 距离/纬度关系 GPS: 22.514519,113.380301 GPS: 22.511962,113.380301 距离: 284. ...

  5. Docker(三):Docker入门教程-CentOS Docker 安装

    CentOS Docker 安装 Docker支持以下的CentOS版本: CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更高的版本 前提条件 目前,CentOS 仅发 ...

  6. WPF 远程显示原图 当前主页面 工具栏 一个Window页面的元素适用一个效果

    http://www.jb51.net/article/98384.htm 1.wpf远程显示原图: Stretch="Fill" + ; 主要是因为那个950和650,据显示位置 ...

  7. css-按钮中有图片和文字,怎么才能让文字和图片都中??

    <div class="btn1 trans" > <img src="../../images/img/add.png"/> < ...

  8. 接口返回buffer的16进制数据如何转换

    我们请求接口数据经常会看到buffer数据,这是我们可以使用data.toString()就可以啦~

  9. Activiti组任务(十)

    1 Candidate-users 候选人 1.1需求 在流程定义中在任务结点的 assignee 固定设置任务负责人,在流程定义时将参与者固定设置在.bpmn 文件中,如果临时任务负责人变更则需要修 ...

  10. sh_05_超市买苹果

    sh_05_超市买苹果 # 1. 定义苹果的单价 price = 8.5 # 2. 挑选苹果 weight = 7.5 # 3. 计算付款金额 money = weight * price # 4. ...