SpringBoot集成Beetl

一、什么是Beetl?
带你了解什么是Beetl。

二、创建SpringBoot项目

三、Maven引入Beetl依赖

 <!-- beetl模板 -->
        <dependency>
            <groupId>com.ibeetl</groupId>
            <artifactId>beetl</artifactId>
            <version>2.9.8</version>
        </dependency>

四、通过Java config来配置配置 beetl需要的BeetlGroupUtilConfiguration,和 BeetlSpringViewResolver

import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;

@Configuration
public class BeetlTemplateConfig {
    @Value("${RESOURCE.root:/views}")
    String resourceRoot;// 模板跟目录

    @Bean(initMethod = "init", name = "beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        ClasspathResourceLoader classPathLoader = new ClasspathResourceLoader(this.getClass().getClassLoader(),
                resourceRoot);
        beetlGroupUtilConfiguration.setResourceLoader(classPathLoader);
        // 读取配置文件信息
        ResourcePatternResolver patternResolver = ResourcePatternUtils
                .getResourcePatternResolver(new DefaultResourceLoader());
        beetlGroupUtilConfiguration.setConfigFileResource(patternResolver.getResource("classpath:beetl.properties"));
        return beetlGroupUtilConfiguration;
    }

    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(
            @Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        // beetlSpringViewResolver.setPrefix("/");
        beetlSpringViewResolver.setSuffix(".html");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }
}

beetl.properties

#配置引擎实现类
ENGINE=org.beetl.core.engine.FastRuntimeEngine
#指定了占位符号,默认是${ }.也可以指定为其他占位符
DELIMITER_PLACEHOLDER_START=${
DELIMITER_PLACEHOLDER_END=}
#指定了语句的定界符号,默认是<% %>,也可以指定为其他定界符号 //内置方法
DELIMITER_STATEMENT_START=@
DELIMITER_STATEMENT_END=null
#指定IO输出模式,默认是FALSE,即通常的字符输出,再考虑高性能情况下,可以设置成true
DIRECT_BYTE_OUTPUT=FALSE
#指定了支持HTML标签,且符号为#(只能用一个符号标示),默认配置下,模板引擎识别<#tag ></#tag>
#这样的类似html标签,并能调用相应的标签函数。
HTML_TAG_SUPPORT=true
HTML_TAG_FLAG=#
#指定允许本地Class直接调用
NATIVE_CALL=TRUE
#指定模板字符集是UTF-8
TEMPLATE_CHARSET=UTF-8
#指定异常的解析类,默认是ConsoleErrorHandler,他将在render发生异常的时候在后台打印出错误信息(System.out)。
ERROR_HANDLER=org.beetl.ext.web.WebErrorHandler
#指定了本地Class调用的安全策略
NATIVE_SECUARTY_MANAGER=org.beetl.core.DefaultNativeSecurityManager
#指定了默认使用的模板资源加载器
RESOURCE_LOADER=org.beetl.core.resource.ClasspathResourceLoader
#配置了是否进行严格MVC,通常情况下,此处设置为false.
MVC_STRICT=FALSE
#资源配置,resource后的属性只限于特定ResourceLoader
#classpath 根路径(其实就是前缀的配置)
#RESOURCE.root=/views
#是否检测文件变化
RESOURCE.autoCheck=true
#自定义脚本方法文件的Root目录和后缀
RESOURCE.functionRoot=functions
RESOURCE.functionSuffix=fn
#自定义标签文件Root目录和后缀
RESOURCE.tagRoot=htmltag
RESOURCE.tagSuffix=tag
FN.date=org.beetl.ext.fn.DateFunction
FN.nvl=org.beetl.ext.fn.NVLFunction
FNP.strutil=org.beetl.ext.fn.StringUtil

五、创建Controller测试

 @RequestMapping("/test")
    public ModelAndView test(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","jackson");
        modelAndView.setViewName("beetl/test");
        return modelAndView;
    }

代码中的ViewName路径为图中views文件夹中的beetl文件夹的test.html文件

test.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<body>
<hr>
获取项目的context-path-->${ctxPath}
<hr>
获取后台返回的数据--->${username}
</body>
</body>
</html>

注意:要输出${ctxPath},必须要在application.yml中配置context-path
在这里插入图片描述

目录结构
在这里插入图片描述

输出:
在这里插入图片描述

(想用更多的beetl的功能请自行看Beetl官方文档。。。。。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值