Spring boot(17) thymeleaf 使用类似于include的方式

1.建立模板

secure_template.html

<!DOCTYPE html>
<!-- 仅作为模板使用,服务端不要直接返回 -->
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>权限center</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" type="text/css" th:href="@{/mg/css/table.css}" />
        <link rel="stylesheet" type="text/css" th:href="@{/mg/css/public.css}" />
        <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
        <link href="//cdn.bootcss.com/bootstrap/3.0.1/css/bootstrap.min.css" rel="stylesheet"/>
        <script src="//cdn.bootcss.com/bootstrap/3.0.1/js/bootstrap.js"></script>
    </head>
    <body>
        <div id="wrapper">
        <nav class="navbar navbar-default navbar-inverse navbar-fixed-top" role="navigation">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span
                        class="icon-bar"></span><span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#"><span class="glyphicon glyphicon-home">Home</span></a>
            </div>
            <div class="collapse navbar-collapse"
                id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="/mg/sercure_roles">角色列表</a></li>
                    <li><a href="/mg/sercure_accounts">用户列表</a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">监控<strong class="caret"></strong></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">用户</a></li>
                            <li><a href="#">角色</a></li>
                            <li class="divider"></li>
                            <li><a href="#">日志监控</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </nav>
        <div style="margin-top: 5%" th:include="this :: content"></div>
        <div id="footer">
            @ gaotianyue
        </div>
        </div>
    </body>
</html>

其中最重要的是th:include=”this :: content”

2使用方法

<!DOCTYPE html>
<html lang="zh-CN"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="secure_template">
<head>
<title>角色管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<link rel="stylesheet" type="text/css" th:href="@{/mg/css/table.css}" />
<link rel="stylesheet" type="text/css" th:href="@{/mg/css/public.css}" />
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<link href="//cdn.bootcss.com/bootstrap/3.0.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="//cdn.bootcss.com/bootstrap/3.0.1/js/bootstrap.js"></script>
    <body>
        <div th:fragment="content" >
            <!-- 自己的内容 -->
            <div class="container"> 
            </div>
        </div>
    </body>
</html>

其中最重要的是 layout:decorator=”secure_template”
div th:fragment=”content”

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
## springboot整合thymeleaf ### 1. 导入起步依赖 ```xml org.springframework.boot spring-boot-starter-thymeleaf ``` ### 2. 更改引入版本 ```xml 3.0.2.RELEASE 2.1.1 ``` > 1. springboot自带的thymeleaf依赖为2.1.3版本,使用thymeleaf-layout-dialect版本为2以下版本。 > 2. 使用3或3以上的thymeleaf时,需要thymeleaf-layout-dialect的版本为2或以上。 > 3. 锁定thymeleaf版本时不能使用thymeleaf.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-thymeleaf.version标签来锁定版本。 ### 3. 配置文件配置 ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false ``` > spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false ## 语法 ### 1. 替换标签体内容 ```html 显示欢迎 显示欢迎 ``` ### 2. 替换属性 ```html 显示欢迎 ``` ### 3. 在表达式中访问属性域 ```html 访问属性域 访问请求域 方式一 访问请求域 方式二 访问Session域 访Session域 方式一 访问Application域 方式一 ``` ### 4. 解析url地址 ```html 解析URL地址,获取ContextPath的值 @{}是把ContextPath的值附加到指定的地址前 @{}是把ContextPath的值附加到指定的地址前 ``` ### 5. 直接执行表达式 ```html 直接执行表达式 无转义效果 : [[${attrRequestScope}]] 有转义效果 : [(${attrRequestScope})] ``` ### 6. 分支与迭代 #### 1. if 判断 ```html if判断字符串是否为空 <p th
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值