springMVC整合thymeleaf

springMVC 整合thymeleaf

  1. 添加依賴

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n5" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit;">
    <dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.6.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring4</artifactId>
    <version>3.0.6.RELEASE</version>
    </dependency></pre>

  2. 配置文件

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n12" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit;">
    <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".html"/>
    <property name="templateMode" value="HTML"/>
    <property name="cacheable" value="false"/>
    </bean>

    <bean id="templateEngine"
    class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver"/>
    <property name="enableSpringELCompiler" value="true"/>
    </bean>

    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine"/>
    <property name="characterEncoding" value="utf-8"/>
    </bean></pre>

  3. 網頁申明

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" cid="n18" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit;"><!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org"></pre>

  4. 表達式符號

    1. "#"代表獲取對象從 messages bundle 也就是消息資源本地化文件

      • 在${}中使用時,代表引用方法

      • <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" cid="n39" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit;"><td th:text="${#dates.format(user.createTime,'yyyy-MM-dd')}">2012-12-10</td></pre>

    2. $ 表示從model里面獲取

      • #羡洛、$這2個可以一起用 比如#{'system.'+${model.id}} 這相當于 #{system.01}的資源本地化文件中的system.01內容

      • 表示從當前對象中讀取屬性

        <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" cid="n56" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit;"><div th:object="${session.user}">
        <p>Name: <span th:text="{firstName}">Sebastian</span>.</p>
        <p>Surname: <span th:text="
        {lastName}">Pepper</span>.</p>
        <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
        </div></pre>

    3. @{} 表示一個URL表達式

    4. ~{...} 表示引有頁面的表達式

  5. 常用th標簽

    • th:each="user: {users}" 相當于c:foreach 使用時候 奇偶行判斷 <tr th:each="user,status:{users}" th:class="{status.odd}?'odd':'even'" > 顯示行號 <td th:text="{status.index}+1">1</td>

    • th:href 重新生成url th:href="@{/list/details(id={user.id})} 生成:[http://domain.org/context/list/details?id=1](http://domain.org/context/list/details?id=1) 相對地址速警,使用"|"符號 th:href="|/list/detail?l={lid}|" 生成:http://domain.org/list/details?id=1 擴展 th:href="@{'/list/details/'+${user.id}} 生成 http://domain.org/context/list/details/1

    • th:text="{data}",將data的值替換該屬性所在標簽的body th:text="'my name is '+{user.name}"

    • th:with,定義變量 th:with="isEven=${prodStat.count}%2==0"础爬,定義多個變量可以用逗號分隔

    • th:attr贫奠,設置標簽屬性 多個屬性可以用逗號分隔,比如th:attr="src=@{/image/aa.jpg},title=#{logo}"

    • th:if or th:unless爆雹,條件判斷 支持布爾值撑刺,數字(非零為true)爸吮,字符,字符串等

    • th:switch铅忿,th:case剪决,選擇語句

      • th:case="*"表示default case

      • 示例

      • <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" cid="n181" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit;"><div th:switch="${user.role}">
        <p th:case="'admin'">User is an administrator</p>
        <p th:case="#{roles.manager}">User is a manager</p>
        <p th:case="*">User is some other thing</p>
        </div></pre>

      th:fragment,th:include,th:substituteby:fragment為片段標記 指定一個模板內一部分代碼為一個片段檀训,然后在其它的頁面中用th:include或

      th:substituteby進行包含柑潦。 片段格式 "templatename::fragname",指定模板內的指定片段 "templateName::[domselector]"峻凫,指定模板的dom selector渗鬼,被包含的模板內不需要th:fragment. ”templatename",包含整個模板 示例 th:include="footer::$(user.logined)?'logined':'notLogin'" 格式內可以為表達式 th:include和th:substituteby的區(qū)別 th:include 包含片段的內容到當前標簽內 th:substituteby 用整個片段(內容和上一層)替換當前標簽(不僅僅是標簽內容) th:object 獲取model中的對象

      ?

      <form action="#" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px;">th:field 獲取表單中綁定對象的字段 <input type="text" style="box-sizing: border-box !important; color: rgb(184, 191, 198); font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; margin: 0px; background-color: var(--bg-color); padding-left: 4px; border: 1px solid transparent;">
      th:errors 顯示頁面中包含的錯誤* 顯示username字段上的錯誤* 顯示所有錯誤 其它請參考:thymeleaf所有標簽屬性.xlsx和thymeleaf-spring.pdf</form>

      ?

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末荧琼,一起剝皮案震驚了整個濱河市譬胎,隨后出現的幾起案子,更是在濱河造成了極大的恐慌命锄,老刑警劉巖堰乔,帶你破解...
    沈念sama閱讀 221,406評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異脐恩,居然都是意外死亡镐侯,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 94,395評論 3 398
  • 文/潘曉璐 我一進店門驶冒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來苟翻,“玉大人,你說我怎么就攤上這事骗污〕缑ǎ” “怎么了?”我有些...
    開封第一講書人閱讀 167,815評論 0 360
  • 文/不壞的土叔 我叫張陵需忿,是天一觀的道長诅炉。 經常有香客問我,道長屋厘,這世上最難降的妖魔是什么汞扎? 我笑而不...
    開封第一講書人閱讀 59,537評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮擅这,結果婚禮上澈魄,老公的妹妹穿的比我還像新娘。我一直安慰自己仲翎,他們只是感情好痹扇,可當我...
    茶點故事閱讀 68,536評論 6 397
  • 文/花漫 我一把揭開白布铛漓。 她就那樣靜靜地躺著,像睡著了一般鲫构。 火紅的嫁衣襯著肌膚如雪浓恶。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,184評論 1 308
  • 那天结笨,我揣著相機與錄音包晰,去河邊找鬼。 笑死炕吸,一個胖子當著我的面吹牛伐憾,可吹牛的內容都是我干的。 我是一名探鬼主播赫模,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼树肃,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了瀑罗?” 一聲冷哼從身側響起胸嘴,我...
    開封第一講書人閱讀 39,668評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎斩祭,沒想到半個月后劣像,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 46,212評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡摧玫,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,299評論 3 340
  • 正文 我和宋清朗相戀三年驾讲,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片席赂。...
    茶點故事閱讀 40,438評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖时迫,靈堂內的尸體忽然破棺而出颅停,到底是詐尸還是另有隱情,我是刑警寧澤掠拳,帶...
    沈念sama閱讀 36,128評論 5 349
  • 正文 年R本政府宣布癞揉,位于F島的核電站,受9級特大地震影響溺欧,放射性物質發(fā)生泄漏喊熟。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,807評論 3 333
  • 文/蒙蒙 一姐刁、第九天 我趴在偏房一處隱蔽的房頂上張望芥牌。 院中可真熱鬧,春花似錦聂使、人聲如沸壁拉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽弃理。三九已至溃论,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間痘昌,已是汗流浹背钥勋。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留辆苔,地道東北人算灸。 一個月前我還...
    沈念sama閱讀 48,827評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像姑子,于是被迫代替她去往敵國和親乎婿。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,446評論 2 359