SpringBoot集成FreeMarker

环境

springboot2.4.3 + freemarker2.3.31 + bootstrap3

需求

浏览器中访问:http://localhost:8080/employee/list 列表员工数据

操作步骤

步骤1:创建项目 springboot-freemarker

步骤2:在/项目/resourcess/static 文件中导入bootstrap3相关文件

步骤3:导入依赖

核心依赖有3个:springboot parent  + web环境 + freemarker 依赖

在pom.xml文件中导入

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
    </dependencies>

步骤4:在/项目/resources/application.properties配置freemarker

#暴露session对象的属性
spring.freemarker.expose-session-attributes=true
#配置为传统模式,空值自动处理
spring.freemarker.settings.classic_compatible=true
#重新指定模板文件后缀 springboot 2.2.x 后 默认后缀为 .ftlh
spring.freemarker.suffix=.ftl  

其他配置

spring.freemarker.enabled=true: 是否开启freemarker支持
spring.freemarker.charset=UTF-8: 模板编码
spring.freemarker.content-type=text/html: 模板contenttype
spring.freemarker.expose-session-attributes: 是否开启session属性暴露,默认false
spring.freemarker.prefix: 加载模板时候的前缀
spring.freemarker.settings.*: 直接配置freemarker参数
spring.freemarker.suffix: 模板文件后缀
spring.freemarker.template-loader-path=classpath:/templates/: 模板加载地址

步骤5:编写相关相关代码

代码结构:

实体类:Employee

package com.langfeiyes.freemarker.entities;

/**
 * 员工对象
 */
public class Employee {
    private Long id;
    private String name;
    private int age;
    public Employee() {
    }
    public Employee(Long id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

接口类:EmployeeController

package com.langfeiyes.freemarker.controller;


import com.langfeiyes.freemarker.entities.Employee;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {


    //假装是从数据库中查询出来的
    private List<Employee> queryList(){
        List<Employee> list = new ArrayList<>();
        list.add(new Employee(1L, "zhangsan", 16));
        list.add(new Employee(2L, "lisi", 17));
        list.add(new Employee(3L, "wangwu", 18));
        list.add(new Employee(4L, "zhaoliu", 19));
        list.add(new Employee(5L, "qianqi", 20));
        return list;
    }

    /**
     * 员工列表
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model){
        model.addAttribute("emps", queryList());
        return  "employee/list"; //返回员工模板
    }

}

启动类:App

package com.langfeiyes.freemarker;


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

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

步骤6:编写员工列表模板

代码结构:

<html lang="en">
<head>
    <title>员工列表</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/bootstrap/css/bootstrap.css" type="text/css" />
    <script type="text/javascript" src="/bootstrap/js/bootstrap.js"></script>
</head>
<body>

<div class="container-fluid " style="margin-top: 20px">
    <div class="row">
        <div class="col-sm-10">
            <table class="table table-striped table-hover" >
                <thead>
                    <tr>
                        <th>序号</th>
                        <th>员工名称</th>
                        <th>员工年龄</th>
                        <th>操作</th>
                    </tr>
                </thead>
               <#list emps as e>
                   <tr>
                       <td>${e_index+1}</td>
                       <td>${e.name!}</td>
                       <td>${e.age!}</td>

                       <td>
                           <a class="btn btn-info btn-xs " href="javascript:;" >
                               <span class="glyphicon glyphicon-edit"></span> 编辑
                           </a>
                           <a href="javascript:;" class="btn btn-danger btn-xs ">
                               <span class="glyphicon glyphicon-trash"></span> 删除
                           </a>
                       </td>
                   </tr>
               </#list>
            </table>

        </div>
    </div>
</div>

</body>
</html>

步骤7:测试

右键执行App类

浏览器输入:http://localhost:8080/employee/list

显示最终效果:

 到这,Springboot集成freemarker成功。

案例源码:传送门

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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
发出的红包

打赏作者

浪飞yes

我对钱没兴趣~

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

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

打赏作者

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

抵扣说明:

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

余额充值