• Spring MVC : Java模板引擎 Thymeleaf (三)


    以下以构造一个表单開始,解说 Thymeleaf的使用方法。

    为了演示方便,还是以经典的注冊为例。

    这是Thymeleaf的form的形式,

       <form action="#" th:action="@{/register}" th:object="${person}" method="post">
        
        
        </form>

    action="#"是固定部分。由于action是由th:action指出。

    @符号是Thymeleaf对URL表达的方式。

    这是本文的第一个知识点。


    • URL表达式

    首先是绝对路径,

    <a th:href="@{http://www.baidu.com}">

    但最经常使用的肯定是相对路径。非常多朋友把上下文相对路径和server相对路径混为一谈,事实上它们是不同的。上下文相对路径都是以 / 开头的,比方是你的一个应用myapp部署在tomcat下。你能够用http://localhost:8080/myapp訪问它。这里的myapp就是上下文名称。

    这时。

    <a th:href="@{/login}">
    解析后就是,

    <a href="/myapp/login">

    server相对路径和它差别是,server相对路径不会如果你的资源在应用上下文内。(你可能部署多个应用),也就是它同意你訪问在同一个server的其它上下文。比方,

    <a th:href="@{~/other-app/hello.html}" >

    解析后就是,

    <a href="/other-app/showDetails.htm">

    当然另一种相对路径(相对协议的),实际上是绝对路径,

    <a th:href="@{<span style="font-family: Arial, Helvetica, sans-serif;">//code.jquery.com/jquery-1.10.2.js</span><span style="font-family: Arial, Helvetica, sans-serif;">}" ></span>

    解析后就是,

    <a href="//code.jquery.com/jquery-1.10.2.js">


    接下来是URL的加入參数问题,

    <a th:href="@{/order/details(id=3)}">

    解析后。

    <a href="/order/details?id=3">

    多个參数能够在()内用逗号隔开。

    以下的形式也是支持的,请细细体会,

    <a th:href="@{/order/{id}/details(id=3,action='show_all')}">
    解析后,

    <a href="/order/3/details?action=show_all">
    另一个概念,叫URL fragment,什么是URL Fragment呢?




    上面两张图基本说明了url fragment是什么了。


    <a th:href="@{/home#all_info(action='show')}">

    解析后,

    <a href="/home?

    action=show#all_info">



    下一个知识点是th:object,

    • th:object

    该属性在Thymeleaf非经常见。但form会强制要求你写这个。为了整合Spring。它对form里面的th:object规定例如以下:

    1. 必须是变量表达式(${...}),代表模型的名字,且不能向模型的属性导航,就是${a}合法。但${a.b}不合法
    2. form内不能有其它th:object。也就是HTML的表单不能嵌套
    你可能猜到,这个object就是向后台传递数据的。
    在stackoverflow有个提问。"send datas from html to controller in thymeleaf?",採纳的答案是这种:
    controller:
    @RequestMapping(value = "/processForm", method=RequestMethod.POST)
    public String processForm(@ModelAttribute(value="foo") Foo foo) {
      ...
    }

    html:
    <form action="#" th:action="@{/processForm}" th:object="${foo}" method="post">
      <input type="text" th:field="*{bar}" />
      <input type="submit" />
    </form>

    Foo.java
    public class Foo {
      private String bar;
    
      public String getBar() {
        return bar;
      }
    
      public void setBar(String bar) {
        this.bar = bar;
      }
    }

    看完之后,你肯定就能豁然开朗了。


    还有个知识点是 th:field。


    • th:field
    这个属性在Spring-mvc里非常重要,承担着绑定后台Bean属性的重任,这和JSP标签里的path非常像。
    对不同类型的input。th:field有些差异。

    这个之后再说。

    要记住的是,th:field必须是选择表达式(*{...})。

    最后,你可能对前面提到的表达式有疑问了。
    变量表达式 ${...}就是OGNL表达式。

    假设你使用springstandard。就是Spring表达式(SpEL)。

    给个样例你就明确了,
    <span th:text="${book.author.name}">

    选择表达式*{...}非常像变量表达式,不同在于,它运行的是前面选择的对象。

    <div th:object="${book}">
      ...
      <span th:text="*{title}">...</span>
      ...
    </div>
    前面选择了book,接下来就依照它求值。

    另一个表达式,前面没有出现。国际化表达式#{...},顾名思义是处理国际化的。
    <table>
      ...
      <th th:text="#{header.address.city}">...</th>
      <th th:text="#{header.address.country}">...</th>
      ...
    </table>

    国际化资源通常是在.properties文件定义。


  • 相关阅读:
    [转]Spring——jar包详解
    [转]Null value was assigned to a property of primitive type setter of"原因及解决方法
    [转]Spring3 MVC + jQuery easyUI 做的ajax版本用户管理
    [转]SpringMVC+Hibernate+Spring 简单的一个整合实例
    Spring MVC配置
    [转] 使用Spring MVC构建REST风格WEB应用
    NYOJ 467 中缀式变后缀式
    HBase源代码分析之HRegionServer上MemStore的flush处理流程(二)
    树中两个节点的最低公共祖先
    计算机图形学(一) 视频显示设备_1_CRT原理
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7001431.html
Copyright © 2020-2023  润新知