SpringBoot integrated FreeMarker yml configuration

1. Add freemarker maven

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

2. Configure freemarker in the application.yml file

application.yml compared to application.properties, in fact, the effect is the same, but using yml looks more clear, it is recommended to use

  #------------------------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. Front-end and background code testing

The author writes a simple controller here for testing

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

The front-end page should be careful. Because of some small problems with freemarker, if msg is empty, an error will be reported.

<html>
<head>
 <title>login</title>
 </head>
 <body>
	<#if (msg)!="">
	       ${msg}
	</#if>
</body>
</html>
Published 25 original articles · praised 4 · visits 1516

Guess you like

Origin blog.csdn.net/weixin_39025362/article/details/105433670