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. 轻松构建基于 Serverless 架构的弹性高可用音视频处理系统

    前言 随着计算机技术和 Internet 的日新月异,视频点播技术因其良好的人机交互性和流媒体传输技术倍受教育.娱乐等行业青睐,而在当前, 云计算平台厂商的产品线不断成熟完善, 如果想要搭建视频点播类 ...

  2. Electron node integration enabled 设置

    解决办法 参考博客:https://blog.csdn.net/hwytree/article/details/103167175

  3. MYSQL-JDBC批量新增-更新-删除

    目录 1 概述 2 开启MYSQL服务端日志 3 深入MYSQL/JDBC批量插入 3.1 从一个例子出发 3.2 JDBC的批量插入操作 3.3 两个常被忽略的问题 3.5 误区 4 MYSQL/J ...

  4. 《Java练习题》习题集二

    编程合集: https://www.cnblogs.com/jssj/p/12002760.html Java总结:https://www.cnblogs.com/jssj/p/11146205.ht ...

  5. Intent知识详解

    Intent知识详解 一.什么是Intent 贴一个官方解释: An intent is an abstract description of an operation to be performed ...

  6. MyBatis进阶讲解+ssm集成

    1.sql映射器Mapper MyBatis基于动态代理机制,让我们无需再编写Dao的实现. 传统Dao接口,现在名称统一以Mapper结尾,还有我们映射器配置文件要和映射器在同一个包. 1.1使用映 ...

  7. SpringCloud+Eureka+Feign+Ribbon的简化搭建流程和CRUD练习

    作者:个人微信公众号:程序猿的月光宝盒 环境:win10--idea2019--jdk8 1.搭建Eureka服务模块 1.1 新建eureka服务模块(Sping Initializr) 取名为eu ...

  8. 基于Postman中的报错

    Postman中的报错: Could not get any response 错误 Could not get any response There was an error connecting ...

  9. spark-3.0 application 调度算法解析

    spark 各个版本的application 调度算法还是有这明显的不同之处的.从spark1.3.0 到 spark 1.6.1.spark2.0 到 现在最新的spark 3.0 ,调度算法有了一 ...

  10. API访问控制设计

    References ● OAuth 2.0 for native apps: https://datatracker.ietf.org/doc/rfc8252/ ● OAuth 2.0 for br ...