Spring Boot集成

  1. <dependency>
  2. <groupId>com.ibeetl</groupId>
  3. <artifactId>beetl-framework-starter</artifactId>
  4. <version>1.2.11.RELEASE</version>
  5. </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

  1. @Configuration
  2. public MyConfig{
  3. @Bean
  4. public BeetlTemplateCustomize beetlTemplateCustomize(){
  5. return new BeetlTemplateCustomize(){
  6. public void customize(GroupTemplate groupTemplate){
  7. }
  8. };
  9. }
  10. }

使用Starter来配置已经够用,如果你想自己配置模板引擎, 通过java config来配置 beetl需要的BeetlGroupUtilConfiguration,和 BeetlSpringViewResolver,参考代码如下

  1. @Configuration
  2. public class BeetlConf {
  3. @Value("${beetl.templatesPath}") String templatesPath;//模板根目录 ,比如 "templates"
  4. @Bean(name = "beetlConfig")
  5. public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
  6. BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
  7. //获取Spring Boot 的ClassLoader
  8. ClassLoader loader = Thread.currentThread().getContextClassLoader();
  9. if(loader==null){
  10. loader = BeetlConf.class.getClassLoader();
  11. }
  12. beetlGroupUtilConfiguration.setConfigProperties(extProperties);//额外的配置,可以覆盖默认配置,一般不需要
  13. ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader,
  14. templatesPath);
  15. beetlGroupUtilConfiguration.setResourceLoader(cploder);
  16. beetlGroupUtilConfiguration.init();
  17. //如果使用了优化编译器,涉及到字节码操作,需要添加ClassLoader
  18. beetlGroupUtilConfiguration.getGroupTemplate().setClassLoader(loader);
  19. return beetlGroupUtilConfiguration;
  20. }
  21. @Bean(name = "beetlViewResolver")
  22. public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
  23. BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
  24. beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
  25. beetlSpringViewResolver.setOrder(0);
  26. beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
  27. return beetlSpringViewResolver;
  28. }
  29. }

注意:这里并没有配置后缀,因此controller代码里必须显式的加上后缀

  1. //return "/hello" 错误用法
  2. return "hello.html"

注意,可以通过Application.properties 配置如下属性禁用BeetlSQL或者禁用Beetl

  1. beetlsql.enabled=false
  2. beetl.enabled=false