html - 如何使用 thymeleaf 命名空间来包含页面的页眉和页脚而不使用任何模板解析器?

标签 html thymeleaf

我有一个登录页面,我在其中声明了包含通用模板,如下所示

“head”是common.html中header属性的片段名称,它包含所有常见的js和css文件。

当我访问该 URL 时,我发现页面上没有加载这些 css。这里需要注意的是,我没有使用任何模板解析类或引擎类,它是纯静态的。

谁能帮我解决这个问题吗?

这是登录.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:th="http://www.thymeleaf.org">
<head>
<meta name="loginPage" />
<meta th:remove="tag" th:include="../fragments/common :: [//head]"/>

</head>
<body  class="claro">
<div></div>
        <br />
          <div class="grid">
            <div class="col-1-1" style="text-align: center; margin: 0 auto;">
                <form class="form" t">
                    <fieldset>
                        <legend>Please login</legend>
                        <div class="col-1-1">
                            <div class="label">
                                User Name:
                            </div>
                            <div class="input">
                                <input data-dojo-type="dijit/form/TextBox" id="user_name" length="35" required="true" />
                            </div>
                        </div>
                        <div class="col-1-1">
                           <div class="label"> 
                                Password:
                           </div> 
                           <div class="input">
                            <input  id="password" name="password" data-dojo-type="dijit/form/TextBox"
                                type="password" required="required" maxlength="100" />
                           </div>
                         </div>
                           <div class="col-1-1">
                                <div class="module">
                                    <input type="submit" data-dojo-type="dijit.form.Button"
                                        id="save_msg_button" intermediateChanges="false"
                                        label="Login" iconClass="dijitNoIcon"></input>
                                </div>
                           </div>

                    </fieldset>
                </form>
            </div>
         </div>
</body>
</html>

这是 common.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head th:fragment="head">
<meta charset="utf-8" />
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>  
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>  
<![endif]-->

<!-- stylesheets -->
<link rel="stylesheet" th:href="@{/css/grid.css}" href=""
    type="text/css" />
<link rel="stylesheet" th:href="@{/css/app.css" href=""
    type="text/css" />
<link rel="stylesheet" th:href="@{/css/app.css" href=""
    type="text/css" />
<link rel="stylesheet" href="../../../cdn/js/dojo/dijit/themes/claro/document.css"
    type="text/css" />
<link rel="stylesheet" href="../../../cdn/js/dojo/dijit/themes/claro/claro.css"
    type="text/css" />
<link rel="stylesheet" href="../../../cdn/js/dojo/dijit/themes/claro/Gridx.css"
    type="text/css" />
<link rel="stylesheet" href="../../../cdn/js/dojo/dijit/themes/claro/Gridx_rtl.css"
    type="text/css" />
<link rel="stylesheet" href="../../../cdn/js/dojo/dijit/themes/claro/ExpandoPane.css"
    type="text/css" />



</head>
<body>
    <div>
        <span>Body goes here</span>
    </div>
</body>
</html>

最佳答案

如果我理解正确,您希望使用片段静态构建模板原型(prototype)。

当 Thymeleaf 模板用作静态原型(prototype)时,您将看不到使用 th:include/th:replace 包含的片段。

在我看来,你有两个选择:

  1. 创建一个静态原型(prototype)代码,该代码将在运行时删除,如下文件所示:https://github.com/thymeleaf/thymeleafexamples-layouts/blob/master/src/main/webapp/WEB-INF/views/home/homeNotSignedIn.html

  2. 使用 Thymol - 一个非官方 JavaScript 库,它是 Thymeleaf 标准片段包含功能的实现,为某些 Thymeleaf 属性(如 th:include 或 th:replace)提供静态支持。请参阅:http://sourceforge.net/u/jjbenson/wiki/thymol/

关于html - 如何使用 thymeleaf 命名空间来包含页面的页眉和页脚而不使用任何模板解析器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21067929/

相关文章:

html - 如何使 Bootstrap 卡片在卡片列中具有相同的高度?

html - 为什么我的 HTML5 矩形看起来放大了?

html - 带有按钮的CSS旋转木马不起作用

html - 如何通过css实现如下图形

java - Spring 中的表单处理

java - findbyid对象 Spring 启动和更新

css - .css 文件中的 Thymeleaf URL 表达式

html - Safari 14 - min()、max()、clamp() 不适用于 vw 和 px 值

java - thymeleaf 的可读性

spring-mvc - Thymeleaf:如何使用布局包含特定于页面的javascript?