1. 程式人生 > >IDEA Springboot 整合 beetl 模板引擎

IDEA Springboot 整合 beetl 模板引擎

省略建立springboot專案的步驟

配置pox.xml新增freemarker依賴

        <!--beetl 模板引擎-->
		<dependency>
			<groupId>com.ibeetl</groupId>
			<artifactId>beetl-framework-starter</artifactId>
			<version>1.1.70.RELEASE</version>
		</dependency>

法一:

starter 自動處理以btl結尾的檢視,模板根目錄是Spring Boot預設的templates目錄。

 這樣看來可以直接使用

建立beetl頁面userInfo.btl 當然也可用直接用html,以btl為字尾是為了區分其他模板語言,也為了讓啟動以來直接處理。

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
${name}
${id}
</body>
</html>

編寫BeetlController

@Controller
@RequestMapping("beetl")
public class BeetlController {

    @RequestMapping("/showuser.html")
    public ModelAndView showUsername(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("id", id);
        mv.addObject("name", "dongguo1");
        mv.setViewName("/userInfo.btl");
        return mv;
    }
}

application.yml中的配置

server:
    port: 8080
    servlet:
      context-path: /springboot-web

訪問頁面 

http://localhost:8080/springboot-web/beetl/showuser.html?id=456

 法二(轉 https://blog.csdn.net/xslde_com/article/details/81132673):

使用Starter來配置已經夠用,如果你想自己配置模板引擎, 通過java config來配置 beetl需要的BeetlGroupUtilConfiguration,和 BeetlSpringViewResolver,

建立beetl配置檔案:BeetlConf.class

package com.xslde.configurer;
 
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * @Author xslde
 * @Description
 * @Date 2018/7/20 15:17
 */
@Configuration //載入配置檔案
public class BeetlConf {
 
    @Value("${beetl.templatesPath}") String templatesPath;//模板根目錄 ,比如 "templates"
    @Bean(name = "beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        //獲取Spring Boot 的ClassLoader
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if(loader==null){
            loader = BeetlConf.class.getClassLoader();
        }
        //beetlGroupUtilConfiguration.setConfigProperties(extProperties);//額外的配置,可以覆蓋預設配置,一般不需要
        ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader,
                templatesPath);
        beetlGroupUtilConfiguration.setResourceLoader(cploder);
        beetlGroupUtilConfiguration.init();
        //如果使用了優化編譯器,涉及到位元組碼操作,需要新增ClassLoader
        beetlGroupUtilConfiguration.getGroupTemplate().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;
    }
 
}


在application.yml中配置模板引擎路徑:

server:
    port: 8080
    servlet:
      context-path: /springboot-web
#模板路徑
beetl:
  templatesPath: templates

建立一個 BeetlAction.class

package com.xslde.action;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
 
/**
 * @Author xslde
 * @Description
 * @Date 2018/7/20 15:22
 */
@Controller
public class BeetlAction {
 
    @RequestMapping("/index")
    public String beetl(Model model) {
        model.addAttribute("beetl", "測試一下通過模板引擎傳遞引數!");
        return "index.html";
    }
 
}


在templates資料夾下建立一個html檔案

index.html



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>測試頁面</title>
</head>
<body>
<span>通過beetl取值</span>
<h1>${beetl!}</h1>
</body>
</html>

訪問頁面 

 http://localhost:8080/springboot-web/beetl/index