前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot集成FreeMarker

Spring Boot集成FreeMarker

作者头像
小诸葛
发布2020-04-14 15:53:18
4300
发布2020-04-14 15:53:18
举报
文章被收录于专栏:方法论方法论

前言

FreeMarker是一种Java模板引擎,类似jsp、themleaf,FreeMarker适合用于页面伪静态化,方便爬虫爬取,比较适用于官网。

如何使用?

1.pom文件引入依赖

代码语言:javascript
复制
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>

2.在application.yml文件中配置参数

代码语言:javascript
复制
server:
  port: 8080
spring:
  #配置freemarker
  freemarker:
    template-loader-path: classpath:/templates
    charset: UTF-8
    check-template-location: true
    content-type: text/html
    expose-request-attributes: true
    expose-session-attributes: true
    request-context-attribute: request
    suffix: .ftl
    #关闭缓存,及时刷新,上线需要改成true
    cache: false

3.在resources文件夹下新建templates,在templates文件夹下新建index.ftl文件,内容如下:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试free marker</title>
</head>
<body>
<h1>${userName},Welcome to Freemaker</h1>
</body>
</html>

4.编写Controller

代码语言:javascript
复制
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Ldkj_FreeMarkerController {

    @RequestMapping("/testFree")
    public ModelAndView testFree(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("userName","小诸葛");
        return modelAndView;
    }
}

5.启动项目,在地址栏输入:http://localhost:8080/testFree访问,访问结果如图1-1所示。

图1-1 访问结果

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小诸葛的博客 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档