1、在工程新建文件夹templates
路径:src/main/resources/templates
在配置文件application.properties中新增FreeMarker配置
######################################################## ###FREEMARKER (FreeMarkerAutoConfiguration) ######################################################## 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 #spring.freemarker.prefix= #spring.freemarker.request-context-attribute= #spring.freemarker.settings.*= spring.freemarker.suffix=.ftl spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list #spring.freemarker.view-names= # whitelist of view names that can be resolved
2、Freemarker的文件格式是.ftl
3、pom.xml中引入依赖
<!-- 引入freeMarker的依赖包. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
4、创建一个controller接口,controller的注解要用@Controller【跳转页面】不能用@RestController【返回JSON数据】
@RequestMapping("/index") public String index(Map<String, Object> map) { map.put("name","美丽的天使..."); return "index"; }
5、写一个index.ftl文件
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title>首页</title> </head> <body> ${name} <#if sex=="1"> 男 <#else if sex=="2"> 女 <#else> 其他 </#if> <#list userlist as user> ${user} </#list> </body> </html>