宅男在线永久免费观看网直播,亚洲欧洲日产国码无码久久99,野花社区在线观看视频,亚洲人交乣女bbw,一本一本久久a久久精品综合不卡

全部
常見問題
產(chǎn)品動(dòng)態(tài)
精選推薦

Spring Boot 2.x基礎(chǔ)教程:使用 Thymeleaf開發(fā)Web頁面

管理 管理 編輯 刪除

接下來,我們就來具體講講在Spring Boot應(yīng)用中,如何使用Thymeleaf模板引擎開發(fā)Web頁面類的應(yīng)用。

#靜態(tài)資源訪問

在我們開發(fā)Web應(yīng)用的時(shí)候,需要引用大量的js、css、圖片等靜態(tài)資源。Spring Boot默認(rèn)提供靜態(tài)資源目錄位置需置于classpath下,目錄名需符合如下規(guī)則:

  • /static
  • /public
  • /resources
  • /META-INF/resources

舉例:我們可以在src/main/resources/目錄下創(chuàng)建static,在該位置放置一個(gè)圖片文件。啟動(dòng)程序后,嘗試訪問http://localhost:8080/D.jpg。如能顯示圖片,配置成功。

#渲染W(wǎng)eb頁面

在之前的示例中,我們都是通過@RestController來處理請(qǐng)求,所以返回的內(nèi)容為json對(duì)象。那么如果需要渲染html頁面的時(shí)候,要如何實(shí)現(xiàn)呢?

#模板引擎

在動(dòng)態(tài)HTML實(shí)現(xiàn)上Spring Boot依然可以完美勝任,并且提供了多種模板引擎的默認(rèn)配置支持,所以在推薦的模板引擎下,我們可以很快的上手開發(fā)動(dòng)態(tài)網(wǎng)站。

Spring Boot提供了自動(dòng)化配置模塊的模板引擎主要有以下幾種:

  • Thymeleaf
  • FreeMarker
  • Groovy

當(dāng)你使用上述模板引擎中的任何一個(gè),它們默認(rèn)的模板配置路徑為:src/main/resources/templates。當(dāng)然也可以修改這個(gè)路徑,具體如何修改,可在后續(xù)各模板引擎的配置屬性中查詢并修改。

補(bǔ)充:Spring Boot從一開始就建議使用模板引擎,避免使用JSP。同時(shí),隨著Spring Boot版本的迭代,逐步的淘汰了一些較為古老的模板引擎。

#Thymeleaf

Thymeleaf是本文我們將具體介紹使用的模板引擎。它是一個(gè)XML/XHTML/HTML5模板引擎,可用于Web與非Web環(huán)境中的應(yīng)用開發(fā)。它是一個(gè)開源的Java庫,基于Apache License 2.0許可,由Daniel Fernández創(chuàng)建,該作者還是Java加密庫Jasypt的作者。

Thymeleaf提供了一個(gè)用于整合Spring MVC的可選模塊,在應(yīng)用開發(fā)中,你可以使用Thymeleaf來完全代替JSP或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目標(biāo)在于提供一種可被瀏覽器正確顯示的、格式良好的模板創(chuàng)建方式,因此也可以用作靜態(tài)建模。你可以使用它創(chuàng)建經(jīng)過驗(yàn)證的XML與HTML模板。相對(duì)于編寫邏輯或代碼,開發(fā)者只需將標(biāo)簽屬性添加到模板中即可。接下來,這些標(biāo)簽屬性就會(huì)在DOM(文檔對(duì)象模型)上執(zhí)行預(yù)先制定好的邏輯。

示例模板:

<table>
  <thead>
    <tr>
      <th th:text="#{msgs.headers.name}">Name</td>
      <th th:text="#{msgs.headers.price}">Price</td>
    </tr>
  </thead>
  <tbody>
    <tr th:each="prod : ${allProducts}">
      <td th:text="${prod.name}">Oranges</td>
      <td th:text="${#numbers.formatDecimal(prod.price,1,2)}">0.99</td>
    </tr>
  </tbody>
</table>

可以看到Thymeleaf主要以屬性的方式加入到html標(biāo)簽中,瀏覽器在解析html時(shí),當(dāng)檢查到?jīng)]有的屬性時(shí)候會(huì)忽略,所以Thymeleaf的模板可以通過瀏覽器直接打開展現(xiàn),這樣非常有利于前后端的分離。

#動(dòng)手試一下

第一步:新建一個(gè)Spring Boot應(yīng)用,在pom.xml中加入所需的模板引擎模塊,比如使用thymeleaf的話,只需要引入下面依賴:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

第二步:創(chuàng)建一個(gè)Spring MVC的傳統(tǒng)Controller,用來處理根路徑的請(qǐng)求,將解決渲染到index頁面上,具體實(shí)現(xiàn)如下

@Controller
public class HelloController {

    @GetMapping("/")
    public String index(ModelMap map) {
        map.addAttribute("host", "https://blog.didispace.com");
        return "index";
    }

}

