SpringBoot 整合 Freemarker properties、 ymal 配置详细说明

参考文档:

https://blog.csdn.net/qq_45794678/article/details/102999519

 

导包:

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

注意:

spring.mvc.static-path-pattern=/** 不要写成 classpath:/**\

## Freemarker 配置
## 文件配置路径
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
spring.mvc.static-path-pattern=/**

yaml:

spring:
  ## Freemarker 配置
  freemarker:
    cache: false
    charset: UTF-8
    check-template-location: true
    content-type: text/html
    expose-request-attributes: true
    expose-session-attributes: true
    request-context-attribute: request
    suffix: .ftl
    template-loader-path: classpath:/templates/
  mvc:
    static-path-pattern: /**

如果需要更多的 FreeMarker 配置,可以查看下面的详解:

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.
#配置freemarker详解

#spring.freemarker.allow-request-override=false 
# Set whether HttpServletRequest attributes are allowed to override (hide) controller generated model attributes of the same name.
#设置是否允许HttpServletRequest属性覆盖(隐藏)控制器生成的同名模型属性。

#spring.freemarker.allow-session-override=false 
# Set whether HttpSession attributes are allowed to override (hide) controller generated model attributes of the same name.
#设置是否允许HttpSession属性覆盖(隐藏)控制器生成的同名模型属性。

#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.
#为这种技术启用MVC视图解决方案。

#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.
#设置是否在与模板合并之前将所有HttpSession属性添加到模型中。

#spring.freemarker.expose-spring-macro-helpers=true 
# Set whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext".
#设置是否公开RequestContext供Spring宏库使用,名称为“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.
#前缀,用于在构建URL时查看名称

#spring.freemarker.request-context-attribute= 
# Name of the RequestContext attribute for all views.
#所有视图的RequestContext属性的名称。

#spring.freemarker.settings.*= 
# Well-known FreeMarker keys which will be passed to FreeMarker's Configuration.
#众所周知的FreeMarker密钥将传递给FreeMarker的配置。

#spring.freemarker.suffix= 
# Suffix that gets appended to view names when building a URL.
#后缀,该后缀用于在构建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.
#可以解析的视图名称的白列表。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Freemarker是一种模板引擎,可以将数据和模板进行整合生成输出内容。SpringBoot提供了对Freemarker的支持,可以很方便地整合Freemarker。 1. 添加依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> ``` 2. 配置文件 在application.properties文件中添加以下配置: ```properties spring.freemarker.template-loader-path=classpath:/templates/ spring.freemarker.cache=false ``` - template-loader-path:模板文件的路径,这里设置为classpath:/templates/,表示在项目的classpath下的templates目录中查找模板文件。 - cache:是否开启模板缓存,这里设置为false,表示关闭缓存。 3. 创建模板文件 在classpath:/templates/目录下创建一个名为index.ftl的模板文件,内容如下: ```html <!DOCTYPE html> <html> <head> <title>SpringBoot整合Freemarker</title> </head> <body> <h1>${message}</h1> </body> </html> ``` 4. 创建控制器 创建一个名为IndexController的控制器,代码如下: ```java @Controller public class IndexController { @RequestMapping("/") public String index(Model model) { model.addAttribute("message", "Hello, World!"); return "index"; } } ``` 该控制器中,使用@RequestMapping注解指定了请求路径为/,并将一个名为message的属性值设置为“Hello, World!”,然后返回了index作为视图名称。由于配置了spring.freemarker.template-loader-path=classpath:/templates/,所以SpringBoot会在classpath:/templates/目录下查找名为index的模板文件,并将模板文件中的${message}替换为“Hello, World!”。 5. 运行程序 启动应用程序,访问http://localhost:8080/,可以看到页面中显示了“Hello, World!”的字样。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值