不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2022-04-16 21:37:22  修改时间:2024-04-21 13:04:26  分类:Java框架/系统  编辑

一、增加Maven依赖包

<dependency>
	<groupId>com.ibeetl</groupId>
	<artifactId>beetl-framework-starter</artifactId>
	<version>1.2.40.Beetl.RELEASE</version>
</dependency>

二、新建一个Config文件,用来配置Beetl,代码如下:

package com.wanma.framework_web.config;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 参考文档:
 * /2175.html
 */
@Configuration
public class BeetlConf { //【特别注意1】此类不能命名为 BeetlConfig,因为它已被Beetl底层占用了,会冲突!
    @Bean(name = "beetlConfig") //【特别注意2】此Bean必须命名为 beetlConfig,方便在BeetlHelper类中,Spring以单例的方式自动找到BeetlGroupUtilConfiguration的实例
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
        String templatesPath = "/templates";
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();

        // 获取Spring Boot 的ClassLoader
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader == null) {
            loader = BeetlConf.class.getClassLoader();
        }

        // 额外的配置,可以覆盖默认配置,但一般不需要
        // ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        // 自定义配置文件路径:/resources/config/beetl.properties
        // Resource beetlResource = resolver.getResource("classpath:config//beetl.properties");
        // beetlGroupUtilConfiguration.setConfigFileResource(beetlResource);

        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();
        // 上面已经设置了全局路径了:String templatesPath = "/templates",下面的路径前缀就不用设置了
        // beetlSpringViewResolver.setPrefix("/templates");
        // 模板文件后缀,默认为.btl,这里改为.html
        beetlSpringViewResolver.setSuffix(".html");
        // 这个一般会在模板文件中指定编码为UTF-8
        // beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }
}

这里需要注意一下:

1、如果缺少beetlGroupUtilConfiguration.init(),会抛出异常。

2、如果直接使用beetlSpringViewResolver.setPrefix()定义模板路径,会有警告,这个可以看一下BeetlSpringViewResolver的源码:

public void setPrefix(String prefix) {
    this.logger.warn("Beetl不建议使用使用spring前缀,会导致include,layout找不到对应的模板,请使用beetl的配置RESOURCE.ROOT来配置模板根目录");
    super.setPrefix(prefix);
}

3、新建了一个beetl.properties,但是将它放在了resources/config目录中,所以代码中指定了配置文件的路径。

4、如果缺少beetlSpringViewResolver.setSuffix(".html"),会找不到模板。之前查资料,应该有缺省配置,可是实际使用的时候却不是这样,所以还是加上为妙。

beetl.properties的配置,目前为空,预留以后自定义配置使用,具体选项参考官方文档:

https://www.kancloud.cn/xiandafu/beetl3_guide/2138945

三、测试配置的效果

1、新建 控制器类:

@Controller
@RequestMapping("test")
public class TestController {

    @GetMapping("test")
    public String getTest(Model model) {
        model.addAttribute("userName", "test1");
        return "/test"; // 注意:新版不需要增加模板文件后缀名(.html)了
    }
}

2、新建 模板文件:

<!DOCTYPE html>
<html>
<head>
    <title>测试</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
<div class="dialog">
    <p>${userName}</p>
</div>
</body>
</html>

3、输出:

test1

四、新建一个BeetlHelper助手类,方便在业务类(非控制器类)中使用

package com.wanma.framework_web.helper;

import org.beetl.core.Template;
import org.beetl.core.exception.BeetlException;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;

import java.util.Map;

/**
 * BeetlHelper 助手类
 */
public class BeetlHelper {
    public static BeetlGroupUtilConfiguration groupTemplate = SpringContextHelper.getBean(BeetlGroupUtilConfiguration.class);

    /**
     * 解析模板
     */
    public static String parseTpl(String tplName, Map<String, Object> params) {
        String html = "";
        Template tpl;

        try {
            tpl = groupTemplate.getGroupTemplate().getTemplate(tplName + ".html");
            tpl.binding(params);
            html = tpl.render();
        } catch (BeetlException e) {
            e.printStackTrace();
        }

        return html;
    }
}

 

 

参考:

  1. [官方教程]Spring Boot配置集成Beetl
  2. Spring Boot + Beetl配置问题
  3. SpringBoot配置Beetl模板引擎
  4. SpringBoot集成Beetl模板引擎
  5. SpringBoot – 整合Beetl模板引擎