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. Selenium之ActionChains类、Keys类

    ActionChains类(鼠标操作)常用于模拟鼠标的行为,比如单击.双击.拖拽等行为. 一些常用的模拟鼠标的操作方法有: click(on_element=None)     --- 鼠标单击 do ...

  2. 【JS】332- 为什么我更喜欢对象而不是 switch 语句

    昨天偷懒了,欢迎点击关注???这样我就多更大的动力日更了- 正文从这里开始~~~ 最近(或者不是最近,这完全取决于您什么时候阅读这边文章),我正在跟我的团队伙伴讨论如何去处理这种需要根据不同的值去处理 ...

  3. Rabbitmq-单机安装

    Rabbitmq介绍   官网地址:https://www.rabbitmq.com RabbitMQ是一款在全球范围内使用非常广泛的开源消息队列中间件.它轻量级.易部署.并支持多种协议.它基于Erl ...

  4. 12个超好用的IntelliJ IDEA 插件!你用过几个?

    一.前言 IntelliJ IDEA 如果说IntelliJ IDEA是一款现代化智能开发工具的话,Eclipse则称得上是石器时代的东西了. 其实笔者也是一枚从Eclipse转IDEA的探索者,随着 ...

  5. 【朝花夕拾】Android多线程之(三)runOnUiThread篇——程序猿们的贴心小棉袄

    runOnUiThread()的使用以及原理实在是太简单了,简单到笔者开始都懒得单独开一篇文章来写它.当然这里说的简单,是针对对Handler比较熟悉的童鞋而言的.不过麻雀虽小,五脏俱全,runOnU ...

  6. GHOST CMS - Ghost Handlebars主题 Ghost Handlebars Themes

    Ghost Handlebars主题 Ghost Handlebars Themes Ghost主题层被设计为让开发人员和设计人员能够灵活地构建由Ghost平台支持的自定义发布 The Ghost t ...

  7. UWP 打开系统设置面板

    由于UWP各种权限管理的比较严格,所以在执行某一个特殊的操作之前,最好先申请一下相应的权限,以便告知用户你使用了这个权限,而且可以有效的避免App崩溃. 比如你想让用户手动打开麦克风权限,那么可以执行 ...

  8. Python面向对象-多重继承之MixIN

    以Animal类为例,假设要实现以下4种动物: Dog(狗).Bat(蝙蝠).Parrot(鹦鹉)和Ostrich(鸵鸟) 如果按照哺乳类和鸟类来区分的话,可以这样设计: Animal: |--Mam ...

  9. [译]C# 7系列,Part 4: Discards 弃元

    原文:https://blogs.msdn.microsoft.com/mazhou/2017/06/27/c-7-series-part-4-discards/ 有时我们想要忽略一个方法返回的值,特 ...

  10. 一文解读Docker (转)

    最初的2小时,你会爱上Docker,对原理和使用流程有个最基本的理解,避免满世界无头苍蝇式找资料.本人反对暴风骤雨式多管齐下狂轰滥炸的学习方式,提倡迭代学习法,就是先知道怎么玩,有个感性认识,再深入学 ...