A-A+

SpringBoot – 整合Beetl模板引擎

2019年09月22日 技术, 默认 暂无评论 阅读 6,845 次

之前的流程引擎测试代码都是基于Spring的,乱七八糟的配置文件一大堆,看着极其不爽。近期打算更换成Spring Boot,所以就预先试验一下了,先从整合beetl模板引擎开始。

新建了一个Maven项目,打算使用Jetty作为Web容器,Pom文件最开始的时候非常简单,主要配置如下:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.8.RELEASE</version>
</parent>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
      <exclusion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
  </dependency>
  <dependency>
    <groupId>com.ibeetl</groupId>
    <artifactId>beetl-framework-starter</artifactId>
    <version>1.2.14.RELEASE</version>
  </dependency>
</dependencies>

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

@Configuration
public class SpringBeetlConfig {

    @Bean(name = "beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource beetlResource = resolver.getResource("classpath:properties//beetl.properties");
        beetlGroupUtilConfiguration.setConfigFileResource(beetlResource);
        beetlGroupUtilConfiguration.setResourceLoader(classpathResourceLoader);
        beetlGroupUtilConfiguration.init();
        return beetlGroupUtilConfiguration;
    }

    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setSuffix(".btl");
        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\properties中,所以代码中指定了配置文件的路径。

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

beetl.properties的配置目前只有一行,如下:

RESOURCE.root= /templates/

最后,测试的Controller:

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

    @GetMapping("test")
    public String getTest(Model model) {
        model.addAttribute("userName""test1");
        return "test";
    }
}

测试的页面:

<!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>

运行的结果:

觉的不错?可以关注我的公众号↑↑↑

给我留言

Copyright © 字痕随行 保留所有权利.   Theme  Ality

用户登录

分享到: