spring boot 中3步集成beetl


spring boot 中3步集成beetl

因为项目要使用spring boot 和 beetl,官方文档写的也不是很详细,网上找了几篇文章写法各异,有的ide中好用,发布成jar就找不到模版了。折腾了快3个小时,终于搞定了。关键就是beetl的classloader要配置成springbot的。并且视图不要配置前后缀。集成步骤如下:

  1. 引入beetl依赖。注意引入的artifactId为 beetl-framework-starter。这个是spring boot的版本,不要引入成普通版本哦。
		<dependency>
			<groupId>com.ibeetl</groupId>
			<artifactId>beetl-framework-starter</artifactId>
			<version>1.1.17.RELEASE</version>
		</dependency>
  1. 在resources目录下新建beetl.properties。可以在这里修改beetl的默认配置。如果不修改默认配置,就建立一个空文件。

  2. 在springboot的启动类上添加如下代码。

Bean(initMethod = "init", name = "beetlConfig")
	public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
		BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
		//获取Spring Boot 的ClassLoader
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		if(loader==null){
			loader = SpringBootApplication.class.getClassLoader();
		}
		try {
			beetlGroupUtilConfiguration.setConfigProperties(PropertiesLoaderUtils.loadAllProperties("beetl.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader,"templates");
		beetlGroupUtilConfiguration.setResourceLoader(cploder);
		beetlGroupUtilConfiguration.init();
		//如果使用了优化编译器,涉及到字节码操作,需要添加ClassLoader
		GroupTemplate groupTemplate = beetlGroupUtilConfiguration.getGroupTemplate();
		groupTemplate.setClassLoader(loader);
		return beetlGroupUtilConfiguration;
	}

	@Bean(name = "beetlViewResolver")
	public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
		BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
		beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
		beetlSpringViewResolver.setOrder(0);
		beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
		return beetlSpringViewResolver;
	}

注意 getBeetlSpringViewResolver方法中,不要设置视图的前缀和后缀,否则可能会找不到模版。

		//不需要配置
		beetlSpringViewResolver.setPrefix("/xxx");
		beetlSpringViewResolver.setSuffix("html");
  1. 使用
    controller代码,跳转页面时,因为没有配置视图的前缀和后缀,所以return的字符串要写全如下。
/***
  * 欢迎页面
  */
@RequestMapping(value = "/admin/welcome",method = RequestMethod.GET)
public String welcome() {
   return "/admin/" + getTemplateName() + "/welcome.html";
}

模版位置


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM