-模板引擎,比jsp(本质servlet),效率高,freemark页面后缀是 .ftl
1、引入maven
<!--2019-5-14 模板引擎 freemarker ,在application中配置--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId><version>2.1.3.RELEASE</version></dependency>
2、在application中配置
#设定ftl文件位置,ftl就是页面
spring.freemarker.template-loader-path=classpath:/templates
#关闭缓存,即使刷新,上线生成环境需要改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
#定义页面后缀
spring.freemarker.suffix=.ftl#设置静态文件路径,静态资源配置
spring.mvc.static-path-pattern=/static/**
resources文件结构:
3、写个简单页面,来测试效果
页面:center.ftl
<!DOCTYPE html>
<html>
<head lang="en"><meta charset="UTF-8"/><title></title>
</head>
<#--访问:http://localhost:8009/demo/freemark/index-->
<body>
FreeMarker模板引擎
<h1>center page</h1>
</body>
</html>
Controller.java
@RequestMapping("/freemark")
@Controller
public class FreemarkController {@Autowiredprivate Resources resources;// 访问:http://localhost:8009/demo/freemark/index@RequestMapping(value="/index")public String index(ModelMap map){map.addAttribute("resources",resources);return "freemarker/index";}
// 访问:http://localhost:8009/demo/freemark/center@RequestMapping(value="/center")public String center(ModelMap map){return "freemarker/center/center";}
}
页面测试(1):
(2)从后台传送数据到页面:把资源文件里的内容显示到页面
页面:index.ftl
<!DOCTYPE html>
<html>
<head lang="en"><meta charset="UTF-8"/><title></title>
</head>
<body>FreeMarker模板引擎<h1>${resources.name}</h1><h1>${resources.website}</h1><h1>${resources.language}</h1>
</body>
</html>