Spring Boot使用freemarker并且生成静态html页面

Spring Boot使用freemarker并且生成静态html页面

之前我介绍了在spring boot中使用thymeleaf模板,这次我会给大家介绍在spring boot中使用freemarker模板技术,同时利用freemarker生成静态html页面。生成静态html页面就能实现网站的静态化进而提高网站的访问速度以及提高SEO能力。
首先在pom.xml中添加依赖

添加依赖

<dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.23</version>
        </dependency>

application配置

在application.properties中添加freemarker的配置参数

##freemarker
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates

Controller和ftl模板

下一步我们就建一个基础Controller类和配套的ftl模板
Controller类

package com.hw.myp2c.common.controller;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.io.*;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("")
public class MainController {

    @GetMapping
    public String main(Model model){
        String w="Welcome FreeMarker!";
        Map root = new HashMap();
        root.put("w",w);
        model.addAttribute("w","Welcome FreeMarker!");
        return "test";
    }
}

可以看到很简单,跟之前的thymelefa和jsp的没有区别。
freemarker模板

<html>
<head>
    <title>Welcome!</title>
    <link rel="stylesheet" href="/bootstrap.min.css">
    <script src="/lib/jquery.min.js"></script>
</head>
<body>
<h1>Hello ${w}!</h1>
</body>
</html>

这样之后我们就能完成了基础freemarker的使用,更多的使用参见freemarker官方网站,这里不做过多的描述。
这里我们已经完成了标准的freemarker集成,下面我们将介绍如何利用freemarker生成静态html页面,直接上代码,作为演示我们还是在Controller中完成,在实际应用中我们可以按照自己的实际需要进行封装。

package com.hw.myp2c.common.controller;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.io.*;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("")
public class MainController {

    @Resource
    Configuration cfg;

    @GetMapping
    public String main(Model model){
        String w="Welcome FreeMarker!";
        Map root = new HashMap();
        root.put("w",w);
        freeMarkerContent(root);
        model.addAttribute("w","Welcome FreeMarker!");
        return "test";
    }

    private void freeMarkerContent(Map<String,Object> root){
        try {
            Template temp = cfg.getTemplate("test.ftl");
            //以classpath下面的static目录作为静态页面的存储目录,同时命名生成的静态html文件名称
            String path=this.getClass().getResource("/").toURI().getPath()+"static/test.html";
            Writer file = new FileWriter(new File(path.substring(path.indexOf("/"))));
            temp.process(root, file);
            file.flush();
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}

利用freemarker生成静态页面我理解的流程是这样的
1.利用Configuration读取想生成静态页面的模板,这里是test.ftl
2.解析模板文件,并将模板中的${}!包含的参数替换成真实的数据
3.最终将读取了真实数据的模板生成相应的html文件,并写入指定目录
这样我们就完成了spring boot中使用freemarker模板,并且利用freemarker生成静态html文件

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Spring Boot整合邮件发送并保存历史发送邮箱 项目描述 项目主要是使用 Spring Boot 发送邮件,主要的技术点有: 1、Spring Boot +mybatis的整合 2、Spring Boot项目中jsp的使用 3、Spring Boot 发送邮件(文本格式的邮件、发送HTML格式的邮件、发送带附件 的邮件、发送带静态资源的邮件) 个人觉得Springboot的开发简单的归纳为三步jar包引入,配置,应用。 (一)简单使用 1)JSP的使用配置 Spring Boot整合邮件发送并保存历史发送邮箱 Spring Boot整合邮件发送并保存历史发送邮箱 2) 邮件发送服务 1、pom 包配置 pom.xml 引入加 spring-boot-starter-mail 依赖包: Spring Boot整合邮件发送并保存历史发送邮箱 2、配置文件 application.yml Spring Boot整合邮件发送并保存历史发送邮箱 注意:测试时需要将 spring.mail.username 和 spring.mail.password 改成自己邮箱对应的登录名和密码,这里的密码不是邮箱的登录密码,是开启 POP3 之后设置的客户端授权密码。 MailServiceImpl.java JavaMailSender (1)Spirng 已经帮我们内置了 JavaMailSender,直接在项目中引用即可。我们封装一个 MailService 类来实现普通的邮件发送方法。 Spring Boot整合邮件发送并保存历史发送邮箱 from,即为邮件发送者; to,邮件接收者; subject,邮件主题; content,邮件的主体。 邮件发送者 from 一般采用固定的形式写到配置文件中。 (2)富文本邮件 在日常使用的过程中,通常在邮件中加入图片或者附件来丰富邮件的内容 发送 HTML 格式邮件 邮件发送支持以 HTML 的形式去构建我们喜欢的文本格式,SpringHTML 格式的邮件也做出了支持,非常方便使用。 我们在 MailService 中添加支持 HTML 发送的方法. Spring Boot整合邮件发送并保存历史发送邮箱 和上面对比,这次发送邮件使用 MimeMessageHelper 类。MimeMessageHelper 支持发送复杂邮件模板,支持文本、附件、HTML、图片等,接下来我们会继续使用。 (3)发送带附件的邮件 在 MailService 添加 sendAttachmentsMail 方法。 Spring Boot整合邮件发送并保存历史发送邮箱 (4)发送带静态资源的邮件 邮件中的静态资源一般就是指图片,在 MailService 添加 sendAttachmentsMail 方法。 Spring Boot整合邮件发送并保存历史发送邮箱 相关测试在这里就省略了 (二)本项目中主要以发送 HTML 格式邮件为例,发送邮件并把邮箱保存到数据库中 FreeMarker模板引擎 Spring Boot整合邮件发送并保存历史发送邮箱 邮件模板 Spring Boot整合邮件发送并保存历史发送邮箱 运行环境 jdk8+tomcat8+mysql+IntelliJ IDEA+maven 项目技术(必填) springboot +mybatis +jquery+jsp 数据库文件 压缩包内 jar包文件 maven搭建

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值