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

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

1.配置pom文件,引入Freemarker

<dependency>
   <groupId>org.springframework.bootgroupId>
   <artifactId>spring-boot-starter-freemarkerartifactId>
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 mapping

@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.编写hello.ftl页面模板


<html lang="en">
<head>
    <title>SpringBoot + Freemarkertitle>
    <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}

Hello zhangyd25man
2016-10-21 10:51:26.026

你可能感兴趣的:(springboot)