SpringBoot学习-(二十)SpringBoot整合Freemarker

基本步骤

  • 添加pom依赖
  • 在application.yml中添加相关配置
  • 创建freemarker模板
  • 创建控制层
  • 测试访问

添加pom依赖

<!-- springboot整合freemarker -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

在application.yml中添加相关配置

# 配置freemarker
spring:
  freemarker:
    # 设置模板后缀名
    suffix: .ftl
    # 设置文档类型
    content-type: text/html
    # 设置页面编码格式
    charset: UTF-8
    # 设置页面缓存
    cache: false
    # 设置ftl文件路径
    template-loader-path:
      - classpath:/templates
  # 设置静态文件路径,js,css等
  mvc:
    static-path-pattern: /static/**

创建freemarker模板

目录:src/main/resources 创建templates文件夹,文件夹里新建freemarker.ftl文件

<!DOCTYPE>
<html>
    <head>
        <title>freemark</title>
    </head>
    <body>
        <h1>Hello ${name} from resource freemark!</h1>
    </body>
</html>

创建控制层

package com.ahut.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * @ClassName: FreemarkerAction
 * @Description: freemarker控制层
 * @author cheng
 * @date 2018年1月22日 下午8:19:39
 */
@Controller
@RequestMapping(value = "/freemarker")
public class FreemarkerAction {
   

    /**
     * 日志管理
     */
    private static Logger log = LoggerFactory.getLogger(FreemarkerAction.class);

    /**
     * 
     * @Title: toDemo
     * @Description: 跳转freemarker页面
     * @param mv
     * @return
     */
    @RequestMapping(value = "/toDemo")
    public ModelAndView 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值