溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot+Thymeleaf基于HTML5現(xiàn)代模板引擎的方法

發(fā)布時(shí)間:2022-04-07 11:14:44 來源:億速云 閱讀:174 作者:iii 欄目:編程語(yǔ)言

今天小編給大家分享一下SpringBoot+Thymeleaf基于HTML5現(xiàn)代模板引擎的方法的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

開始使用

1.引入依賴

SpringBoot默認(rèn)提供了Thymeleaf的Starter,只需簡(jiǎn)單引入依賴即可。

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

目前默認(rèn)版本是2.1,如果想升級(jí)版本到3.0,可以這樣修改。

  <properties>
    <thymeleaf.version>3.0.7.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
  </properties>

為了方便開發(fā),項(xiàng)目案例采用了熱部署工具dev-tools ,這樣我們?cè)谛薷捻?yè)面之后,IDEA會(huì)自動(dòng)加載,從而達(dá)到實(shí)現(xiàn)熱更新的效果。

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
    </dependency>

注:由于IDEA默認(rèn)關(guān)閉了熱部署,需要做一些設(shè)置才能使其生效。解決方法:首先按住Ctrl+Shift+Alt+/ 然后進(jìn)入 Registry ,然后勾選compiler.automake.allow.when.app.running 即可。另外,Build->Compiler 也要勾選上Build Project automatically .

2. 添加相關(guān)配置

Thymeleaf默認(rèn)開啟了頁(yè)面緩存,在開發(fā)的時(shí)候,應(yīng)該關(guān)閉緩存。此外,通常我們還會(huì)指定頁(yè)面的存放路徑。(默認(rèn)是classpath:/templates/)

application.yml 配置如下:
spring:
 thymeleaf:
  cache: false #關(guān)閉緩存
  prefix: classpath:/views/ #添加路徑前綴

3.編寫HTML

編寫Thymeleaf和書寫HTML5頁(yè)面沒有什么不同,最大的轉(zhuǎn)變就是使用拓展屬性(th:xx)去跟服務(wù)端進(jìn)行數(shù)據(jù)交互,保留原始頁(yè)面風(fēng)格,也是Thymeleaf的推崇的風(fēng)格。例如下面這個(gè)簡(jiǎn)單的案例,啟動(dòng)項(xiàng)目,我們發(fā)現(xiàn)頁(yè)面跳轉(zhuǎn)的是簡(jiǎn)書的連接,這一點(diǎn)也驗(yàn)證了Thymeleaf覆蓋原生頁(yè)面數(shù)據(jù)的極佳能力。

頁(yè)面代碼:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <title>Thymeleaf</title>
</head>
<body>
  <h3>歡迎使用Thymeleaf!!</h3>
  <a href="http://www.baidu.com" rel="external nofollow" th:href="${info}" rel="external nofollow" >戳我有驚喜</a>
</body>
</html>

后端代碼:

@Controller
public class UserController {

  @GetMapping("/")
  public String index(Model model) {
    model.addAttribute("info", "user/list");
    return "index";
  }

  @GetMapping("/user")
  public String hehe(Model model) {
    model.addAttribute("user", new User(UUID.randomUUID().toString(), "yizhiwazi", "20170928"));
    return "user";
  }

  @GetMapping("/user/list")
  public String userlist(Model model) {
    List<User> userList = new ArrayList<>();
    userList.add(new User(UUID.randomUUID().toString(), "yizhiwazi", "20170928"));
    userList.add(new User(UUID.randomUUID().toString(), "kumamon", "123456"));
    userList.add(new User(UUID.randomUUID().toString(), "admin", "admin"));
    model.addAttribute("userList", userList);
    return "userList";
  }

}

現(xiàn)在我們嘗試回填一個(gè)表單,展示單個(gè)用戶信息。

<!-- 使用th:object 搭配*{} 可以省略對(duì)象名 -->
<form action="/" th:object="${user}" >
  <input type="hidden" name="userId" th:value="*{userId}" />
  <input type="text" name="username" th:value="*{username}" />
  <input type="text" name="password" th:value="*{password}" />
</form>

接下來,我們進(jìn)入一個(gè)更復(fù)雜的案例,例如展示一個(gè)用戶列表信息,不需要編寫新的標(biāo)簽,就可以完成對(duì)批量用戶的遍歷。

<h3>用戶列表</h3>
<!--說明: th:each="obj,stat:${objects}" 分別代表單個(gè)實(shí)例,狀態(tài)(可省略),待遍歷對(duì)象-->
<div th:each="user:${userList}">
  <input type="hidden" name="userId" th:value="${user.userId}"/>
  用戶姓名:<input type="text" name="password" th:value="${user.username}"/>
  登錄密碼:<input type="text" name="username" th:value="${user.password}"/>
</div>

以上就是“SpringBoot+Thymeleaf基于HTML5現(xiàn)代模板引擎的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI