springboot整合freemarker-freemarker入门篇

资源贴 https://blog.csdn.net/z69183787/article/details/73850417

githup项目 https://github.com/wenrongyao/springboot_freemarker  master分支是最基础的整合,dev分支是freemarker的标签学习

目录结构

springboot整合freemarker-freemarker入门篇_第1张图片

1、pom.xml中添加freemarker依赖


     org.springframework.boot
     spring-boot-starter-freemarker

2、配置application.properties,springboot项目有默认的配置,自己写了以后可以替代默认的配置。

spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
server.port = 8090

3、HelloController,映射配置和数据填充


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * @author rongyaowen
 */
@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("name", "kangkang");
        return "hello";
    }

}

4、模板文件,htllo.ftl




    
    Title


    Hello ${name}

5、springboot启动类


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author rongyaowen
 */
@SpringBootApplication
@ComponentScan("com.wry.controller")
public class SpringBootSampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootSampleApplication.class, args);

    }
}

结果展示,模板中的${name}已经被替换

springboot整合freemarker-freemarker入门篇_第2张图片

你可能感兴趣的:(freemarker)