springboot+freemarker实现动态表格输出

1.添加依赖:

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.创建测试实体

import lombok.Data;
import java.io.Serializable;

@Data
public class User implements Serializable {
    private int id;
    private String name;
    private int age;
}

3. 创建 html 转 string 的 Utils

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

public class MakeHtmlUtils {

    /**
     * 生成带数据的html
     * @param data 数据
     * @param fileName html文件名
     * @return
     */
    public static String MakeHtml(Map<String, Object> data, String fileName){
        // 第一步:创建一个Configuration对象。
        Configuration configuration = new Configuration(Configuration.getVersion());
        // 创建一个Writer对象。
        StringWriter out = new StringWriter();
        try {
            // 第二步:设置模板文件所在的路径。
            configuration.setDirectoryForTemplateLoading(new File("D:\\workspace\\communicate\\communicate-api\\src\\main\\resources\\templates"));
            // 第三步:设置模板文件使用的字符集。
            configuration.setDefaultEncoding("utf-8");
            // 第四步:加载一个模板,创建一个模板对象。
            Template template = configuration.getTemplate(fileName);
            // 第五步:调用模板对象的process将数据塞入模板中。
            template.process(data, out);
            //返回处理好的HTML字符串
            return out.toString();
        } catch (IOException | TemplateException e) {
            return "error";
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


}

4.controller测试类

import com.alibaba.fastjson.JSONArray;
import com.geely.office.im.common.util.MakeHtmlUtils;
import com.geely.office.im.model.entity.AdsFinancingCompanyAllIndexesDfPo;
import com.geely.office.im.model.entity.DataAuditVo;
import com.geely.office.im.model.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;

@Controller
public class UserController {

    @GetMapping("/msg")
    @ResponseBody
    public String index(){

        List<User> users = new ArrayList<>();

        for (int i = 0; i < 6; i++) {
            User user = new User();
            user.setId(i);
            user.setName("liudy"+i);
            user.setAge(18);
            users.add(user);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("users", users);
        data.put("group1", "班级一");

        String result = MakeHtmlUtils.MakeHtml(data, "user.html");

        return result;
    }
}

5.访问测试地址(测试效果)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot是一个用于构建独立的、基于生产级别的Spring应用程序的框架。而Freemarker是一种模板引擎,用于生成动态内容,特别适合Web应用程序的开发。在Spring Boot中使用Freemarker可以通过引入相应的依赖和配置来实现。 首先,在Spring Boot工程中引入Freemarker依赖。可以通过在pom.xml文件中添加以下依赖来实现: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 这样,在工程中就可以使用Freemarker来处理模板了。如果想要修改模板文件的位置等配置,可以在application.properties文件中进行配置。比如可以通过以下配置来指定模板文件的位置: ``` spring.freemarker.allow-request-override=false spring.freemarker.allow-session-override=false spring.freemarker.cache=false spring.freemarker.charset=UTF-8 spring.freemarker.check-template-location=true spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.suffix=.ftl spring.freemarker.template-loader-path=classpath:/templates/ ``` 这样,Spring Boot就会根据这些配置来加载并解析模板文件,生成相应的动态内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Spring Boot 整合 Freemarker](https://blog.csdn.net/yaxuan88521/article/details/117173289)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Springboot整合FreeMarker](https://blog.csdn.net/m0_67402096/article/details/126114796)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值