Spring Boot 初体验(10)使用使用freemarker
Posted kuangyefeige
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 初体验(10)使用使用freemarker相关的知识,希望对你有一定的参考价值。
一:在pom.xml中引入freemarker
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
关于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
代码:
controller:
package com.mt.controller; import java.util.Date; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/thymeleaf") public class ThymeleafController { @RequestMapping("/hello") public String hello(Map<String, Object> map) { map.put("date", new Date()); return "thymeleafDemo"; } @RequestMapping("/demo") public String demo(Map<String, Object> map) { map.put("demo", "freemarker demo "); return "freemarkerDemo"; } @RequestMapping("/rr") public String rr(Map<String, Object> map) { map.put("demo", "freemarker demo "); return "rr"; } }
ftl:
<!DOCTYPE html> <head> <title>Hello World!</title> </head> <body> <h1>Hello.v.2</h1> <p>${demo}</p> </body> </html>
以上是关于Spring Boot 初体验(10)使用使用freemarker的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot+Spring Security:初体验
Spring Boot学习进阶笔记-初体验,创建基本的web功能