之前在学习SpringMVC时,被讲师推荐了国内一款模板引擎Beetl,随后在学习和搭建项目中,大多数使用的都是Beetl,这款引擎虽然有些小众,但其部署和使用方式与当前国外的例如Freemarker之类并无差距,甚至我更衷意于Beetl。
在学习和使用SpringBoot时,我也倾向于使用Beetl作为模板引擎,与SpringMVC的XML配置方式不同,SpringBoot使用注解配置,为此,我查阅了一些资料,作为SpringBoot的学习笔记来记录。
首先,加入Maven依赖:
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
然后在config包下创建BeetlConfig配置类:
@Configuration
public class BeetlConfig {
//Beetl配置类
@Bean(name = "beetlConfigurer")
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration(){
BeetlGroupUtilConfiguration beetlGroupUtilConfiguration=new BeetlGroupUtilConfiguration();
ResourcePatternResolver patternResolver= ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
try{
WebAppResourceLoader webAppResourceLoader=new WebAppResourceLoader(patternResolver.getResource("classpath:/").getFile().getPath());
beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader);
}catch (IOException e){
System.out.println(e.getMessage());
}
beetlGroupUtilConfiguration.init();
return beetlGroupUtilConfiguration;
}
//Beetl视图解析类
@Bean(name = "beetlViewResolver")
public BeetlSpringViewResolver getBeetlSpringViewResoler(@Qualifier("beetlConfigurer") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration){
BeetlSpringViewResolver beetlSpringViewResolver=new BeetlSpringViewResolver();
beetlSpringViewResolver.setPrefix("/templates/");
beetlSpringViewResolver.setSuffix(".html");
beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
beetlSpringViewResolver.setOrder(0);
beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
return beetlSpringViewResolver;
}
}
注意在视图解析类中我把解析匹配后缀改为.html,其默认为.btl
如果还希望更改一些自定义配置,可以在application.properties的同级目录下创建beetl.properties文件.
我的自定义配置如下:
##默认配置
DELIMITER_PLACEHOLDER_START=${
DELIMITER_PLACEHOLDER_END=}
DELIMITER_STATEMENT_START=@
DELIMITER_STATEMENT_END=
具体自定义配置可以参考Beetl官网文档。
来源:CSDN
作者:铁血丹心.
链接:https://blog.csdn.net/weixin_44874115/article/details/104113362