原创

SpringBoot项目实战(3):整合Freemark模板

SpringBoot 自动继承了thymeleaf、freemark、velocity三种模板技术,因为我项目中需要用来Freemark模板进行生成Java类所以,就整合了SB+Freemark

1.配置pom文件,引入Freemarker

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2.配置模板属性(application.properties)

# FREEMARKER (FreeMarkerAutoConfiguration)
spring.freemarker.allow-request-override=false
spring.freemarker.allow-session-override=false
spring.freemarker.cache=false
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=true
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

3.编写Controller

@RequestMapping("/hello/{name}")
public String hello(@PathVariable("name") String name, Model model) {
   model.addAttribute("name", name);
   model.addAttribute("age","25");
   model.addAttribute("sex","man");
   model.addAttribute("birth",new Date());
   return "hello";
}

4.编写ftl页面模板

<!DOCTYPE html>
<html lang="en">
<head>
    <title>SpringBoot + Freemarker</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Hello ${name}${age}${sex}
<p>${birth?string("yyyy-MM-dd HH:mm:ss.sss")}</p>
</body>
</html>

5.启动Application、访问hello/{name}


项目地址:GitHub地址,点击查看


其他相关文章


正文到此结束