Thymeleaf模板引擎和Webjars靜態(tài)資源入門應(yīng)用
在pom.xml添加Thymeleaf依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
代碼目錄結(jié)構(gòu)

resourses文件夾下的static文件夾用來存放靜態(tài)資源,如圖片,css樣式等;templates文件夾創(chuàng)建Thymeleaf頁面;java文件夾下用來創(chuàng)建model類
Controller示例代碼
package com.niit.quickstart.controller; import com.niit.quickstart.entity.Student; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import javax.annotation.Resource; @Controller public class IndexController {
//注入了一個(gè)Student類的對象 被Spring容器托管——bean
@Resource
private Student student; // @RequestMapping(value = "index",method = RequestMethod.GET)
@GetMapping("/index")
public String index(ModelMap map){
student.setAge(21);
student.setName("Mr.Li");
student.setMale("男");
//將模型加入視圖
map.addAttribute("model",student);
return "index";
}
}
示例頁面聲明

通過webjars引用靜態(tài)資源
為什么要用webjars
Java Web前端通常需要使用JS或CSS技術(shù),例如jQuery, Backbone.js,Twitter Bootstrap等等。通過人工方式將Web資源拷貝到Java Web項(xiàng)目的Webapp相應(yīng)目錄下,可能會產(chǎn)生版本誤差,拷貝版本錯(cuò)誤,漏拷等現(xiàn)象,前端頁面就無法正確展示。
WebJars是將Web前端Javascript和CSS等資源打包成Java的Jar包,這樣在Java Web開發(fā)中我們可以借助Maven這些依賴庫的管理,保證這些Web資源版本唯一性
上代碼

效果圖
