Springboot 集成 Freemarker

本文介绍如何在SpringBoot项目中集成Freemarker模板引擎,包括添加依赖、配置项目及编写示例代码。

Springboot 集成 Freemarker

网址

Freemarker 官网:https://freemarker.apache.org/

Freemarker 手册网址:http://freemarker.foofun.cn/

spring-boot-starter-freemarker maven地址:http://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker

Freemarker maven地址:http://mvnrepository.com/artifact/org.freemarker/freemarker

集成

在 pom.xml 添加依赖

<!--springboot默认支持freemarker,无需添加版本号-->
<!-- freemarker start -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- freemarker end  -->

项目

pom.xml
<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>top.simba1949</groupId>
    <artifactId>Springboot-Freemarker</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 继承spring-boot-start-parent -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <!--配置管理-->
    <properties>
        <!--配置项目编码-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--jdk编译版本-->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <!--依赖管理-->
    <dependencies>
        <!--springboot-web start-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--springboot-web  end-->

        <!--springboot test start-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--springboot test   end-->

        <!-- freemarker start -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <!-- freemarker end   -->
    </dependencies>
</project>
templates 目录

在 src/resources目录下创建 templates 文件夹,用于放置 freemarker 模板文件

user.ftl 文件

<html>
<head>
    <title>Welcome!</title>
</head>
<body>
    <#-- Greet the user with his/her name -->
    <h1>Welcome</h1>
    <p>We have these animals:
    <ul>
    ${user}
    </ul>
</body>
</html>
java 代码

启动类 App.java

package top.simba1949;

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

/**
 * @author simba@onlying.cn
 * @date 2018/7/13 19:38
 */
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

UserController.java

package top.simba1949.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author simba@onlying.cn
 * @date 2018/7/13 19:39
 * a.这里不是走 HTTP + JSON 模式,使用了 @Controller 而不是先前的 @RestController
 * b.方法返回值是 String 类型,和 application.properties 配置的 Freemarker 文件配置路径下的各个 *.ftl 文件名一致。这样才会准确地把数据渲染到 ftl 文件里面进行展示。
 * c.用 Model 类,向 Model 加入数据,并指定在该数据在 Freemarker 取值指定的名称。
 */
@RequestMapping("/user")
@Controller
public class UserController {

    @GetMapping
    public String demo(Model model){
        model.addAttribute("user","springboot集成freemarker");
        // 在 templates 目录下多了一层目录 user
        // return "user/user"; // 俩个return都可以
        return "/user/user";
    }
}
访问测试
http://localhost:8888/user

这里写图片描述

Spring Boot可以集成Freemarker作为视图格式,以便在Spring MVC中使用。为了创建一个Spring Boot Freemarker工程并测试模板,你需要执行以下步骤: 1. 创建一个测试工程,并在pom.xml文件中添加必要的依赖项。其中包括以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> ``` 2. 创建一个Controller类,在这个类中将需要的数据添加到Map中,并返回模板文件的名称。例如: ``` @Controller public class HelloController { @GetMapping("/basic") public String test(Model model) { // 纯文本形式的参数 model.addAttribute("name", "freemarker"); // 实体类相关的参数 Student student = new Student(); student.setName("小明"); student.setAge(18); model.addAttribute("stu", student); return "01-basic"; } } ``` 3. 创建一个启动类,使用@SpringBootApplication注解标记,并在main方法中运行Spring Boot应用程序。例如: ``` @SpringBootApplication public class FreemarkerDemotApplication { public static void main(String[] args) { SpringApplication.run(FreemarkerDemotApplication.class, args); } } ``` 4. 运行测试,访问对应的URL来查看Freemarker模板渲染的结果。 以上是使用Spring Boot集成Freemarker的基本步骤。你可以根据具体的需求和业务逻辑进行相应的调整和配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Springboot集成Freemarker|超级详细,建议收藏](https://blog.csdn.net/heima005/article/details/129435750)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Springboot整合FreeMarker](https://blog.csdn.net/zxy15974062965/article/details/122818338)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Simba1949

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值