SpringBoot中怎么搭建Beetl環境,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創新互聯-專業網站定制、快速模板網站建設、高性價比稱多網站開發、企業建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式稱多網站制作公司更省心,省錢,快速模板網站建設找我們,業務覆蓋稱多地區。費用合理售后完善,十年實體公司更值得信賴。
首先我們是需要一個springboot項目,基本的pom結構大都相似
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.0.4.RELEASE</version> <relativepath /> <!-- lookup parent from update --> </parent> <properties> <project.build.sourceencoding>UTF-8</project.build.sourceencoding> <project.reporting.outputencoding>UTF-8</project.reporting.outputencoding> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> <java.version>1.8</java.version> </properties> <build> <pluginmanagement> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </pluginmanagement> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
在這個項目中,我們主要需要引入兩個依賴包,一個web,一個官方提供的beetl-framework-starter,當前最新的版本為 1.2.12.RELEASE
<dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>com.ibeetl</groupid> <artifactid>beetl-framework-starter</artifactid> <version>1.2.12.RELEASE</version> </dependency> </dependencies>
通常我們直接使用默認的thymeleaf參數配置即可,下面給出幾個常用的配置
beetl: enabled: true suffix: btl beetl-beetlsql: dev: true # 即自動檢查模板變化
搭建一個web項目和我們之前的純后端項目有點不一樣,前端資源放在什么地方,依賴文件怎么處理都是有講究的,下面是一個常規的項目結構

如上圖,前端資源文件默認放在resources目錄下,下面有兩個目錄
templates:存放模板文件,可以理解為我們編寫的html,注意這個文件名不能有問題
static: 存放靜態資源文件,如js,css,image等
我們這里提供了三個接口,主要是為了演示三種不同的數據綁定方式(和前面兩篇博文基本一樣)
@Controller
public class IndexController {
@GetMapping(path = {"", "/", "/index"})
public ModelAndView index() {
Map<string, object> data = new HashMap<>(2);
data.put("name", "YiHui Beetl");
data.put("now", LocalDateTime.now().toString());
return new ModelAndView("index.btl", data);
}
private static String[] contents =
("綠蟻浮觴香泛泛,黃花共薦芳辰。\n清霜天宇凈無塵。\n登高宜有賦,拈筆戲成文。\n可奈園林搖落盡,悲秋意與誰論。\n眼中相識幾番新。\n龍山高會處,落帽定何人。").split("\n");
private static Random random = new Random();
@GetMapping(path = "show1")
public String showOne(Model model) {
model.addAttribute("title", "臨江仙");
model.addAttribute("content", contents[random.nextInt(6)]);
return "show1.btl";
}
@GetMapping(path = "show2")
public String showTow(Map<string, object> data) {
data.put("name", "Show2---->");
data.put("now", LocalDateTime.now().toString());
return "show2.btl";
}
}上面的三種case中
第一個是最好理解的,在創建ModelAndView時,傳入viewName和數據
第二個是通過接口參數Model,設置傳遞給view的數據
第三種則直接使用Map來傳遞數據
注意
如果和前面兩篇博文進行對比,會發現一個顯著的區別,之前的Freemaker, Thymeleaf指定視圖名的時候,都不需要后綴,但是這里,必須帶上后綴,否則會500錯誤
三個接口,對應的三個btl文件,如下
index.btl
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="SpringBoot Beetl">
<meta name="author" content="YiHui">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YiHui's SpringBoot Beetl Demo</title>
<link rel="stylesheet" href="index.css">
<div>
<div class="title">hello world!</div>
<br>
<div class="content">歡迎訪問 ${name}</div>
<br>
<div class="sign">當前時間 ${now}</div>
<br>
<a href="show1">傳參2測試</a>
<a href="show2">傳參3測試</a>
</div>show1.btl
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="SpringBoot Beetl">
<meta name="author" content="YiHui">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YiHui's SpringBoot Beetl Demo</title>
<link rel="stylesheet" href="index.css">
<div>
<div class="title">${title}</div>
<div class="content">${content}</div>
</div>show2.btl
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="SpringBoot Beetl">
<meta name="author" content="YiHui">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YiHui's SpringBoot Beetl Demo</title>
<link rel="stylesheet" href="index.css">
<div>
<div class="title">${name}</div>
<div class="content">${now}</div>
</div>在上面的模板文件中,需要注意引用css樣式文件,路徑前面并沒有static,我們對應的css文件
index.css
.title {
color: #c00;
font-weight: normal;
font-size: 2em;
}
.content {
color: darkblue;
font-size: 1.2em;
}
.sign {
color: lightgray;
font-size: 0.8em;
font-style: italic;
}啟動項目后,可以看到三個頁面的切換,模板中的數據根據后端的返回替換,特別是主頁的時間,每次刷新都會隨之改變

關于SpringBoot中怎么搭建Beetl環境問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創新互聯行業資訊頻道了解更多相關知識。
當前文章:SpringBoot中怎么搭建Beetl環境
標題網址:http://www.yijiale78.com/article6/ghdcig.html
成都網站建設公司_創新互聯,為您提供自適應網站、品牌網站建設、定制開發、ChatGPT、云服務器、網站收錄
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