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