×

springboot+thymeleaf+thymeleaf-layout+admin-lte配置后台admin管理以及解决layout:fragment不起作用

我的笔记 我的笔记 发表于2019-02-20 15:20:31 浏览6529 评论77

77人参与发表评论

问题:

    smart-web-im在搭建后台时,使用的前端框架是adminlte,这个框架是单页面框架,也就是一个页面对应一个url,这就导致左侧的菜单栏和顶部共用的部分会出现重复使用的代码。百度找了一下解决方案,因为我用的thymeleaf模板,发现了一个thymeleaf-layout这个东西,觉得它能解决我的问题。。

解决办法:

    首先:admin.html我这个文件在WEB-INF/templates/admin/admin.html

<!DOCTYPE html><!DOCTYPE <html lang="en" xmlns="http://www.w3.org/1999/xhtml"
 xmlns:th="http://www.thymeleaf.org"
 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
 xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<head th:include="common/head::head('Smart&nbsp;WEB-IM后台管理')">

</head>
<body class="hold-transition skin-blue-light sidebar-mini">
    <div class="wrapper">
    <!-- Main Header -->
 <header class="main-header">

        <!-- Logo -->
 <a href="#" class="logo">
            <!-- mini logo for sidebar mini 50x50 pixels -->
 <span class="logo-mini"><b>S&nbsp;</b>W-I</span>
            <!-- logo for regular state and mobile devices -->
 <span class="logo-lg"><b>Smart&nbsp;</b>WEB-IM</span>
        </a>

        <!-- Header Navbar -->
 <nav class="navbar navbar-static-top" role="navigation">
            <!-- Sidebar toggle button-->
 <a href="#" class="sidebar-toggle" data-toggle="push-menu" role="button">
                <span class="sr-only">Toggle navigation</span>
            </a>
            <!-- Navbar Right Menu -->
 <div class="navbar-custom-menu">
                <ul class="nav navbar-nav">
                    <!-- Messages: style can be found in dropdown.less-->
 <li class="dropdown messages-menu">
                        <!-- Menu toggle button -->
 <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                            <i class="fa fa-envelope-o"></i>
                            <span class="label label-success">4</span>
                        </a>
                        <ul class="dropdown-menu">
                            <li class="header">你有 4 条信息!</li>
                            <li>
                                <!-- inner menu: contains the messages -->
 <ul class="menu">
                                    <li><!-- start message -->
 <a href="#">
                                            <div class="pull-left">
                                                <!-- User Image -->
 <img th:src="@{/static/lte/dist/img/user2-160x160.jpg}" class="img-circle" alt="User Image">
                                            </div>
                                            <!-- Message title and timestamp -->
 <h4>
                                                发件人
 <small><i class="fa fa-clock-o"></i> 5 分钟前</small>
                                            </h4>
                                            <!-- The message -->
 <p>主题主题主题主题主题主题主题?</p>
                                        </a>
                                    </li>
                                    <!-- end message -->
 </ul>
                                <!-- /.menu -->
 </li>
                            <li class="footer"><a href="#">查看所有信息</a></li>
                        </ul>
                    </li>
                    <!-- /.messages-menu -->

                    <!-- Notifications Menu -->
 <li class="dropdown notifications-menu">
                        <!-- Menu toggle button -->
 <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                            <i class="fa fa-bell-o"></i>
                            <span class="label label-warning">10</span>
                        </a>
                        <ul class="dropdown-menu">
                            <li class="header">你有 10 条通知!</li>
                            <li>
                                <!-- Inner Menu: contains the notifications -->
 <ul class="menu">
                                    <li><!-- start notification -->
 <a href="#">
                                            <i class="fa fa-users text-aqua"></i> 5 个会员加入。
 </a>
                                    </li>
                                    <!-- end notification -->
 </ul>
                            </li>
                            <li class="footer"><a href="#">查看所有通知</a></li>
                        </ul>
                    </li>
                    <!-- User Account Menu -->
 <li class="dropdown user user-menu">
                        <!-- Menu Toggle Button -->
 <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                            <!-- The user image in the navbar-->
 <img th:src="@{/static/lte/dist/img/user2-160x160.jpg}" class="user-image" alt="User Image">
                            <!-- hidden-xs hides the username on small devices so only the image appears. -->
 <span class="hidden-xs">管理员</span>
                        </a>
                        <ul class="dropdown-menu">
                            <!-- The user image in the menu -->
 <li class="user-header">
                                <img th:src="@{/static/lte/dist/img/user2-160x160.jpg}" class="img-circle" alt="User Image">

                                <p>
                                    管理员 - 职位
 <small>2018-01-01</small>
                                </p>
                            </li>
                            <!-- Menu Body -->
 <li class="user-body">
                                <div class="row">
                                    <div class="col-xs-4 text-center">
                                        <a href="#">备用1</a>
                                    </div>
                                    <div class="col-xs-4 text-center">
                                        <a href="#">备用2</a>
                                    </div>
                                    <div class="col-xs-4 text-center">
                                        <a href="#">备用3</a>
                                    </div>
                                </div>
                                <!-- /.row -->
 </li>
                            <!-- Menu Footer-->
 <li class="user-footer">
                                <div class="pull-left">
                                    <a href="#" class="btn btn-default btn-flat">个人信息</a>
                                </div>
                                <div class="pull-right">
                                    <a th:href="@{/signOut}" class="btn btn-default btn-flat">退出</a>
                                </div>
                            </li>
                        </ul>
                    </li>
                    <!-- Control Sidebar Toggle Button -->
 <li>
                        <a href="#" data-toggle="control-sidebar"><i class="fa fa-gears"></i></a>
                    </li>
                </ul>
            </div>
        </nav>
    </header>
    <!-- Left side column. contains the logo and sidebar -->
 <aside class="main-sidebar">

        <!-- sidebar: style can be found in sidebar.less -->
 <section class="sidebar">

            <!-- Sidebar user panel (optional) -->
 <div class="user-panel">
                <div class="pull-left image">
                    <img th:src="@{/static/lte/dist/img/user2-160x160.jpg}" class="img-circle" alt="User Image">
                </div>
                <div class="pull-left info">
                    <p>管理员</p>
                    <!-- Status -->
 <a href="#"><i class="fa fa-circle text-success"></i> 在线</a>
                </div>
            </div>

            <!-- search form (Optional) -->
 <form action="#" method="get" class="sidebar-form">
                <div class="input-group">
                    <input type="text" name="q" class="form-control" placeholder="搜索...">
                    <span class="input-group-btn">
              <button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
              </button>
            </span>
                </div>
            </form>
            <!-- /.search form -->

            <!-- Sidebar Menu -->
 <ul class="sidebar-menu" data-widget="tree" th:each="permission:${session.permissions}">
                <li><a th:href="${permission.url}"><i th:class="${permission.icon}"></i> <span th:text="${permission.permissionName}"></span></a></li>
                <!--<li>菜单</li>
                &lt;!&ndash; Optionally, you can add icons to the links &ndash;&gt;
                <li><a href="#"><i class="fa fa-link"></i> <span>菜单一</span></a></li>
                <li><a href="#"><i class="fa fa-link"></i> <span>链接1</span></a></li>
                <li>
                    <a href="#"><i class="fa fa-link"></i> <span>菜单2</span>
                        <span>
                <i class="fa fa-angle-left pull-right"></i>
              </span>
                    </a>
                    <ul>
                        <li><a href="#">链接1</a></li>
                        <li><a href="#">链接1</a></li>
                    </ul>
                </li>-->
 </ul>
            <!-- /.sidebar-menu -->
 </section>
        <!-- /.sidebar -->
 </aside>

    <!-- Content Wrapper. Contains page content -->
 <div class="content-wrapper" layout:fragment="contentWrapper">
        <!-- Content Header (Page header) -->
 <section class="content-header">
            <h1>
                首页
 <small>欢迎来到Smart&nbsp;Web-IM后台管理系统</small>
            </h1>
            <ol class="breadcrumb">
                <li  class="active"><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
            </ol>
        </section>

        <!-- Main content -->
 <section class="content container-fluid">

            <!--------------------------
              | Your Page Content Here |
              -------------------------->

 </section>
        <!-- /.content -->
 </div>
    <!-- /.content-wrapper -->

    <!-- Main Footer -->
 <footer class="main-footer">
        <!-- To the right -->
 <div class="pull-right hidden-xs">
            未经作者同意,禁止传播本程序!
 </div>
        <!-- Default to the left -->
 <strong>Copyright &copy; 2018 <a href="https://www.javagoing.com">付亚东</a>.</strong> 版权所有.
    </footer>

    <!-- Control Sidebar -->
 <aside class="control-sidebar control-sidebar-dark">
        <!-- Create the tabs -->
 <ul class="nav nav-tabs nav-justified control-sidebar-tabs">
            <li class="active"><a href="#control-sidebar-home-tab" data-toggle="tab"><i class="fa fa-home"></i></a></li>
            <li><a href="#control-sidebar-settings-tab" data-toggle="tab"><i class="fa fa-gears"></i></a></li>
        </ul>
        <!-- Tab panes -->
 <div class="tab-content">
            <!-- Home tab content -->
 <div class="tab-pane active" id="control-sidebar-home-tab">
                <h3 class="control-sidebar-heading">近期活动</h3>
                <ul class="control-sidebar-menu">
                    <li>
                        <a href="javascript:;">
                            <i class="menu-icon fa fa-birthday-cake bg-red"></i>

                            <div class="menu-info">
                                <h4 class="control-sidebar-subheading">某某某生日</h4>

                                <p>2018年11月11日</p>
                            </div>
                        </a>
                    </li>
                </ul>
                <!-- /.control-sidebar-menu -->

 <h3 class="control-sidebar-heading">任务进展</h3>
                <ul class="control-sidebar-menu">
                    <li>
                        <a href="javascript:;">
                            <h4 class="control-sidebar-subheading">
                                某某任务。。
 <span class="pull-right-container">
                    <span class="label label-danger pull-right">70%</span>
                  </span>
                            </h4>

                            <div class="progress progress-xxs">
                                <div class="progress-bar progress-bar-danger" style="width: 70%"></div>
                            </div>
                        </a>
                    </li>
                </ul>
                <!-- /.control-sidebar-menu -->

 </div>
            <!-- /.tab-pane -->
            <!-- Stats tab content -->
 <div class="tab-pane" id="control-sidebar-stats-tab">Stats Tab Content</div>
            <!-- /.tab-pane -->
            <!-- Settings tab content -->
 <div class="tab-pane" id="control-sidebar-settings-tab">
                <form method="post">
                    <h3 class="control-sidebar-heading">常规设置</h3>

                    <div class="form-group">
                        <label class="control-sidebar-subheading">
                            某设置
 <input type="checkbox" class="pull-right" checked>
                        </label>

                        <p>
                            有关此常规设置选项的一些信息
 </p>
                    </div>
                    <!-- /.form-group -->
 </form>
                <h4 class="control-sidebar-heading">皮肤</h4>
                <ul class="list-unstyled clearfix">
                    <li style="float:left; width: 33.33333%; padding: 5px;">
                        <a href="javascript:void(0)" data-skin="skin-blue" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)" class="clearfix full-opacity-hover">
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 7px; background: #367fa9"></span>
                                <span class="bg-light-blue" style="display:block; width: 80%; float: left; height: 7px;"></span>
                            </div>
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 20px; background: #222d32"></span>
                                <span style="display:block; width: 80%; float: left; height: 20px; background: #f4f5f7"></span>
                            </div>
                        </a>
                        <p class="text-center no-margin">蓝黑</p>
                    </li>
                    <li style="float:left; width: 33.33333%; padding: 5px;">
                        <a href="javascript:void(0)" data-skin="skin-black" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)" class="clearfix full-opacity-hover">
                            <div style="box-shadow: 0 0 2px rgba(0,0,0,0.1)" class="clearfix">
                                <span style="display:block; width: 20%; float: left; height: 7px; background: #fefefe"></span>
                                <span style="display:block; width: 80%; float: left; height: 7px; background: #fefefe"></span>
                            </div>
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 20px; background: #222"></span>
                                <span style="display:block; width: 80%; float: left; height: 20px; background: #f4f5f7"></span>
                            </div>
                        </a>
                        <p class="text-center no-margin">白黑</p>
                    </li>
                    <li style="float:left; width: 33.33333%; padding: 5px;">
                        <a href="javascript:void(0)" data-skin="skin-purple" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)" class="clearfix full-opacity-hover">
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 7px;" class="bg-purple-active"></span>
                                <span class="bg-purple" style="display:block; width: 80%; float: left; height: 7px;"></span>
                            </div>
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 20px; background: #222d32"></span>
                                <span style="display:block; width: 80%; float: left; height: 20px; background: #f4f5f7"></span>
                            </div>
                        </a>
                        <p class="text-center no-margin">紫黑</p></li>
                    <li style="float:left; width: 33.33333%; padding: 5px;">
                        <a href="javascript:void(0)" data-skin="skin-green" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)"class="clearfix full-opacity-hover">
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 7px;" class="bg-green-active"></span>
                                <span class="bg-green" style="display:block; width: 80%; float: left; height: 7px;"></span>
                            </div>
                            <div><span style="display:block; width: 20%; float: left; height: 20px; background: #222d32"></span>
                                <span style="display:block; width: 80%; float: left; height: 20px; background: #f4f5f7"></span>
                            </div>
                        </a>
                        <p class="text-center no-margin">绿黑</p></li>
                    <li style="float:left; width: 33.33333%; padding: 5px;">
                        <a href="javascript:void(0)" data-skin="skin-red" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)" class="clearfix full-opacity-hover">
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 7px;"  class="bg-red-active"></span>
                                <span class="bg-red" style="display:block; width: 80%; float: left; height: 7px;"></span>
                            </div>
                            <div><span style="display:block; width: 20%; float: left; height: 20px; background: #222d32"></span>
                                <span style="display:block; width: 80%; float: left; height: 20px; background: #f4f5f7"></span>
                            </div>
                        </a>
                        <p class="text-center no-margin">红色</p></li>
                    <li style="float:left; width: 33.33333%; padding: 5px;">
                        <a href="javascript:void(0)" data-skin="skin-yellow" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)" class="clearfix full-opacity-hover">
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 7px;"  class="bg-yellow-active"></span>
                                <span class="bg-yellow"  style="display:block; width: 80%; float: left; height: 7px;"></span>
                            </div>
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 20px; background: #222d32"></span>
                                <span style="display:block; width: 80%; float: left; height: 20px; background: #f4f5f7"></span>
                            </div>
                        </a>
                        <p class="text-center no-margin">黄色</p></li>
                    <li style="float:left; width: 33.33333%; padding: 5px;">
                        <a href="javascript:void(0)" data-skin="skin-blue-light" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)" class="clearfix full-opacity-hover">
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 7px; background: #367fa9"></span>
                                <span class="bg-light-blue" style="display:block; width: 80%; float: left; height: 7px;"></span>
                            </div>
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 20px; background: #f9fafc"></span>
                                <span style="display:block; width: 80%; float: left; height: 20px; background: #f4f5f7"></span>
                            </div>
                        </a>
                        <p class="text-center no-margin" style="font-size: 12px">蓝白</p></li>
                    <li style="float:left; width: 33.33333%; padding: 5px;">
                        <a href="javascript:void(0)" data-skin="skin-black-light" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)" class="clearfix full-opacity-hover">
                            <div style="box-shadow: 0 0 2px rgba(0,0,0,0.1)" class="clearfix">
                                <span style="display:block; width: 20%; float: left; height: 7px; background: #fefefe"></span>
                                <span style="display:block; width: 80%; float: left; height: 7px; background: #fefefe"></span>
                            </div>
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 20px; background: #f9fafc"></span>
                                <span style="display:block; width: 80%; float: left; height: 20px; background: #f4f5f7"></span>
                            </div>
                        </a>
                        <p class="text-center no-margin" style="font-size: 12px">全白</p></li>
                    <li style="float:left; width: 33.33333%; padding: 5px;">
                        <a href="javascript:void(0)" data-skin="skin-purple-light" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)" class="clearfix full-opacity-hover">
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 7px;" class="bg-purple-active"></span>
                                <span class="bg-purple" style="display:block; width: 80%; float: left; height: 7px;"></span>
                            </div>
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 20px; background: #f9fafc"></span>
                                <span style="display:block; width: 80%; float: left; height: 20px; background: #f4f5f7"></span>
                            </div>
                        </a>
                        <p class="text-center no-margin" style="font-size: 12px">紫白</p></li>
                    <li style="float:left; width: 33.33333%; padding: 5px;">
                        <a href="javascript:void(0)" data-skin="skin-green-light" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)" class="clearfix full-opacity-hover">
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 7px;" class="bg-green-active"></span>
                                <span class="bg-green" style="display:block; width: 80%; float: left; height: 7px;"></span>
                            </div>
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 20px; background: #f9fafc"></span>
                                <span style="display:block; width: 80%; float: left; height: 20px; background: #f4f5f7"></span>
                            </div>
                        </a>
                        <p class="text-center no-margin" style="font-size: 12px">绿白</p></li>
                    <li style="float:left; width: 33.33333%; padding: 5px;">
                        <a href="javascript:void(0)" data-skin="skin-red-light" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)" class="clearfix full-opacity-hover">
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 7px;" class="bg-red-active"></span>
                                <span class="bg-red" style="display:block; width: 80%; float: left; height: 7px;"></span>
                            </div>
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 20px; background: #f9fafc"></span>
                                <span style="display:block; width: 80%; float: left; height: 20px; background: #f4f5f7"></span>
                            </div>
                        </a>
                        <p class="text-center no-margin" style="font-size: 12px">红白</p></li>
                    <li style="float:left; width: 33.33333%; padding: 5px;">
                        <a href="javascript:void(0)" data-skin="skin-yellow-light" style="display: block; box-shadow: 0 0 3px rgba(0,0,0,0.4)" class="clearfix full-opacity-hover">
                            <div>
                                <span style="display:block; width: 20%; float: left; height: 7px;" class="bg-yellow-active"></span>
                                <span class="bg-yellow" style="display:block; width: 80%; float: left; height: 7px;"></span>
                            </div>
                            <div><span style="display:block; width: 20%; float: left; height: 20px; background: #f9fafc"></span>
                                <span style="display:block; width: 80%; float: left; height: 20px; background: #f4f5f7"></span>
                            </div>
                        </a>
                        <p class="text-center no-margin" style="font-size: 12px">黄白</p></li>
                </ul>
            </div>
            <!-- /.tab-pane -->
 </div>
    </aside>
    <!-- /.control-sidebar -->
    <!-- Add the sidebar's background. This div must be placed
    immediately after the control sidebar -->
 <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->
