增加freemarker依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
增加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
文件
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> 欢迎您,${Request.member.username} </body> </html>
修改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"; } }