spring boot 配置 freemarker


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

1.1freemaker 優點

 

 

 

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

 

thymeleaf優點:

  1. 靜態html嵌入標簽屬性,瀏覽器可以直接打開模板文件,便於前后端聯調。
  2. springboot官方推薦方案。

thymeleaf缺點:

  1. 模板必須符合xml規范

比較下兩者

1.從寫code的習慣角度可能freemarker更習慣於我們的思維。
2.不過從前后分離開發的角度看thymeleaf更合適,值的綁定都是基於html的dom元素屬性的,適合前后聯調。

還是回歸下主題

開始編碼:

 

1.引入pom依賴

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

 

2.向yml格式配置文件添加內容

 

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.##'   #數字格式化,無小數點

 

3.測試接口如圖

 

4.index文件位置如圖

 

index 內容

5.啟動項目如圖

index頁面渲染成功

網上收集 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM