zoukankan      html  css  js  c++  java
  • 项目thymeleaf

    官方文档:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls

     

    定义和引用片段

    在我们的模板中,我们经常需要包含其他模板中的部分,页脚,标题,菜单等部分......

    为了做到这一点,Thymeleaf需要我们定义这些部分,“片段”,以便包含,这可以使用th:fragment属性来完成

    假设我们要在所有杂货页面上添加标准版权页脚,因此我们创建一个/WEB-INF/templates/footer.html包含以下代码文件:

    <!DOCTYPE html>
    <!DOCTYPE html>
    
    <html xmlns:th="http://www.thymeleaf.org">
    
      <body>
      
        <div th:fragment="copy">
          &copy; 2011 The Good Thymes Virtual Grocery
        </div>
      
      </body>
      
    </html>

    上面的代码定义了一个名为的片段copy,我们可以使用其中一个th:insertth:replace属性轻松地在我们的主页中包含这些片段(并且th:include,尽管自Thymeleaf 3.0以来不再推荐使用它):

    <body>
    
      ...
    
      <div th:insert="footer :: copy"></div>
      
    </body>

    4.4链接URL

    由于它们的重要性,URL是Web应用程序模板中的一等公民,而Thymeleaf标准方言具有特殊的语法,@语法:@{...}

    <!-- Will produce 'http://localhost:8080/gtvg/order/details?orderId=3' (plus rewriting) -->
    <a href="details.html" 
       th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a>
    
    <!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) -->
    <a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
    
    <!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
    <a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>

    这里要注意的一些事情:

    • th:href是一个修饰符属性:一旦处理,它将计算要使用的链接URL并将该值设置href<a>标记属性
    • 我们被允许使用表达式来指定URL参数(如您所见orderId=${o.id})。还将自动执行所需的URL参数编码操作。
    • 如果需要几个参数,这些参数将用逗号分隔: @{/order/process(execId=${execId},execType='FAST')}
    • URL路径中也允许使用变量模板: @{/order/{orderId}/details(orderId=${orderId})}
    • /(例如:)开头的相对URL /order/details将自动以应用程序上下文名称为前缀。

    内联

    一.文本内联

    [[…]]之间的表达式在Thymeleaf被认为是内联表达式,在其中您可以使用任何类型的表达式,也会有效th:text属性。

    <p>Hello, [[${session.user.name}]]!</p>

    等同于:

    <p>Hello, <span th:text="${session.user.name}">Sebastian</span>!</p>

    为了让内联工作,我们必须激活它使用th:inline 属性,它有三个可能的值或模式(textjavascript 和 none)。让我们试着文本:

    <p th:inline="text">Hello, [[${session.user.name}]]!</p>

    如果不使用th:inline="text",则会被当做字符串显示。th:inline="javascript"表示能在js中使用[ [] ]取值。

    标签的th:inline不需要包含内联的一个表达式,任何父标签都行,例如如下也是可以的:

    复制代码
    <body th:inline="text">
    
       ...
    
       <p>Hello, [[${session.user.name}]]!</p>
    
       ...
    
    </body>
    复制代码

    二.脚本内联

      Thymeleaf提供一系列的“脚本”的内联模式功能,这样你就可以将你的数据在脚本中创建一些脚本语言。

      我们可以做的第一件事,写脚本内联表达式的值到我们的脚本。

    <script th:inline="javascript">
        var user = [[${user.username}]];
          alert(user);
    </script>
    <script th:inline="javascript">
          var msg  = 'Hello, ' + [[${user.username}]];
          alert(msg);
    </script>

      只要加入th:inline="javascript"在js代码中才能使用[ [] ]

  • 相关阅读:
    xml/xslt常用转义字符
    用ScriptManager实现Web服务的异步调用
    声明静态方法和实例方法的原则
    http request header 中的host行的作用
    获取客户端数据
    HTTP 处理程序(HttpHandlers)
    display与visibility
    会话管理
    ASP.NET 管道
    HttpContext, HttpRequest, HttpResponse
  • 原文地址:https://www.cnblogs.com/twoheads/p/10277925.html
Copyright © 2011-2022 走看看