Spring Boot:集成 Beetl 模板引擎 + 打包 Jar 后分离模板

此文章发布于 59 个月前,部分信息可能已经过时,请自行斟酌确认。

扩展阅读


BeetlBee Template Language 的缩写,官方称是新一代的模板引擎,它功能强大,性能良好,超过当前流行的模板引擎,官方称 Beetl 远超过主流 java 模板引擎性能(引擎性能5-6倍与 freemaker,2倍于JSP),国产且易学易用。

本文是我基于 Spring Boot 集成 Beetl 过程的简单记录。

官方文档:

http://ibeetl.com/guide/#beetl

Spring Boot 集成 Beetl 模板引擎

1、添加依赖

注意针对 spring boot 有单独的 beetl-framework-starter 可引用,使用更方便。

compile group: 'com.ibeetl', name: 'beetl-framework-starter', version: '1.2.13.RELEASE'

2、配置

beetl.templatesPath = templates
beetl.suffix = html

3、自定义 Beetl 配置(可选,若分离模板则必须)

项目目录下新建config/BeetlConf.java添加以下代码进行

/**
 * Beetl 模板配置
 * 作者:xinggang
 * 邮箱:willcoo@qq.com
 * 网址:https://weiku.co
 * 日期:2019-08-29
 * 说明:
 */
@Configuration
public class BeetlConf {

    @Value("${beetl.templatesPath}")
    String templatesPath; //模板根目录 ,比如 "templates"

    @Bean(name = "beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {

        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();

        //模板分离,打包成 jar 后模板文件放在 jar 所在目录的 resources 目录下
        ApplicationHome home = new ApplicationHome(getClass());
        String rootPath;
        if (home.getSource().getPath().endsWith(".jar")) {
            rootPath = home.getDir().getPath(); //打包后jar包目录
        } else {
            rootPath = home.getDir().getParent(); //开发环境目录
        }
        String root = rootPath + File.separator + "resources/" + templatesPath;
        FileResourceLoader resourceLoader = new FileResourceLoader(root, "utf-8");
        beetlGroupUtilConfiguration.setResourceLoader(resourceLoader);
        beetlGroupUtilConfiguration.init();
        return beetlGroupUtilConfiguration;
    }

    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setSuffix(".html"); //此处设置为.html后在controller中就不能再写return "index.html",要写return "index"
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }
}

4、编写 Controller

@Controller
public class HomeController {

    @RequestMapping("/")
    public String index(Model model) {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", "xg");
        map.put("pwd", "263499118");
        map.put("mail", "www@weiku.co");
        map.put("age", 16);
        model.addAttribute("name", "admin");
        model.addAttribute("data", map);
        return "index.html";
    }
}

5、编写模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
beetl
你好:${name},邮箱:${data.mail}
</body>
</html>

6、运行

浏览即可:http://localhost:8080/

个性化配置

1、修改定界符为 @

默认的 <% %> 用着相当不舒服,改成 @ 好多了

模板开发者可以创建一个 beetl.properties 的配置文件,此时,该配置文件将覆盖默认的配置文件属性,比如,你的定界符考虑是<!--: 和 -->,则在 beetl.properties 加入一行即可,并将此配置文件放入Classpath根目录下即可。 Configuration.defaultConfiguration() 总是先加载系统默认的,然后再加载Beetl.properties的配置属性,如果有重复,用后者代替前者的配置

resources 目录下新建 beetl.properties ,增加两行配置:

DELIMITER_STATEMENT_START = @
DELIMITER_STATEMENT_END = null 

使用示例:

@if(1==1){
<p>当前日期: ${strutil.formatDate(date(),'yyyy-MM-dd')}</p>
@}

完美。

2、Srping Boot 打包 jar 发布后分离模板到单独目录

项目上线后想随时可以修改html模板文件肯定有不少小伙伴们都喜欢这样搞,解决方案如下。

如上面集成步骤中的第3步,需要配置BeetlConf.java,请查看其中的代码。

其中将官方代码中的ClasspathResourceLoader 改为了FileResourceLoader,因为在IDEA开发环境运行时和打包成 jar 运行时的目录有区别所以单独处理。

开发环境:不需要任何处理还是将模板文件放到resources/templates下面,运行时 IDEA 会自动拷贝到项目目录\out\production\resources\templates

打包jar后:将模板文件放到jar 包同级目录下的 resources/templates下即可,需要注意的是启动 jar 之前要确保 resources 目录存在,否则启动后再创建目录放上模板会会访问不到。

示例源码

https://github.com/ifu25/spring-boot-beetl-demo

最后修改:2019 年 08 月 30 日 11 : 12 PM
如果觉得我的文章对你有用,请随意赞赏

发表评论