0 课程地址
https://www.imooc.com/video/16720
1 整合freemarker前置条件
1.1 pom.xml引入相关依赖
<!-- 引入 freemarker 模板依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
1.2 application.properties配置文件引入freemarker
############################################################
#
# freemarker 静态资源配置
#
############################################################
#设定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
# 后缀
springfreemarker.suffix=.ftl
2 整合freemarker测试案例
2.1 demo1 静态template和非静态template案例
FreemarkerController
package com.example.demo.controller; import com.example.demo.bean.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * FreemarkerController * * @author 魏豆豆 * @date 2020/11/28 */ @Controller @RequestMapping("/ftl") /*1.RequestMapping注解的作用是建立请求URL和处理方法之间的对应关系 2.RequestMapping注解可以作用在方法和类上 1. 作用在类上:第一级的访问目录 2. 作用在方法上:第二级的访问目录 3. 细节:路径可以不编写 / 表示应用的根目录开始 */ public class FreemarkerController { /** * 自动装配,初始化resource, * Resource类相关属性 */ @Autowired private Resource resource2;
/* 非静态
*/
@RequestMapping(value = "/index") public String index(ModelMap map){ map.addAttribute("resource",resource2); return "freemarker/index"; } /** * 1 静态template引用 * 2 多级路径 * @return */ @RequestMapping(value = "/center") public String center(){ return "freemarker/center/center"; } }
Resource类
package com.example.demo.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; /** * Resource * * @author 魏豆豆 * @date 2020/11/21 */ @Configuration//会引入资源文件 @PropertySource(value="classpath:resource.properties")//引入资源文件的地址 @ConfigurationProperties(prefix = "com.imooc.openresource")//前缀 public class Resource { private String name; private String website; private String language; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getWebsite() { return website; } public void setWebsite(String website) { this.website = website; } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } }
resource.properties
com.imooc.openresource.name=weidoudou
com.imooc.openresource.website=www.imooc.com
com.imooc.openresource.language=java
静态template center.ftl
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title></title> </head> <body> FreeMarker模板引擎 <h1>center page</h1> </body> </html>
非静态template index.ftl
<!DOCTYPE html> <html> <head lang="en"> <meta charset="GBK" /> <title></title> </head> <body> FreeMarker模板引擎 <h1>${resource.name}</h1> <h1>${resource.website}</h1> <h1>${resource.language}</h1> </body> </html>
静态ftl测试:
动态ftl测试