5分钟学会springboot整合Freemarker

一、前言

       springboot整合Freemarker。

       在此记录下,分享给大家。

二、springboot整合Freemarker

                                                 5分钟学会springboot整合Freemarker_第1张图片

1、pom文件 依赖引入

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.8.RELEASE
        
    

    
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

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

        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

2、 application.yml 新增配置

spring:
  http:
    encoding:
      force: true
      # 模板引擎编码为UTF-8
      charset: UTF-8
  freemarker:
    allow-request-override: false
    cache: false
    check-template-location: true
    charset: UTF-8
    content-type: text/html; charset=utf-8
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    # 模板文件结尾.ftl
    suffix: .ftl
    # 模板文件目录
    template-loader-path: classpath:/templates

3、FreemarkerController.java

/**
 * Freemarker测试
 *      Controller
 * @author yys
 */
@Controller
public class FreemarkerController {

    @RequestMapping("/index")
    public String index(Map map) {
        // 花名
        map.put("name", "yys");
        // 性别
        map.put("sex", "1");
        // 爱好
        List list = new ArrayList(0);
        list.add("挑灯写博客");
        list.add("打羽毛球");
        list.add("健身");
        map.put("hobbys", list);
        return "/index";

    }

}

4、启动类

@SpringBootApplication
public class YysApp {

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

}

5、index.ftl




    
    一生猿,一世猿。



    
简介:
花名 ${name}
性别 <#if sex == "1"> 男 <#elseif sex == "2"> 女 <#else> other
爱好 <#list hobbys as hobby> ${hobby} 

6、测试

http://localhost:8080/index

  a、页面结果 - 如下图所示 :

                                         5分钟学会springboot整合Freemarker_第2张图片

你可能感兴趣的:(java,程序员,springboot)