<div th:include="common/food::food"></div>
<!-- 样例 -->
<script th:src="@{/static/lte/dist/js/demo.js}"></script>
</body>
</html>

重点是这一句

<div layout:fragment="contentWrapper">

第四句

<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
 xmlns:th="http://www.thymeleaf.org"
 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
 xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">

建了一个test.html

<!DOCTYPE html>
<html lang="en" xmlns:layout="http://www.w3.org/1999/xhtml"  xmlns:th="http://www.thymeleaf.org"
 layout:decorator="admin/admin">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        input[type="text"]:focus{

            background-color: grey;
        }

        input#userid {

            margin-left: 20px;
        }

        #add{

            padding: 10px 40px;
            border: 5px gray solid;
            box-shadow: 10px 10px 5px aquamarine;
            font-family: 华文行楷;
            font-size: 14px;
            border-radius: 20px;
        }

        input{

            padding: 10px;
        }


    </style>
</head>

<body>
<!--<div layout:fragment="content">-->

<div layout:fragment="contentWrapper" >
    <div id="add">
        <form action="#" th:action="@{/user/add}"  method="post">
            <label>id</label><input type="text" id="userid"/><br>
            <label>姓名</label><input type="text" /><br>
            <label>密码</label><input type="text" /><br>
            <label>昵称</label><input type="text" /><br>
            <input type="submit" value="添加"/>
        </form>
    </div>
</div>
</div>
</body>
</html>

重点是:第二行,是我的公共部分页面的地址

<html lang="en" xmlns:layout="http://www.w3.org/1999/xhtml"  xmlns:th="http://www.thymeleaf.org"
 layout:decorator="admin/admin">

需要被替换的地方

<div layout:fragment="contentWrapper" >



tips:

    在配置好上边的内容后,发现不起作用。查找官网文档发现还要配置启动类

TemplateEngine templateEngine = new TemplateEngine();
templateEngine.addDialect(new LayoutDialect());

image.png

我的笔记博客版权我的笔记博客版权