从0开始学习SpringCould(4)--SpringBoot 集成freemarker

来源:互联网 发布:mac上安装jmeter 编辑:程序博客网 时间:2024/06/06 02:17

前面几篇已经介绍过SpringBoot的基本使用和配置
本篇来介绍下 使用SpringBoot和freemarker集成使用

一、添加maven依赖

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


二、application.yml中添加freemarker配置


spring:   freemarker:   # freemarker配置    suffix: .ftl    check-template-location: true # 是否缓存    defaultEncoding: UTF-8    templateLoaderPath: classpath:/demo # ftl所在路径    content-type: text/html    request-context-attribute: request  mvc:    static-path-pattern: /static/**  # 静态文件所在路径


项目路径如图:

这里写图片描述

简单说明下,webapp目录如果不存在,需要手动创建。
static–用于存放css、images、js等静态文件
demo–用于存放ftl文件


三、新建index.ftl文件

<body>code:${code!''}<br>name:${name!''}</body>


四、编写controller

@GetMapping("/index")public String index(ModelMap modelMap, @RequestParam String code){    modelMap.put("code",code);    modelMap.put("name","和讯");    return "index";}


五、运行

特别说明,在运行前需要在pom文件中增加项目结构的配置,不然webapp路径可能不能被编译。
配置如下:

<build>    <resources>        <resource>            <directory>src/main/webapp</directory>        </resource>        <resource>            <directory>src/main/resources</directory>        </resource>        <resource>            <directory>src/main/java</directory>            <includes>                <include>**/*.xml</include>            </includes>        </resource>    </resources></build>

启动应用
访问:
http://localhost:9000/demo/index?code=hexun
这里写图片描述


SpringBoot集成freemarker 到此完成,主要引入一个依赖包,配置yml文件即可。

本篇结束,谢谢!

更多内容请关注微信公众号:

这里写图片描述

原创粉丝点击