1、配置 maven 依赖
<!-- beetl模板引擎 -->
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.8.5</version>
</dependency>
2、配置 BeetlConfig
package com.sample.common.config;
import org.beetl.core.resource.WebAppResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import java.io.IOException;
@Configuration
public class BeetlConfig {
@Bean(initMethod = "init")
public BeetlGroupUtilConfiguration beetlGroupUtilConfiguration() {
BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
try {
// WebAppResourceLoader 配置root路径是关键
WebAppResourceLoader webAppResourceLoader =
new WebAppResourceLoader(patternResolver.getResource("classpath:/").getFile().getPath());//设置beetl根路径
beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader);
} catch (IOException e) {
e.printStackTrace();
}
beetlGroupUtilConfiguration.setConfigFileResource(patternResolver.getResource("classpath:beetl.properties"));
//读取配置文件信息
return beetlGroupUtilConfiguration;
}
@Bean(name = "beetlViewResolver")
public BeetlSpringViewResolver getBeetlSpringViewResolver() {
BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
beetlSpringViewResolver.setPrefix("beetls/");//设置beetl文件的路径为:resources/templates
beetlSpringViewResolver.setSuffix(".html");//设置beetl的后缀设置为btl
beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
beetlSpringViewResolver.setOrder(0);
beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration());
return beetlSpringViewResolver;
}
}
3、创建 beetl 文件目录
4、文件内容
head.html
<meta charset="UTF-8">
<title>header</title>
foot.html
<div>footer</div>
default.html
<!DOCTYPE html>
<html>
<head>
<% include(‘../include/header.html‘){} %>
</head>
<body>
<div class="wrapper">${layoutContent}</div>
<% include(‘../include/footer.html‘){} %>
</body>
</html>
beetl.html
<% layout("layouts/default.html"){ %>
Hello world!
<% } %>
5、配置文件 beetl.properties
#取值开始符号
DELIMITER_PLACEHOLDER_START=${
#取值结束符号
DELIMITER_PLACEHOLDER_END=}
#beetl代码开始符号
DELIMITER_STATEMENT_START=<%
#beetl代码结束符号
DELIMITER_STATEMENT_END=%>
---------------------
原文地址:https://www.cnblogs.com/liyanyan665/p/11257596.html