首页 > 其他分享 >SpringBoot整合Freemarker实现页面静态化

SpringBoot整合Freemarker实现页面静态化

时间:2022-10-25 20:31:33浏览次数:60  
标签:map SpringBoot Freemarker spring boot template 页面 模板 out

第一步:创建项目添加依赖:

<!--web和actuator(图形监控用)基本上都是一起出现的-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

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

第二步:修改application.yml文件:

spring:
  freemarker:
    charset: UTF-8  #设定Template的编码
    suffix: .ftl #后缀名
    template-loader-path: classpath:/templates/  #模板加载路径,多个以逗号分隔,默认: [“classpath:/templates/”]
    cache: false  #缓存配置,是否开启template caching
    enabled: true #是否允许mvc使用freemarker

第三步:在resources/templates目录下创建模板文件index.ftl:

<html>
	<head>
		<title>${title}</title>
	</head>
	<body>
		<h2>${msg}</h2>
	</body>
</html>

第四步:创建代码静态化工具类:

@Component
public class GenUtil {

    //创建Freemarker配置实例
    @Resource
    private Configuration configuration;

    /**
     * 根据模板,利用提供的数据,生成文件
     *
     * @param sourceFile 模板文件,带路径
     * @param data       数据
     * @param aimFile    最终生成的文件,若不带路径,则生成到当前项目的根目录中
     */
    public void gen(String sourceFile, String aimFile, Map<String, Object> data) {
        try {
            //加载模板文件
            Template template = configuration.getTemplate(sourceFile);
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(aimFile), StandardCharsets.UTF_8));
            template.process(data, out);
            out.flush();
            out.close();
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
    }

}

第五步:静态化测试

@SpringBootTest
public class GenTest {
    @Resource
    private GenUtil genUtil;
    @Test
    void fun(){
        Map<String, Object> map = new HashMap<>();
    	map.put("title", "首页");
    	map.put("msg", "好好学习,天天向上!");
    	FreemarkerUtil.execute("index.ftl", "haha.html", map);
    }
}

测试

运行测试代码发现在当前项目根目录下生成了一个haha.html的文件。

标签:map,SpringBoot,Freemarker,spring,boot,template,页面,模板,out
From: https://blog.51cto.com/lianghecai/5795097

相关文章

  • 【springboot】静态方法从用户协议头获取信息
     publicstaticIntegergetUid(){HttpServletRequestrequest=((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();......
  • VueRouter 实现登录后跳转到之前相要访问的页面的简单示例
    简介该功能主要用于判定用户权限,在用户无权限时重定向至登录页,并在用户完成登录后,再定向至用户之前想要访问的路由;或者用户在任意路由点击登录时,登录成功后返回当前路由。......
  • Springboot中@Value的使用详解
    Springboot中@Value的使用详解Springboot通过@Value注解将配置文件中的属性注入到容器内组件中(可用在@Controller、@Service、@Configuration、@Component等Spring托管的......
  • SpringBoot2配置HikariCP连接池的密码保护
    本文讨论如何保护SpringBoot配置文件中的数据库连接信息,一般情况下application.properties里会如下配置DataSource:spring.datasource.driver-class-name=com.mysql.cj.jdbc.......
  • SpringBoot2配置HikariCP连接池的密码保护
    本文讨论如何保护SpringBoot配置文件中的数据库连接信息,一般情况下application.properties里会如下配置DataSource:1spring.datasource.driver-class-name=com.mysql.cj.......
  • SpringBoot使用WebUploader做大文件的分块和断点续传
    ​ 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现。先说下要求:PC端全平台支持,要求支持Windows,Mac,Linux......
  • js禁止浏览器操作页面回退
    浏览器实现页面回退的三个步骤://1、在刚进入页面时在浏览器历史记录中加一条当前页面的记录window.addEventListener('popstate',this.popstateFun,false);//2、监听......
  • SpringBoot 配置内部tomcat https双向验证
    1.在application.properties或者application.yml配置文件中加入server:port:8443ssl:key-store:classpath:xxxx.jks#xxxx.jks的别名key-ali......
  • html2Canvas 前端保存页面为图片
    html2canvas 能够实现在用户浏览器端直接对整个或部分页面进行截屏。这个html2canvas脚本将当页面渲染成一个canvas图片,通过读取DOM并将不同的样式应用到这些元素上实现。......
  • Koa扩展之下载文件和访问HTML页面
    下载文件和访问HTML页面示例PS:在项目的根目录新增static文件夹,static文件夹中包含load.html页面和模板文件.xlsx。constkoa=require('koa');constRouter=require(......