Spring Boot入门教程-FreeMarker模板

来源:互联网 发布:微传单软件 编辑:程序博客网 时间:2024/05/18 03:45

这篇博客写一下Spring Boot 如何使用FreeMarker 。与Spring 繁琐的xml配置相别,SpringBoot 集成FreeMarler简直太省心。

注意:这篇文章所用的项目是在前边几篇博客所介绍的项目基础上进行的,如果感到不知所云的话,请参考之前的博客。

闲话不多说,正式开始。

1.首先在pom.xml中加入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency>
2.在application.properties中对FreeMarker进行配置

###########################################################FREEMARKER########################################################spring.freemarker.allow-request-override=falsespring.freemarker.cache=truespring.freemarker.check-template-location=truespring.freemarker.charset=UTF-8spring.freemarker.content-type=text/htmlspring.freemarker.expose-request-attributes=falsespring.freemarker.expose-session-attributes=falsespring.freemarker.expose-spring-macro-helpers=falsespring.freemarker.prefix=#spring.freemarker.request-context-attribute=#spring.freemarker.settings.spring.freemarker.suffix=.ftlspring.freemarker.template-loader-path=classpath:/templates/#spring.freemarker.view-names= # whitelist of view names that can be resolved

3.在/resources/templates创建一个模板文件index.ftl

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body>${hello}</body></html>
4.在DemoController中添加以下代码

 @RequestMapping("indexPage")    public String helloFtl(Map<String,Object> map) {        map.put("hello","Hello FreeMarker");        return "index";    }
5,启动项目在浏览器中输入http://localhost:8080/indexPage


这就是我们想要的结果,非常的简单。











原创粉丝点击