SpringBoot中FreeMarker创建
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创建的更多相关文章
- SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用
1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...
- 基于springboot的freemarker创建指定格式的word文档
在web或其他应用中,经常我们需要导出或者预览word文档,比较实际的例子有招聘网站上预览或者导出个人简历,使用POI导出excel会非常的方便,但是如果想导出word,由于其格式控制非常复杂,故而使 ...
- IDEA中SpringBoot项目快速创建单元测试
如何在IDEA中对于SpringBoot项目快速创建单元测试 创建测试用例 右键需要进行测试的方法,选择GO TO然后选择Test 点击Create New Test 勾选需要创建单元测试的方法 然后 ...
- SpringBoot中使用@Scheduled创建定时任务
SpringBoot中使用@Scheduled创建定时任务 定时任务一般会在很多项目中都会用到,我们往往会间隔性的的去完成某些特定任务来减少服务器和数据库的压力.比较常见的就是金融服务系统推送回调,一 ...
- 使用docker创建MySQL容器,并在springboot中使用
最近在看网上找了个springboot的项目试试,在项目中需要的MySQL版本是5.7,但是本机的MySQL只有5.5.因此想着在我的服务器上跑一个MySQL:5.7的容器解决这一问题,但是在实际操作 ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别
SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍
原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...
- springboot使用Freemarker继承
最近需要用到Freemarker的继承.但是发现没有关于springboot配置Freemarker的继承的.所以趁现在有时间写个博客. 1. Freemarker继承介绍 Freemarker 通过 ...
- springboot整合freemarker(转)
添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
随机推荐
- 谈谈mybatis中的#与$的区别
#相当于对数据 加上 双引号,$相当于直接显示数据 . #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:", 如果传入的值是id,则解析成的sql为order by &q ...
- Spring基础16——使用FactoryBean来创建
1.配置bean的方式 配置bean有三种方式:通过全类名(class反射).通过工厂方法(静态工厂&实例工厂).通过FactoryBean.前面我们已经一起学习过全类名方式和工厂方法方式,下 ...
- 使用impala连接hive踩坑过程
一.打包镜像出错 docker build总是出错,如果你用的是python3.7,可以考虑使用python3.6版本 并且注意:选择thrift-sasl==0.2.1,否则会出现: Attribu ...
- 17.Linux-CentOS系统OpenStack-Keystone同步数据库时报错
问题描述:在同步Keystone数据库时报以下错误[root@controller ~]# su -s /bin/sh -c "keystone-manage db_sync" k ...
- bzoj4326: NOIP2015 运输计划(二分+LCA+树上差分)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4326 题目大意:有一颗含有n个顶点的树,每两个点之间有一个边权,现在有m个运输计划,每个 ...
- 安装suds,提示No module named 'client'
最近在研究webservice,但是在线安装suds的时候提示No module named 'client' 提示没有client模块,提示这个错误主要还是因为没有安装client模块 在线安装cl ...
- windows10安装pycharm,以及pycharm教程和破解码
pycharm下载请点我 根据自己的情况选择安装目录 下面我们选择"64位安装"(根据自己的系统来选择),并勾上".py",如图所示: 一定要拉到最后才行 p ...
- 一个错误导致懂了mac系统的PATH环境变量
一个完全不懂mac系统的强迫症小白,由于搭建环境都按照百度走,所以在执行命令echo $PATH查看PATH内容时发现怎么有这样一串东西 /usr/local/bin:/usr/bin:/bin:/u ...
- angularjs表单注册--两次密码验证
html <div class="container" ng-controller="RegisterCtrl"> <form name=&q ...
- 内联元素的盒子模型与文档流定位padding属性
内联元素的盒子模型 1.内联元素不能设置width宽度和高度height span{width:200px ; height:200px} 与 span{width:100 ...