gboot template engine configuration beetl

1, the configuration dependent 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 new BeetlSpringViewResolver = ();
beetlSpringViewResolver.setPrefix ( "beetls /"); // Path file is provided beetl: Resources / Templates
beetlSpringViewResolver.setSuffix ( " .html "); // set beetl suffix to BTL
beetlSpringViewResolver.setContentType (" text / HTML; charset = UTF-. 8 ");
beetlSpringViewResolver.setOrder (0);
beetlSpringViewResolver.setConfig (beetlGroupUtilConfiguration ());
return beetlSpringViewResolver;
}


}
3. create a file directory beetl

 

4, file contents

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 / the default.html") {%>
the Hello World!
<%}%>
. 5, the configuration file beetl.properties

# Start symbol value
DELIMITER_PLACEHOLDER_START = $ {
# end symbol value
DELIMITER_PLACEHOLDER_END =}
#beetl codes starting symbol
DELIMITER_STATEMENT_START = <%
#beetl end symbol codes
DELIMITER_STATEMENT_END% =>
 
--------------- ------

Guess you like

Origin www.cnblogs.com/liyanyan665/p/11257596.html
Recommended