thymeleaf引入公共css、js等

网上搜索相关东西有多种实现方式,但是我觉得用起来不舒服。

找了半天都是这种方式,这种也不是不行就是用的很奇怪。这种会移动你head外的一些标签到head内,比如head标签外有script会被复制一份到head标签内。我不喜欢这种。

<head th:fragment="common_header(title,links,scripts)"></head>

最后采用方式:
下面这种方式相当于直接在你页面区域用公共模板里的内容替换掉标签。

模板

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<th:block th:fragment="common_header">
    <!-- Common styles -->
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="shortcut icon" th:href="@{/favicon.ico}" type="image/x-icon" />
    <link rel="stylesheet" th:href="@{/resource/plugin/weAdmin/static/css/font.css}">
    <link rel="stylesheet" th:href="@{/resource/plugin/weAdmin/static/css/weadmin.css}">
</th:block>

代码引公共js
<th:block th:fragment="common_js">
	<!-- Common scripts -->
    <script type="text/javascript" th:src="@{/resource/plugin/weAdmin/lib/layui/layui.js}" charset="utf-8"></script>
</th:block>

代码引用公共head

<head>
<title>haha</title>
<th:block th:replace="common::common_header" />
<link rel="stylesheet" th:href="@{/demo.css}" type="text/css" />
</head>
<th:block th:replace="common::common_js" />
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值