SpringBoot整合freemarker

增加freemarker依赖


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

增加freemarker相关配置

spring.freemarker.allow-request-override=false
spring.freemarker.allow-session-override=false
spring.freemarker.cache=true
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true  
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.prefer-file-system-access=false
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.settings.template_update_delay=0
spring.freemarker.settings.default_encoding=UTF-8
spring.freemarker.settings.classic_compatible=true 
spring.freemarker.order=1

创建ftl视图文件

src/main/resources/目录中增加templates文件夹,并增加.ftl文件




    
    
    
    Document


欢迎您,${Request.member.username}

修改Controller

package cn.ishangit.hellospringboot2.controller;

import cn.ishangit.hellospringboot2.bean.Member;
import cn.ishangit.hellospringboot2.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @description:
 * @author: Chen
 * @create: 2019-06-24 20:22
 **/
@Controller
public class MemberController {

    @Autowired
    private MemberService memberService;

    @RequestMapping(value = "/index/{id}")
    public String getAll(@PathVariable("id")Integer id, Model model){
        Member member = memberService.getMember(id);
        model.addAttribute("member", member);
        return "index";
    }
}

测试

访问http://localhost:8080/index/1测试

你可能感兴趣的:(SpringBoot整合freemarker)