Thymeleaf模板

关于Thymeleaf的优点,我只说一条:它就是html页面。下面直接上代码
pom依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

Spring Boot官方文档建议在开发时将缓存关闭,那就在application.yml文件中加入下面这行

spring.thymeleaf.cache=false


User.java

 package com.hc.springboot01.configurationProperties.entity;import lombok.Data;/*** @author胡聪* @site www.hc.com* @xxx公司* @create  2019-11-26 下午 9:51*/
@Data
public class User {private Integer uid;private String uname;private String pwd;public User() {}public User(Integer uid, String uname, String pwd) {this.uid = uid;this.uname = uname;this.pwd = pwd;}
}

UserController


import com.hc.springboot01.configurationProperties.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胡聪* @site www.hc.com* @xxx公司*  * @create  2019-11-26 下午 9:57*/
@Controller
@RequestMapping("/thymeleaf")
public class UserController {@RequestMapping("/list")public String hello(HttpServletRequest request){/*** 1.获取单个值* 2.能够在html页面进行遍历展示* 3.如何在html也页面转义html代码块*/
//        1.获取单个值request.setAttribute("msg","传输单个字符串");//        2.能够在html页面进行遍历展示List<User> userList = new ArrayList<>();userList.add(new User(1,"zs","123"));userList.add(new User(2,"ls","123"));userList.add(new User(3,"ww","123"));request.setAttribute("userList",userList);//        3.如何在html也页面转义html代码块request.setAttribute("htmlStr","<span style='color:red'>转义html代码块</span>");return "list";}}

list.html
将原的html替换成如下,释放th

<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><!--1.获取单个值-->
<div th:text="${msg}"></div><!--2.能够在html页面进行遍历展示-->
<table width="60%" border="1"><tr><td>id</td><td>用户名</td><td>密码</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><!--3.如何在html也页面转义html代码块-->
<div th:utext="${htmlStr}"></div><!--下拉框-->
<select><option th:each="u:${userList}" th:value="${u.uid}" th:text="${u.uname}">我是默认值</option>
</select>
<div></div>
</body>
</html>

运行结果

Freemarker模板

新建一个ftl文件
默认是没有的,点击设置然后添加

成功之后

导入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文件的默认配置

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>${ctx1}和${ctx2}
sss
</body>
</html>

RoleController

package com.hc.springboot01.controller;import com.hc.springboot01.configurationProperties.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胡聪* @site www.hc.com* @xxx公司* @create  2019-11-26 下午 10:21*/
@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";}
}

common.ftl

<div th:fragment="h2">第一部分内容
</div>
<div th:fragment="h2">第二部分内容
</div>
<div th:fragment="h3">第三部分内容
</div>


thymeleaf中替代jsp:include的写法
一种是包含整个html页面

 <div th:include="common.html"></div>

springboot模板相关推荐

  1. Springboot模板引擎

    Springboot-Thymeleaf 前言 简单测试 使用 结论 前言 我们之前学习的模板引擎是jsp,将后台的数据绑定,通过模板引擎渲染输出到前端. 我们现在不使用jsp,我们使用Thymele ...

  2. springboot模板引擎_Spring Boot实战:如何搞定前端模板引擎?

    作者:liuxiaopeng 链接:https://www.cnblogs.com/paddix/p/8905531.html 前言 虽然现在很多开发,都采用了前后端完全分离的模式,即后端只提供数据接 ...

  3. rabbitMQ快速开始整合springboot模板

    发送端 依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3 ...

  4. springboot模板制作

    复制模板需要修改artifactid

  5. springboot模板整合(四)邮箱验证

    文章目录 引包 邮箱 邮箱工具类 邮箱配置配置 生成随机验证码 html页面 创建js文件夹以及创建js文件 html页面 编辑接口 测试 源码 引包 <!--引入thymeleaf框架--&g ...

  6. 【设计模式实战】SpringBoot模板+策略设计模式实现抽奖流程

    一.需求分析 1. 抽奖余额有两种,1种[货币1],1种[货币2],对应扣除两种余额 2. 不变的部分余额和扣除余额,变化的部分是余额类型与扣除方式,所以可以使用策略模式 二.设计模式思想 1. 定义 ...

  7. SpringBoot 基础拦截器

    所有的开发之中拦截器一定是一个必须要使用的功能,利用拦截器可以更加有效的实现数据的验证处理在SpringBoot之中所使用的拦截器与Spring中的拦截器完全一样.2.1.基础拦截器操作拦截器是一种A ...

  8. SpringBoot 上传多个文件

    在之前都只是进行了单一的文件上传,但是在很多的情况下是有可能要传递有多个文件的,所以这个时候首先要解决的问题是表单里面需要有多个上传的项,而后在控制器上再进行一些简单的处理器即可1.修改上传表单:up ...

  9. SpringBoot 基础上传操作

    所有只要与WEB开发牵扯到的开发框架都必须去面对有文件的上传处理,在原始的Spring之中所使用的上传组件是apache的fileupload组件, 在SpringBoot里面也同样要继续使用此组件. ...

最新文章

  1. 亚马逊生鲜的产品质疑!国外设计师怎么分析用户体验(中英图文对照版)
  2. SQL注入到EXP编写
  3. linux字符设备开发
  4. cocos2dx之lua项目开发中MVC框架的简单应用
  5. 我与TCP连接不得不说的故事
  6. 普通的PHP上传到云函数,php封装上传函数代码示例
  7. (6)tcp-socket
  8. 随机数生成器 java_Java中的随机数生成器
  9. apper安卓×××
  10. 虚拟机linux快捷键,虚拟机控制与Linux快捷键
  11. linux能远程开机么,Linux下如何实现远程开机
  12. 【科创人独家】云风:从创业到招安,自由的游戏玩家+务实的程序员
  13. Day10:捡烟蒂投资法:便宜组合
  14. 【Devc++】战斗1.0.1
  15. 一个08届毕业的学长写给即将毕业的09届的学弟学妹们
  16. 这世上,真有人会陪你手握屠龙刀,杀得生活措手不及!
  17. oracle查询语句大全
  18. cocoscreator数字增长动画
  19. jwt无状态权限认证(pings-shiro-jwt)
  20. java 在数组末尾添加元素和在任意位置删除元素

热门文章

  1. 阿里云与海底捞合作QA
  2. 网络学习---HTTPS的升级
  3. linux安装网卡驱动教程6,CentOS 6.5安装无线网卡驱动教程
  4. 电脑辐射对身体有多大危害
  5. HTML5编写的小游戏
  6. iPad上的数学软件介绍与畅想
  7. 常用API函数 自己研究的API函数
  8. paddle实现语音转文字
  9. pentaho使用步骤简介
  10. 物联网学习之旅:微信小程序控制STM32(三)--STM32代码编写