Thymeleaf利用layout.html文件生成页面布局框架

1.layout.html文件 生成布局

<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<head>

    <meta charset="utf-8" />
    <title>首页</title>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <!-- bootstrap -->
    <link rel="stylesheet" href="../../static/bootstrap/3.3.5/css/bootstrap.min.css" 
        th:href="@{/bootstrap/3.3.5/css/bootstrap.min.css}" />

    <style type="text/css"> 
        body{
            font-family:微软雅黑;
            font-size:14px;
        }
    </style>    

</head>

<body>
    <div th:fragment="topnav">
    <nav class="navbar navbar-default" style="background: none repeat scroll 0 0 #394755;color:#fff;height:60px;">
    <div class="container-fluid">
      <!-- Brand and toggle get grouped for better mobile display -->
       <div class="navbar-header" style="margin-right:20%;">
          <a class="navbar-brand" href="/mainframe" style="color:#fff;">测试&系统</a>
       </div>


        <!-- 退出 -->       
        <a class="navbar-brand" href="#" style="color:#fff;float:right;" onclick="javascript:safeLogoutFun();">退出</a>

       <!-- 欢迎语 -->
       <div class="navbar-brand" style="margin-right:30px;font-size:15px;float:right;color:#f5d313;">
                    欢迎您,<span th:text="${session.LoginUser.user_name}">亲爱的用户</span>  现在是
           <span id="time"></span>
             <script th:inline="javascript">
                  /*<![CDATA[*/
                    function setTime(){
                     var dt=new Date();
                     var arr_week=new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
                     var strWeek=arr_week[dt.getDay()];
                     var strHour=dt.getHours();
                     var strMinutes=dt.getMinutes();
                     var strSeconds=dt.getSeconds();
                     if (strMinutes<10) strMinutes="0"+strMinutes;
                     if (strSeconds<10) strSeconds="0"+strSeconds;
                     var strYear=dt.getFullYear()+"年";
                     var strMonth=(dt.getMonth()+1)+"月";
                     var strDay=dt.getDate()+"日";
                     var strTime=strHour+":"+strMinutes+":"+strSeconds;
                     time.innerHTML=strYear+strMonth+strDay+" "+strTime+"  "+strWeek;   
                    }
                    setInterval("setTime()",1000);


                    //20151013  安全退出
                    function safeLogoutFun(){
                        if(confirm("确定需要退出吗?")){
                            $.ajax({
                                type: "POST",
                                url: "safeLogout",
                                dataType: "json",
                                error:function(){
                                    alert("退出失败");
                                },
                                success: function(responseInfo)
                                {
                                    //
                                    if(responseInfo.status == 0){
                                         window.location.replace("index"); 
                                         //window.event.returnValue = false;//取消默认事件的处理
                                    }

                                    //
                                    if(responseInfo.status == 1){
                                        alert("退出失败");
                                    }
                                }
                            });//end ajax
                        }//end if confirm
                    }
                /*]]>*/
            </script> 
       </div>

     </div>
  </nav>
  </div> <!-- 导航条 -->

  <div layout:fragment="content"></div>

  <!-- put the scripts below -->
  <script src="../../static/jquery/2.1.4/jquery-2.1.4.min.js"
        th:src="@{/jquery/2.1.4/jquery-2.1.4.min.js}"></script>
  <script src="../../static/bootstrap/3.3.5/js/bootstrap.min.js"
        th:src="@{/bootstrap/3.3.5/js/bootstrap.min.js}"></script>
 <!-- 存放具体业务页面需要引入的javascript文件 -->
   <th:block layout:fragment = "bodyAssets">

   </th:block>
</body>

</html>

2.之后页面可套用此布局模式,重写<div layout:fragment = "content">元素,css文件和js文件均可继承

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
    layout:decorator="base/layout">
    <!-- layout文件路径-->

<head>
    <meta charset="utf-8" />
    <title>首页</title>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

</head>

<body>
  <div layout:fragment="content" style="width:96%;margin-left:auto;margin-right:auto;">
    <div style="font-size:18px;">
                 测试thymeleaf layout布局 模式套用
         js css 全部继承自layout
         content 因页面不同而不同
     </div>

     <div style="margin:20px;">
     </div>

     <div style="font-size:16px;margin:20px 0px;">
           <a href="/tools/editTable1" target="_blank">可编辑表格插件一测试</a>
     </div>
     <div style="font-size:16px;">
           <a href="/tools/editTable2" target="_blank">可编辑表格插件二测试</a>
     </div> 
  </div>
  <th:block layout:fragment = "bodyAssets">
     <script src = "../../static/js/security/mainframe.js"
            th:src="@{/js/security/mainframe.js}"></script>
 </th:block>    
</body>

</html>

将业务js放在th:block中

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值