欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring boot 配置freemarker及如何使用freemarker渲染頁面

 更新時間:2023年10月30日 15:04:16   作者:寂夜了無痕  
springboot中自帶的頁面渲染工具為thymeleaf 還有freemarker這兩種模板引擎,本文重點給大家介紹spring boot 配置freemarker及如何使用freemarker渲染頁面,感興趣的朋友一起看看吧

1.springboot 中自帶的頁面渲染工具為thymeleaf 還有freemarker 這兩種模板引擎 簡單比較下兩者不同

1.1freemaker 優(yōu)點

freemarker 不足:thymeleaf由于使用了標簽屬性做為語法,模版頁面直接用瀏覽器渲染,使得前端和后端可以并行開發(fā)。freemarket使用</>這樣的語法,就無法直接使瀏覽器渲染出原本頁面的樣子。

thymeleaf優(yōu)點:

靜態(tài)html嵌入標簽屬性,瀏覽器可以直接打開模板文件,便于前后端聯(lián)調(diào)。 springboot官方推薦方案。

thymeleaf缺點:

模板必須符合xml規(guī)范 比較下兩者

1.從寫code的習慣角度可能freemarker更習慣于我們的思維。
2.不過從前后分離開發(fā)的角度看thymeleaf更合適,值的綁定都是基于html的dom元素屬性的,適合前后聯(lián)調(diào)。

還是回歸下主題

開始編碼:

1.引入pom依賴

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

2.向yml格式配置文件添加內(nèi)容

spring:
  freemarker:
    request-context-attribute: req  #req訪問request
    suffix: .html  #后綴名
    content-type: text/html
    enabled: true
    cache: false #緩存配置
    template-loader-path: classpath:/templates/ #模板加載路徑 按需配置
    charset: UTF-8 #編碼格式
    settings:
      number_format: '0.##'   #數(shù)字格式化,無小數(shù)點

3.測試接口如圖

4.index文件位置如圖

index 內(nèi)容

5.啟動項目如圖

index頁面渲染成功

網(wǎng)上收集 propertity的freemarker 配置 (propertity格式配置文件)

 # FREEMARKER (FreeMarkerAutoConfiguration)
spring.freemarker.allow-request-override=false # Set whether HttpServletRequest attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.freemarker.allow-session-override=false # Set whether HttpSession attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.freemarker.cache=false # Enable template caching.
spring.freemarker.charset=UTF-8 # Template encoding.
spring.freemarker.check-template-location=true # Check that the templates location exists.
spring.freemarker.content-type=text/html # Content-Type value.
spring.freemarker.enabled=true # Enable MVC view resolution for this technology.
spring.freemarker.expose-request-attributes=false # Set whether all request attributes should be added to the model prior to merging with the template.
spring.freemarker.expose-session-attributes=false # Set whether all HttpSession attributes should be added to the model prior to merging with the template.
spring.freemarker.expose-spring-macro-helpers=true # Set whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext".
spring.freemarker.prefer-file-system-access=true # Prefer file system access for template loading. File system access enables hot detection of template changes.
spring.freemarker.prefix= # Prefix that gets prepended to view names when building a URL.
spring.freemarker.request-context-attribute= # Name of the RequestContext attribute for all views.
spring.freemarker.settings.*= # Well-known FreeMarker keys which will be passed to FreeMarker's Configuration.
spring.freemarker.suffix= # Suffix that gets appended to view names when building a URL.
spring.freemarker.template-loader-path=classpath:/templates/ # Comma-separated list of template paths.
spring.freemarker.view-names= # White list of view names that can be resolved.

另外需要使用thymeleaf 可以使用如下配置(yml 格式配置文件方式)

##視圖模型
spring:
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    cache: false
    encoding: utf-8
    content-type: text/html
    check-template-location: true

到此這篇關(guān)于spring boot 配置freemarker及使用freemarker渲染頁面的文章就介紹到這了,更多相關(guān)spring boot 配置freemarker內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java編寫緩存工具類的示例代碼

    Java編寫緩存工具類的示例代碼

    這篇文章主要為大家詳細介紹了如何基于Java編寫一個緩存工具類,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的小伙伴可以了解一下
    2023-07-07
  • spring boot(四)之thymeleaf使用詳解

    spring boot(四)之thymeleaf使用詳解

    Thymeleaf 是一個跟 Velocity、FreeMarker 類似的模板引擎,它可以完全替代 JSP 。接下來通過本文給大家介紹spring boot(四)之thymeleaf使用詳解,需要的朋友可以參考下
    2017-05-05
  • 基于maven實現(xiàn)私服搭建步驟圖解

    基于maven實現(xiàn)私服搭建步驟圖解

    這篇文章主要介紹了基于maven實現(xiàn)私服搭建步驟圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • Java 正則表達式 解釋說明

    Java 正則表達式 解釋說明

    java正則知識小結(jié),一些常見的正則都包括在里面,推薦收藏。
    2009-06-06
  • 關(guān)于springboot中的自定義配置項

    關(guān)于springboot中的自定義配置項

    這篇文章主要介紹了關(guān)于springboot中的自定義配置項,在項目開發(fā)的過程中,經(jīng)常需要自定義系統(tǒng)業(yè)務(wù)方面的配置文件及配置項,Spring Boot提供了@value注解、@ConfigurationProperties注解和Environment接口等3種方式自定義配置項,需要的朋友可以參考下
    2023-07-07
  • Java內(nèi)存管理垃圾回收基礎(chǔ)詳解

    Java內(nèi)存管理垃圾回收基礎(chǔ)詳解

    這篇文章主要為大家介紹了Java內(nèi)存管理垃圾回收基礎(chǔ)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • 關(guān)于SpringCloudStream配置問題

    關(guān)于SpringCloudStream配置問題

    這篇文章主要介紹了關(guān)于SpringCloudStream配置問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 新手入門學習Spring Freemarker教程解析

    新手入門學習Spring Freemarker教程解析

    這篇文章主要介紹了新手入門學習Freemarker教程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • Java如何按16進制發(fā)送和接收TCP指令

    Java如何按16進制發(fā)送和接收TCP指令

    這篇文章主要介紹了Java如何按16進制發(fā)送和接收TCP指令問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Spring中Bean對象的定義、注冊和獲取流程分析

    Spring中Bean對象的定義、注冊和獲取流程分析

    這篇文章主要介紹了Spring中Bean對象的定義、注冊和獲取流程分析,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-06-06

最新評論