1、简单介绍
- 超高的性能:Beetl 远超过主流java模板引擎性能(引擎性能5-6倍与freemaker,2倍于JSP。参考附录),而且消耗较低的CPU。
2、pom文件中添加依赖
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.7.15</version>
</dependency>
3、编写核心配置类BeetlConfig
import java.io.IOException;
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;
@Configuration
public class BeetlConfig {
@Bean(initMethod="init")
public BeetlGroupUtilConfiguration beetlConfiguration(){
BeetlGroupUtilConfiguration configuration=new BeetlGroupUtilConfiguration();
ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
String path=null;
try {
path = patternResolver.getResource("classpath:/").getFile().getPath();
System.out.println("==================path:"+path);
WebAppResourceLoader resourceLoader=new WebAppResourceLoader(path);
configuration.setResourceLoader(resourceLoader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return configuration;
}
@Bean
public BeetlSpringViewResolver getBeetlSpringViewResolver(){
BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
beetlSpringViewResolver.setPrefix("WEB-INF/views/");
beetlSpringViewResolver.setSuffix(".html");
beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
beetlSpringViewResolver.setOrder(0);
beetlSpringViewResolver.setConfig(beetlConfiguration());
return beetlSpringViewResolver;
}
}
4、控制层方法
@RequestMapping(value={"/beetl/add"},method=RequestMethod.GET)
public ModelAndView testBeetl(){
ModelAndView modelAndView = new ModelAndView();
System.out.println("add request");
modelAndView.addObject("email", "apk2sf@163.com");
modelAndView.setViewName("add");
return modelAndView;
}
5、在src\main\resources路径下创建文件夹WEB-INF,再在WEB-INF下创建文件夹views,最后在views文件夹下创建add.html文件。add.html文件代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>add</title>
</head>
<body>
add Html web info
<hr>
获取项目的context-path-->${ctxPath}
<hr>
获取后台返回的数据--->${email}
</body>
</html>
6、按照控制层方法请求,即可看到页面。
我的学习资料来自: