1、thymeleaf模板

2.Freemarker模板

Thymeleaf模板

首先导入依赖

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

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

spring:
thymeleaf:
cache: true

user

package com.jt.springboot.entity;

import lombok.Data;

@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层

package com.jt.springboot.controller;

import com.jt.springboot.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; @Controller
@RequestMapping("/thymeleaf")
public class UserController {
@RequestMapping("/list")
public String hello(HttpServletRequest request){
request.setAttribute("msg","传单个字符");
List<user> userList=new ArrayList<>();
userList.add(new user(,"zs",""));
userList.add(new user(,"ww",""));
userList.add(new user(,"ss",""));
request.setAttribute("userList",userList);
request.setAttribute("htmlStr","<span style='color:red' >转译html代码块</span>");
return "list";
}
}

list.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>thymeleaf模板介绍</title>
</head>
<body>
<div th:text="${msg}"></div>
</body>
</html>

html加入提示

<html xmlns:th="http://www.thymeleaf.org">

遍历
htlm页面

<!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="">
<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>
<div th:utext="${htmlStr}"></div>
</body>
</html>

Freemarker模板

学习网站:http://freemarker.foofun.cn/
导入pom依赖

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

application.yml文件的默认配置

spring:
thymeleaf:
cache: false freemarker:
# 设置模板后缀名
suffix: .ftl
# 设置文档类型
content-type: text/html
# 设置页面编码格式
charset: UTF-
# 设置页面缓存
cache: false
# 设置ftl文件路径,默认是/templates,为演示效果添加role
template-loader-path: classpath:/templates/freemarker
mvc:
static-path-pattern: /static/**

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

foot.ftl

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$Title$</title>
</head>
<body>
版权信息
</body>
</html>
common.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:fragment="h1">
第一部分内容
</div>
<div th:fragment="h2">
第二部分内容
</div>
<div th:fragment="h3">
第三部分内容
</div>
</body>
</html>
RoleController
package com.jt.springboot.controller;

import com.jt.springboot.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; @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(,"老板","做生意"));
list.add(new Role(,"员工","打工"));
mav.addObject("roles",list); return mav;
} @RequestMapping("toLogin")
public String toLogin(){
return "login";
} }

springboot模板的更多相关文章

  1. 8.SpringBoot 模板引擎 Thymeleaf

    1.模板引擎原理 JSP.Velocity.Freemarker.Thymeleaf 都是模板引擎.SpringBoot推荐的Thymeleaf:语法更简单,功能更强大: Thymeleaf模板引擎 ...

  2. springboot模板(Freemarker与Thymeleaf)

    Thymeleaf模板 Thymeleaf就是html页面 导入pom依赖 <dependency> <groupId>org.springframework.boot< ...

  3. Springboot模板(thymeleaf、freemarker模板)

    目的: 1.thymeleaf模板 2.Freemarker模板 thymeleaf模板 thymeleaf 的优点: 支持html5标准,页面无须部署到servlet开发到服务器上,直接通过浏览器就 ...

  4. springboot 模板

    参考:https://blog.csdn.net/wangb_java/article/details/71775637

  5. springboot的war和jar包

    本篇和大家分享的是通过maven对springboot中打war包和jar包:war通常来说生成后直接放到tomcat的webapps下面就行,tomcat配置自动解压war,而jar一般通过命令行部 ...

  6. SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1

    在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...

  7. 带着萌新看springboot源码02

    上一节讲到先创建maven项目,然后导入依赖,主配置类等步骤,现在来个快速创建一个springboot模板,不需要向上节那么繁琐. 1.快速创建springboot应用 IDEA---->fil ...

  8. 学习springboot

    一般而言,写个Javaweb应用搭建环境都可能要几十分钟,下载个tomcat服务器,再加上各种xml配置等等,很烦躁,而且每个web应用的配置还差不多,都是什么web.xml,application. ...

  9. SpringBoot之静态资源放行

    为了提高开发效率,编写对应的代码生成器.代码生成器主要有两个方面,一个是在线Web,另外一个是运行某个类. 使用的技术是SpringBoot+MyBatis-Plus+MySQL+JDK8. 在编写在 ...

随机推荐

  1. 转化欧拉回路(难题)-UESTC-1957励志的猴子

    励志的猴子 Time Limit: 1000 MS     Memory Limit: 256 MB Submit Status 上图是2013南京亚青会吉祥物圆圆.自从它诞生以来,它就被吐槽为最丑吉 ...

  2. CentOS7.2下PXE+kickstart自动化安装系统

    一.实验环境 操作系统:CentOS Linux release 7.2.1511 (Core) 网卡地址:192.168.100.147/24 光盘镜像:CentOS-7-x86_64-Minima ...

  3. 【CSS】323- 深度解析 CSS 中的“浮动”

    对于浮动这篇文章解析的狠透彻 ~ 写在最前 习惯性去谷歌翻译看了看 float 的解释: 其中有一句这样写的: she relaxed, floating gently in the water 瞬间 ...

  4. pipelineDB学习笔记-3. Continuous Transforms (连续转换)

    以下内容为本人根据pipelineDB官网内容进行翻译,如有不妥之处请指正,谢谢大家 Continuous Transforms (连续转换) 一.定义: Continuous Transforms ...

  5. pcntl_signal(): Error assigning signal

    错误原因:SIGSTOP(19)和SIGKILL(6)两个信号不能使用,进程间通信换成其他信号量就好了.

  6. 如何用Postman做接口测试

    postman介绍&测试准备: postman介绍:postman是一个开源的接口测试工具,无论是做单个接口的测试还是整套测试脚本的拨测都非常方便. 前期准备:测试前,需要安装好postman ...

  7. CCF-CSP题解 201803-3 URL映射

    题目要求写一个简易的URL规则和URL地址匹配的程序. 说说我的思路. 将URL规则和地址都截成片段用结构体\(<type, str[]>\)存储.对于URL规则,\(type\)为0代表 ...

  8. 常见的 由于未调整服务器 ulimit 而引起的内存溢出问题

    原文内容来自于LZ(楼主)的印象笔记,如出现排版异常或图片丢失等问题,可查看当前链接:https://app.yinxiang.com/shard/s17/nl/19391737/e3bb62c9-9 ...

  9. PyTorch-网络的创建,预训练模型的加载

    本文是PyTorch使用过程中的的一些总结,有以下内容: 构建网络模型的方法 网络层的遍历 各层参数的遍历 模型的保存与加载 从预训练模型为网络参数赋值 主要涉及到以下函数的使用 add_module ...

  10. 前后端分离及Element的使用

    1. 前后端分离 1.1 什么是前后端分离 在传统的web应用开发中,大多数的程序员会将浏览器作为前后端的分界线.将浏览器中为用户进行页面展示的部分称之为前端,而将运行在服务器,为前端提供业务逻辑和数 ...