Springboot集成Beetl

方法一:

1.引入pom依赖

        
            com.ibeetl
            beetl-framework-starter
            1.1.81.RELEASE
        

stater自动处理以btl结尾的视图,模板根目录是Spring Boot默认的templates目录。

beetl.enabled 吗,默认为true,集成beetl

beetl.suffix 默认为btl,表示只处理视图后缀为btl的模板

ViewController

package com.czx.springbootbeetl.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ViewController {

    @GetMapping(value = {"/","/index"})
    public ModelAndView index() {
        return new ModelAndView("index.btl");
    }

}

index.btl




    
    index


    

hello beetl

效果:

Springboot集成Beetl_第1张图片

方法二:通过java config配置beetl,需要BeetlGroupUtilConfiguration和BeetlSpringViewResolver

package com.czx.springbootbeetl.config;

import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * beetl 模板引擎加载
 */
@Configuration
public class BeetlConf {

    @Bean
    public BeetlGroupUtilConfiguration beetlConfig() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        //获取Spring Boot 的ClassLoader
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if(loader==null){
            loader = BeetlConf.class.getClassLoader();
        }
        //beetlGroupUtilConfiguration.setConfigProperties(extProperties);//额外的配置,可以覆盖默认配置,一般不需要
        //templates 为模板根目录
        ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader,"templates");
        beetlGroupUtilConfiguration.setResourceLoader(cploder);
        beetlGroupUtilConfiguration.init();
        //如果使用了优化编译器,涉及到字节码操作,需要添加ClassLoader
        beetlGroupUtilConfiguration.getGroupTemplate().setClassLoader(loader);
        return beetlGroupUtilConfiguration;

    }

    @Bean
    public BeetlSpringViewResolver beetlViewResolver(BeetlGroupUtilConfiguration beetlConfig) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        //设置setSuffix后 controller 不需要加后缀
        beetlSpringViewResolver.setSuffix(".btl");
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlConfig);
        return beetlSpringViewResolver;
    }
}
ViewController:
package com.czx.springbootbeetl.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ViewController {

    @GetMapping(value = {"/","/index"})
    public ModelAndView index() {
        return new ModelAndView("index");
    }

}

效果:

Springboot集成Beetl_第2张图片

你可能感兴趣的:(Spring,Boot)