springboot集成模板Freemarker

本文详细介绍了如何在SpringBoot项目中集成Freemarker模板引擎,包括搭建环境、配置参数、创建控制器及模板文件等步骤,适用于快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot集成模板Freemarker:

  •    第一步:新建maven项目导入依赖项:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot_parent</artifactId>
        <groupId>cn.mesmile</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>_04_formwork_springboot</artifactId>

        <parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>2.0.5.RELEASE</version>
	</parent>

    <dependencies>

        <!-- springboot web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- servlet 依赖. -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- 模板依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

    </dependencies>

</project>
  • 第二步:新建一个sprinboot入口类
package cn.mesmile;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.swing.*;

/**
 * @Created with IDEA
 * @author: Super Zheng
 * @Description: java类作用描述
 * @Date:2019/1/3
 * @Time:12:00
 */
@SpringBootApplication
public class HelloFreemaker {

    public static void main(String[] args) {
        // 启动springboot应用
        SpringApplication.run(HelloFreemaker.class);
    }
}
  • 第三步:新建一个模板,注意模板的后缀为 .ftl  
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <title>Hello World!</title>
</head>
<body>
<h1>Hello , ${message}</h1>
</body>
</html>
  • 第四步:配置模板的相关信息
# FreeeMarker 模板引擎配置
spring.freemarker.allow-request-override=false
spring.freemarker.cache=false
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list
#spring.freemarker.view-names= # whitelist of view names that can be resolved
  • 第五步:写一个controller类
package cn.mesmile.controller;

import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Created with IDEA
 * @author: Super Zheng
 * @Description: controller类
 * @Date:2019/1/3
 * @Time:12:02
 *
 * @RestController = @ResponseBody+@Controller
 */
@RestController
public class FreemarkerController {

    @RequestMapping("/index")
    public ModelAndView freemarker(Model model){
        // 给模板中的对应字段设置值
        model.addAttribute("message", "freemarker");
        // 返回页面
        return new ModelAndView("index");
    }

    @RequestMapping("/index2")
    public String freemarker2(Model model){
        // 给模板中的对应字段设置值
        model.addAttribute("message", "freemarker");
        // 返回json
        return "index";
    }
}
  • 第六步:启动sprinboot入口类

  •  第六步:测试

  • 整个项目结构图:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值