簡要說明:

  • 在渲染到index頁面的時(shí)候,通過ModelMap,往頁面中增加一個(gè)host參數(shù),其值為https://blog.didispace.com
  • return的值index代表了要使用的模板頁面名稱,默認(rèn)情況下,它將對(duì)應(yīng)到src/main/resources/templates/目錄下的index.html模板頁面

第三步:根據(jù)上一步要映射的模板,去模板路徑src/main/resources/templates下新建模板文件index.html,內(nèi)容如下:


<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>
</body>
</html>

在該頁面的body中,包含了一個(gè)帶有Thymeleaf屬性的h1標(biāo)簽,該便簽內(nèi)容將綁定host參數(shù)的值。

由于Thymeleaf通過屬性綁定的特性。該模板頁面同其他模板引擎不同,直接通過瀏覽器打開html頁面,它是可以正常運(yùn)作的,將會(huì)直接展現(xiàn)Hello World標(biāo)題。這有利于開發(fā)頁面的時(shí)候可以在非啟動(dòng)環(huán)境下驗(yàn)證應(yīng)前端樣式的正確性。

如果啟動(dòng)程序后,訪問http://localhost:8080/,上面頁面就會(huì)展示Controller中host的值:https://blog.didispace.com,做到了不破壞HTML自身內(nèi)容的數(shù)據(jù)邏輯分離。

更多Thymeleaf的頁面語法,可以訪問Thymeleaf的官方文檔來深入學(xué)習(xí)使用。

#Thymeleaf的配置參數(shù)

如有需要修改默認(rèn)配置的時(shí)候,只需復(fù)制下面要修改的屬性到application.properties中,并修改成需要的值:


# Enable template caching.
spring.thymeleaf.cache=true 
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true 
# Content-Type value.
spring.thymeleaf.content-type=text/html 
# Enable MVC Thymeleaf view resolution.
spring.thymeleaf.enabled=true 
# Template encoding.
spring.thymeleaf.encoding=UTF-8 
# Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.excluded-view-names= 
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.mode=HTML5 
# Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.prefix=classpath:/templates/ 
# Suffix that gets appended to view names when building a URL.
spring.thymeleaf.suffix=.html  spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.

舉幾個(gè)我們常用的配置內(nèi)容:

Q:不想每次修改頁面都重啟

A:修改spring.thymeleaf.cache參數(shù),設(shè)置為false

Q:不想使用template目錄存放模板文件

A:修改spring.thymeleaf.prefix參數(shù),設(shè)置為你想放置模板文件的目錄

Q:不想使用index作為模板文件的擴(kuò)展名

A:修改spring.thymeleaf.suffix參數(shù),設(shè)置為你想用的擴(kuò)展名

Q:HTML5的嚴(yán)格校驗(yàn)很煩人

A:修改spring.thymeleaf.mode參數(shù),設(shè)置為LEGACYHTML5

更多本系列免費(fèi)教程連載「點(diǎn)擊進(jìn)入?yún)R總目錄」

#代碼示例

本文的相關(guān)例子可以查看下面?zhèn)}庫中的chapter4-1目錄:

請(qǐng)登錄后查看

CRMEB 最后編輯于2025-02-27 14:48:27

快捷回復(fù)
回復(fù)
回復(fù)
回復(fù)({{post_count}}) {{!is_user ? '我的回復(fù)' :'全部回復(fù)'}}
排序 默認(rèn)正序 回復(fù)倒序 點(diǎn)贊倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level }}

作者 管理員 企業(yè)

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
{{item.is_suggest == 1? '取消推薦': '推薦'}}
沙發(fā) 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暫無簡介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
打賞
已打賞¥{{item.reward_price}}
{{item.like_count}}
{{item.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回復(fù) {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
打賞
已打賞¥{{itemc.reward_price}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)
查看更多
打賞
已打賞¥{{reward_price}}
1009
{{like_count}}
{{collect_count}}
添加回復(fù) ({{post_count}})

相關(guān)推薦

快速安全登錄

使用微信掃碼登錄
{{item.label}} 加精
{{item.label}} {{item.label}} 板塊推薦 常見問題 產(chǎn)品動(dòng)態(tài) 精選推薦 首頁頭條 首頁動(dòng)態(tài) 首頁推薦
取 消 確 定
回復(fù)
回復(fù)
問題:
問題自動(dòng)獲取的帖子內(nèi)容,不準(zhǔn)確時(shí)需要手動(dòng)修改. [獲取答案]
答案:
提交
bug 需求 取 消 確 定
打賞金額
當(dāng)前余額:¥{{rewardUserInfo.reward_price}}
{{item.price}}元
請(qǐng)輸入 0.1-{{reward_max_price}} 范圍內(nèi)的數(shù)值
打賞成功
¥{{price}}
完成 確認(rèn)打賞

微信登錄/注冊(cè)

切換手機(jī)號(hào)登錄

{{ bind_phone ? '綁定手機(jī)' : '手機(jī)登錄'}}

{{codeText}}
切換微信登錄/注冊(cè)
暫不綁定
CRMEB客服

CRMEB咨詢熱線 咨詢熱線

400-8888-794

微信掃碼咨詢

CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
返回頂部 返回頂部
CRMEB客服