博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot 2.0 整合 FreeMarker 模板引擎
阅读量:7014 次
发布时间:2019-06-28

本文共 2842 字,大约阅读时间需要 9 分钟。

本篇博文将和大家一起使用Spring Boot 2.0 和FreeMarker 模板引擎整合实战。

1. 创建新的项目

 

2. 填写项目配置信息

3. 勾选web 模块

4. 勾选freemarker模板引擎模块

 5.填写项目名称和项目保存路径

6. 修改POM文件,添加Freemarker 项目依赖

4.0.0
com.xingyun
spring-boot-with-freemarker-sample
0.0.1-SNAPSHOT
jar
spring-boot-with-freemarker-sample
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin

7. 配置applicaiton.properties

# 设定ftl文件路径spring.freemarker.template-loader-path=classpath:/templatesspring.freemarker.cache=falsespring.freemarker.charset=UTF-8spring.freemarker.check-template-location=truespring.freemarker.content-type=text/htmlspring.freemarker.expose-request-attributes=falsespring.freemarker.expose-session-attributes=falsespring.freemarker.request-context-attribute=requestspring.freemarker.suffix=.ftl

Tips: 其实我试了下,把.ftl 换成.jsp ,创建JSP 文件也是可以的。

8. 创建文件夹和ftl格式文件

index.ftl 

    
Titlethis is index page

welcome.ftl

    
Titlethis is welcome page

9. 创建 Controller

package com.xingyun.springbootwithfreemarkersample.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@Controllerpublic class HomeController {    @RequestMapping(value = "/")    public String index(){        return "views/index";    }    @RequestMapping(value = "/welcome")    public String home(){        return "views/welcome";    }}

Tips: 由于要返回模板页面文件,所以我们只能使用@Controller 而不可以使用@RestController

10. 访问

11. 访问 

转载地址:http://mfqtl.baihongyu.com/

你可能感兴趣的文章
双机热备份的切换时间是这么确定的
查看>>
ADSL拨号上网,拨完了获得的地址IP跟网关一样!
查看>>
Socket.IO连接异常时的内置事件流程图
查看>>
算法学习笔记(四)---第k个二进制数字问题
查看>>
忘记sa密码,又删除了windows身份验证账号的解决方法
查看>>
Open-Falcon 监控系统监控 MySQL/Redis/MongoDB 状态监控
查看>>
1-4 Zabbix 用户管理
查看>>
Visual Studio 2017强制更新方法
查看>>
蓝牙设备探测工具blueranger
查看>>
我的友情链接
查看>>
KaliLinux常用服务配置教程DHCP服务工作流程
查看>>
js基础--javascript基础概念之数组
查看>>
date 输出格式
查看>>
Android控件TextView的实现原理分析
查看>>
java中异常的处理
查看>>
我的友情链接
查看>>
最小生成树算法——Prim和Kruskal算法的实现
查看>>
java循环中如何删除集合中的元素
查看>>
JFinal 2.2 国际化功能的应用
查看>>
Kafka性能调优
查看>>