• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    SpringBoot整合Thymeleaf和FreeMarker模板引擎

    今天小編為大家介紹SpringBoot整合視圖層技術,SpringBoot官方提供的模板引擎是Thymeleaf,不過實際開發中FreeMarker的使用也較多,使用模板引擎可以替代JSP的開發,接下來就跟著小編一起學習吧。

    東西/原料

    • IntelliJ IDEA

    方式/步調

    1. 1

      起首我們需要在pom文件中引入Thymeleaf和FreeMarker相關的依靠。

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

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

    2. 2

      Thymeleaf是撐持HTML的,默認的模板文件的位置在classpath:/templates/,默認的模板后綴名為.html。FreeMarker默認的模板文件的位置在classpath:/templates/,默認的模板后綴名為.ftl。

    3. 3

      接下來我們建立Book實體類,添加id, name,author三個屬性,并生當作響應的getter/setter方式。

    4. 4

      然后建立BookController,在Controller中返回ModelAndView。

    5. 5

      接下來我們看看Thymeleaf的模板文件books.html的寫法,如下所示,用th:each遍歷后獲取值并展示。接下來我們啟動辦事,然后請求/books,成果為:

    6. 6

      我們再來看FreeMarker模板文件的寫法,如下為books.ftl文件的內容,兩個模板引擎可以分隔在兩個工程中,便利查看結果。

      END

    注重事項

    • FreeMarker還可以作為電子郵件等的模板。
    • 發表于 2020-02-28 17:21
    • 閱讀 ( 1110 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