springboot模板(Freemarker与Thymeleaf)
Thymeleaf模板
Thymeleaf就是html页面
导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Spring Boot官方文档建议在开发时将缓 存关闭,那就在application.yml文件中加入:

正式环境还是要将缓存开启的
实体类
User
package com.javaxl.springboot01.entity; import lombok.Data; /**
* @author XuFanQi
* @site
* @company
* @create 2019-11-26 15:37
*/
@Data
public class User {
private Integer uid;
private String uname;
private String pwd; public User(Integer uid, String uname, String pwd) {
this.uid = uid;
this.uname = uname;
this.pwd = pwd;
} public User() {
}
}
Controller层
userController
package com.javaxl.springboot01.controller; import com.javaxl.springboot01.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List; /**
* @author XuFanQi
* @site
* @company
* @create 2019-11-26 15:39
*/
@Controller
@RequestMapping("/thymeleaf")
public class userController { @RequestMapping("/list")
public String hello(HttpServletRequest request){
/**
* 1.获取单个值
* 2.能够在html页面竞争遍历展示
* 3.如何在HTML页面转义代码块
*/
request.setAttribute("msg","传输单个字符串! ! !");
List<User> userList = new ArrayList<>();
userList.add(new User(1,"zs","123456"));
userList.add(new User(2,"ls","1234567"));
userList.add(new User(3,"ww","1234568"));
request.setAttribute("userList",userList);
request.setAttribute("htmlStr","<span style='color:red'>转义html代码块</span>"); return "list";
}
}
list.html页面
(注意:
<html xmlns:th="http://www.thymeleaf.org">
)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>thymeleaf模板介绍</title>
</head>
<body>
<div th:text = "${msg}"></div>
<table width="60%" border="1">
<tr>
<td>ID</td>
<td>Uname</td>
<td>Pwd</td>
</tr>
<tr th:each="u : ${userList}">
<td th:text="${u.uid}"></td>
<td th:text="${u.uname}"></td>
<td th:text="${u.pwd}"></td>
</tr>
</table>
<div th:utext="${htmlStr}"></div> </body>
</html>
浏览器访问
Freemarker模板
首先导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency> <!--可以不加,但是做项目的时候可能会用-->
<resources>
<!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--freemarker模板也读取需要注释标红地方-->
<resource>
<directory>src/main/resources</directory>
<includes>
<!--<include>*.properties</include>-->
<!--<include>*.xml</include>-->
<!--<include>*.yml</include>-->
</includes>
</resource>
</resources>
配置application.yml
server:
servlet:
context-path: /spr
port: 80 user:
uname: zs
pwd: 123456
age: 18
sex: "男"
addr: "北京"
spring:
thymeleaf:
cache: false
freemarker:
# 设置模板后缀名
suffix: .ftl
# 设置文档类型
content-type: text/html
# 设置页面编码格式
charset: UTF-8
# 设置页面缓存
cache: false
# 设置ftl文件路径,默认是/templates,为演示效果添加role
template-loader-path: classpath:/templates/freemarker
mvc:
static-path-pattern: /static/**
配置freemarker.ftl

前台页面
list.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>取值</h2>
<h3>提供默认值</h3>
welcome 【${name!'未知'}】 to freemarker! <h3>exists用在逻辑判断</h3>
<#if name?exists>
${name}
</#if> <h2>条件</h2>
<#if sex=='girl'>
女
<#elseif sex=='boy'>
男
<#else>
保密
</#if> <h2>循环</h2>
<table border="1px" width="600px">
<thead>
<tr>
<td>ID</td>
<td>角色名</td>
<td>描述</td>
</tr>
</thead>
<tbody>
<#list roles as role>
<tr>
<td>${role.rid}</td>
<td>${role.roleName}</td>
<td>${role.desc}</td>
</tr>
</#list>
</tbody>
</table> <h2>include</h2>
<#include 'foot.ftl'> <h2>局部变量(assign)/全局变量(global)</h2>
<#assign ctx1>
${springMacroRequestContext.contextPath}
</#assign> <#global ctx2>
${springMacroRequestContext.contextPath}
</#global> ss
${ctx1}和${ctx2}
ss </body>
</html>
foot.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
版权
</body>
</html>
实体类
Role
package com.javaxl.springboot01.entity; import lombok.Data; /**
* @author XuFanQi
* @site
* @company
* @create 2019-11-26 16:53
*/
@Data
public class Role {
private Integer rid;
private String roleName;
private String desc; public Role(Integer rid, String roleName, String desc) {
this.rid = rid;
this.roleName = roleName;
this.desc = desc;
} public Role() {
} // public Integer getRid() {
// return rid;
// }
//
// public void setRid(Integer rid) {
// this.rid = rid;
// }
//
// public String getRoleName() {
// return roleName;
// }
//
// public void setRoleName(String roleName) {
// this.roleName = roleName;
// }
//
// public String getDesc() {
// return desc;
// }
//
// public void setDesc(String desc) {
// this.desc = desc;
// }
}
Controller层
RoleController
package com.javaxl.springboot01.controller; import com.javaxl.springboot01.entity.Role;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import java.util.ArrayList;
import java.util.List; /**
* @author XuFanQi
* @site
* @company
* @create 2019-11-26 16:49
*/
@Controller
@RequestMapping("/freemarker")
public class RoleController {
@RequestMapping("/role/list")
public ModelAndView roleList(){
ModelAndView mav = new ModelAndView();
mav.setViewName("/list"); mav.addObject("name",null);
mav.addObject("sex","gay");
List list = new ArrayList();
list.add(new Role(1,"老师","教书育人"));
list.add(new Role(2,"学生","知识改变命运"));
mav.addObject("roles",list); return mav;
} @RequestMapping("toLogin")
public String toLogin(){
return "login";
}
}
浏览器访问

springboot模板(Freemarker与Thymeleaf)的更多相关文章
- springboot之freemarker 和thymeleaf模板web开发
Spring Boot 推荐使用Thymeleaf.FreeMarker.Velocity.Groovy.Mustache等模板引擎.不建议使用JSP. 一.Spring Boot 中使用Thymel ...
- SpringBoot集成freemarker和thymeleaf模板
1.在MAVEN工程POM.XML中引入依赖架包 <!-- 引入 freemarker 模板依赖 --> <dependency> <groupId>org.spr ...
- SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用
1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...
- SpringBoot集成Freemarker与Thymeleaf
一:概括 pom.xml添加依赖 配置application.yml HTML页面使用表达式 二:Freemarker模板引擎 1.添加依赖 <!-- ftl模板引擎 --> <de ...
- spring boot ----> 常用模板freemarker和thymeleaf
===========================freemarker=================================== freemarker 官网:https://freem ...
- springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息
1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...
- springboot集成模板引擎freemarker和thymeleaf
freemarkder和thymeleaf都是java的模板引擎,这里只介绍这两种模板引擎如何在sprongboot中配置: 1. freemarkder 1.1 在pom.xml中添加依赖包 < ...
- 170703、springboot编程之模板使用(thymeleaf、freemarker)
官方不推荐集成jsp,关于使用jsp模板我这里就不赘述,如果有需要的,请自行百度! thymeleaf的使用 1.在pom中增加thymeleaf支持 <dependency> <g ...
- Springboot模板(thymeleaf、freemarker模板)
目的: 1.thymeleaf模板 2.Freemarker模板 thymeleaf模板 thymeleaf 的优点: 支持html5标准,页面无须部署到servlet开发到服务器上,直接通过浏览器就 ...
随机推荐
- Unreal Engine 4 系列教程 Part 8:粒子系统教程
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- 明解C语言 中级篇 第四章答案
练习4-1 /* 珠玑妙算 */ #include <time.h> #include <ctype.h> #include <stdio.h> #include ...
- RabbitMQ如何保证消息99.99%被发送成功?
1. 本篇概要 RabbitMQ针对这个问题,提供了以下几个机制来解决: 生产者确认 持久化 手动Ack 本篇博客我们先讲解下生产者确认机制,剩余的机制后续单独写博客进行讲解. 2. 生产者确认 要想 ...
- k8s修改pod的hosts文件
1.在1.7版本后使用HostAliases修改pod的hosts文件.该文件由kubelet管理 在deployment的yaml文件中添加在pod template 的spec里面即可: apiV ...
- nginx代理tcp请求
1.概述 ngx_stream_core_module 这个module在nginx1.90后开始支持.开启nginx的tcp代理支持--with-stream=dynamic --with-stre ...
- JVM的监控工具之jstack
参考博客:https://www.jianshu.com/p/213710fb9e40 jstack(Stack Trace for Java)命令用于生成虚拟机当前时刻的线程快照(一般称为threa ...
- 【转载】百度百科:FusionCube超融合
[转载]百度百科:FusionCube超融合 华为FusionCube融合基础设施一体机(Huawei FusionCube Converged Infrastructure)是华为公司IT产品线云计 ...
- 【JS】---5 JS通过事件隐藏显示元素
JS通过事件隐藏显示元素 在开发中,很多时候我们需要点击事件,才显示隐藏元素.那如何做到页面刚开始就把标签隐藏. 有两种方法: (1) display:none <div id=" ...
- GDAL读取Shapefile
-------------------------------------------------------------------------------------- #include < ...
- Retrofit的优点
Retrofit的优点 可以配置不同HTTP client来实现网络请求,如okhttp.httpclient等 将接口的定义与使用分离开来,实现结构. 支持多种返回数据解析的Converter可以快 ...
