4.6. Spring Boot集成
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-framework-starter</artifactId>
<version>1.1.81.RELEASE</version>
</dependency>
starter 自动处理以btl结尾的视图,模板根目录是Spring Boot默认的templates目录。如下配置可以修改beetl部分属性
- beetl-beetlsql.dev,默认为true,即自动检查模板变化
- beetl.enabled 默认为true,集成beetl。
- beetl.suffix 默认为btl,表示只处理视图后缀为btl的模板,比如controller里代码是“return /common/index.btl”,则能被Beetl处理,你写成"return /common/index",或者"/common/index.html",都会出现404错误。
Starter可以实现BeetlTemplateCustomize来定制Beetl
@Configuration
public MyConfig{
@Bean
public BeetlTemplateCustomize beetlTemplateCustomize(){
return new BeetlTemplateCustomize(){
public void customize(GroupTemplate groupTemplate){
}
};
}
}
使用Starter来配置已经够用,如果你想自己配置模板引擎, 通过java config来配置 beetl需要的BeetlGroupUtilConfiguration,和 BeetlSpringViewResolver,参考代码如下
@Configuration
public class BeetlConf {
@Value("${beetl.templatesPath}") String templatesPath;//模板根目录 ,比如 "templates"
@Bean(name = "beetlConfig")
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
//获取Spring Boot 的ClassLoader
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if(loader==null){
loader = BeetlConf.class.getClassLoader();
}
beetlGroupUtilConfiguration.setConfigProperties(extProperties);//额外的配置,可以覆盖默认配置,一般不需要
ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader,
templatesPath);
beetlGroupUtilConfiguration.setResourceLoader(cploder);
beetlGroupUtilConfiguration.init();
//如果使用了优化编译器,涉及到字节码操作,需要添加ClassLoader
beetlGroupUtilConfiguration.getGroupTemplate().setClassLoader(loader);
return beetlGroupUtilConfiguration;
}
@Bean(name = "beetlViewResolver")
public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
beetlSpringViewResolver.setOrder(0);
beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
return beetlSpringViewResolver;
}
}
注意:这里并没有配置后缀,因此controller代码里必须显式的加上后缀
//return "/hello" 错误用法
return "hello.html"
注意,可以通过Application.properties 配置如下属性禁用BeetlSQL或者禁用Beetl
beetlsql.enabled=false
beetl.enabled=false
当前内容版权归 ibeetl.com 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 ibeetl.com .