最近想用一下Beetl模板引擎,据说渲染速度那是相当的快啊,这里写一下整合的过程,以便下次能方便直接使用。这里就不使用官网提供的starter了,配置了一遍,感觉不太对。
一、导入pom文件:
<dependency>
<groupId>com.ibeetlgroupId>
<artifactId>beetlartifactId>
<version>2.8.5version>
dependency>
二、编写相关的配置类
1.BeetlProperties.java 这个类用来映射yml文件的自定义配置属性,用到commons-lang3,lombok相关依赖。
@Data
@ConfigurationProperties(prefix = BeetlProperties.BEETLCONF_PREFIX)
public class BeetlProperties {
public static final String BEETLCONF_PREFIX = "beetl";
private String delimiterStatementStart;
private String delimiterStatementEnd;
private String resourceTagroot;
private String resourceTagsuffix;
private String resourceAutoCheck;
@Value("${spring.mvc.view.prefix}")
private String prefix;
public Properties getProperties() {
Properties properties = new Properties();
if (StringUtils.isNotBlank(delimiterStatementStart)) {
if (delimiterStatementStart.startsWith("\\")) {
delimiterStatementStart = delimiterStatementStart.substring(1);
}
properties.setProperty("DELIMITER_STATEMENT_START", delimiterStatementStart);
}
if (StringUtils.isNotBlank(delimiterStatementEnd)) {
properties.setProperty("DELIMITER_STATEMENT_END", delimiterStatementEnd);
} else {
properties.setProperty("DELIMITER_STATEMENT_END", "null");
}
if (StringUtils.isNotBlank(resourceTagroot)) {
properties.setProperty("RESOURCE.tagRoot", resourceTagroot);
}
if (StringUtils.isNotBlank(resourceTagsuffix)) {
properties.setProperty("RESOURCE.tagSuffix", resourceTagsuffix);
}
if (StringUtils.isNotBlank(resourceAutoCheck)) {
properties.setProperty("RESOURCE.autoCheck", resourceAutoCheck);
}
return properties;
}
}
2.BeetlConfiguration.java
/**
* @version 1.0
* @description: beetl拓展配置, 绑定一些工具类, 方便在模板中直接调用.直接配置groupTemplate
* @author: taozhiyaoyao
* @date 2018/7/5
*/
public class BeetlConfiguration extends BeetlGroupUtilConfiguration {
/*
* 注册自定义方法
*/
public void initOther() {
groupTemplate.registerFunctionPackage("random", new RandomUtils());
}
}
@Configuration
@EnableConfigurationProperties(BeetlProperties.class)
public class MyBeetlConfig {
@Autowired
private BeetlProperties beetlProperties;
/**
* beetl的配置
*/
@Bean(initMethod = "init")
public BeetlConfiguration beetlConfiguration() {
BeetlConfiguration beetlConfiguration = new BeetlConfiguration();
beetlConfiguration.setResourceLoader(new ClasspathResourceLoader(MyBeetlConfig.class.getClassLoader(), beetlProperties.getPrefix()));
beetlConfiguration.setConfigProperties(beetlProperties.getProperties());
return beetlConfiguration;
}
/**
* beetl的视图解析器
*/
@Bean
public BeetlSpringViewResolver beetlViewResolver() {
BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
beetlSpringViewResolver.setConfig(beetlConfiguration());
beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
beetlSpringViewResolver.setOrder(0);
return beetlSpringViewResolver;
}
}
三、上边几个文件就把Beetl配置好了。创建根路径
(1)在main文件夹下新建一个webapp目录,存放静态资源文件和模板文件。
(2)把webapp目录映射为根目录
(3)文件夹结构类似这样,static目录放静态文件。WEB-INF/view目录放模板文件
四、编写yml文件
################### beetl配置 ###################
beetl:
delimiter-statement-start: \@ #开始结束标签(yaml不允许@开头)
delimiter-statement-end: null
resource-tagroot: common/tags #自定义标签文件Root目录和后缀
resource-tagsuffix: tag
resource-auto-check: true #是否检测文件变化,开发用true合适,但线上要改为false
################### spring配置 ###################
spring:
mvc:
static-path-pattern: /static/**
view:
prefix: /WEB-INF/view/
五、测试:
index.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
@var a = 1;
@var b = 1;
@var c = a + b;
<h1>${c}h1>
body>
html>
@Controller
public class HelloController {
@RequestMapping("/")
public String hello() {
return "index.html";
}
}
完事!!!!!!