SpringBoot整合FreeMarker yml配置

1.添加freemarker的maven

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

2.在application.yml文件中配置freemarker

application.yml对比application.properties,其实作用都一样,但是使用yml看起来更加清晰,推荐大家使用

  #------------------------freemarker-----------------------------
spring:
  freemarker:
    cache: false
    suffix: .ftl
    template-loader-path: classpath:/templates
    content-type: text/html; charset=utf-8
    settings:
      number_format: 0   #数字格式,比较大的数字会带有逗号,如1,000

3.前端与后台代码测试

笔者这里写一个简单的controller做测试

@RequestMapping(value = "/")
    public String initItblog(Model model) {
        model.addAttribute("msg", "hello");
        return "login";
    }

前端页面要小心主要,因为freemarker的一些小问题,如果msg是空的话就会报错,所以在获取值的时候要加上非空判断,这样就可以获取到值了

<html>
<head>
 <title>login</title>
 </head>
 <body>
	<#if (msg)!="">
	       ${msg}
	</#if>
</body>
</html>
发布了25 篇原创文章 · 获赞 4 · 访问量 1516

猜你喜欢

转载自blog.csdn.net/weixin_39025362/article/details/105433670