Spring Boot快速入門中咱們完成了一個簡單的RESTful Service,體驗了快速開發的特性。在留言中也有朋友提到如何把處理結果渲染到頁面上。那麼本篇就在上篇基礎上介紹一下如何進行Web應用的開發。 css
在咱們開發Web應用的時候,須要引用大量的js、css、圖片等靜態資源。html
Spring Boot默認提供靜態資源目錄位置需置於classpath下,目錄名需符合以下規則:git
舉例:咱們能夠在 src/main/resources/ 目錄下建立 static ,在該位置放置一個圖片文件。啓動程序後,嘗試訪問 http://localhost:8080/D.jpg 。如能顯示圖片,配置成功。 github
在以前的示例中,咱們都是經過@RestController來處理請求,因此返回的內容爲json對象。那麼若是須要渲染html頁面的時候,要如何實現呢?web
在動態HTML實現上Spring Boot依然能夠完美勝任,而且提供了多種模板引擎的默認配置支持,因此在推薦的模板引擎下,咱們能夠很快的上手開發動態網站。spring
Spring Boot提供了默認配置的模板引擎主要有如下幾種:json
Spring Boot建議使用這些模板引擎,避免使用JSP,若必定要使用JSP將沒法實現Spring Boot的多種特性,具體可見後文:支持JSP的配置後端
當你使用上述模板引擎中的任何一個,它們默認的模板配置路徑爲: src/main/resources/templates 。固然也能夠修改這個路徑,具體如何修改,可在後續各模板引擎的配置屬性中查詢並修改。 瀏覽器
Thymeleaf是一個XML/XHTML/HTML5模板引擎,可用於Web與非Web環境中的應用開發。它是一個開源的Java庫,基於Apache License 2.0許可,由Daniel Fernández建立,該做者仍是Java加密庫Jasypt的做者。app
Thymeleaf提供了一個用於整合Spring MVC的可選模塊,在應用開發中,你可使用Thymeleaf來徹底代替JSP或其餘模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目標在於提供一種可被瀏覽器正確顯示的、格式良好的模板建立方式,所以也能夠用做靜態建模。你可使用它建立通過驗證的XML與HTML模板。相對於編寫邏輯或代碼,開發者只需將標籤屬性添加到模板中便可。接下來,這些標籤屬性就會在DOM(文檔對象模型)上執行預先制定好的邏輯。
示例模板:
<table> <thead> <tr> <th th:text="#{msgs.headers.name}">Name</td> <th th:text="#{msgs.headers.price}">Price</td> </tr> </thead> <tbody> <tr th:each="prod : ${allProducts}"> <td th:text="${prod.name}">Oranges</td> <td th:text="${#numbers.formatDecimal(prod.price,1,2)}">0.99</td> </tr> </tbody> </table>
能夠看到Thymeleaf主要以屬性的方式加入到html標籤中,瀏覽器在解析html時,當檢查到沒有的屬性時候會忽略,因此Thymeleaf的模板能夠經過瀏覽器直接打開展示,這樣很是有利於先後端的分離。
在Spring Boot中使用Thymeleaf,只須要引入下面依賴,並在默認的模板路徑 src/main/resources/templates 下編寫模板文件便可完成。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
在完成配置以後,舉一個簡單的例子,在快速入門工程的基礎上,舉一個簡單的示例來經過Thymeleaf渲染一個頁面。
@Controller public class HelloController { @RequestMapping("/") public String index(ModelMap map) { // 加入一個屬性,用來在模板中讀取 map.addAttribute("host", "http://blog.didispace.com"); // return模板文件的名稱,對應src/main/resources/templates/index.html return "index"; } }
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title></title> </head> <body> <h1 th:text="${host}">Hello World</h1> </body> </html>
如上頁面,直接打開html頁面展示Hello World,可是啓動程序後,訪問 http://localhost:8080/ ,則是展現Controller中host的值: http://blog.didispace.com ,作到了不破壞HTML自身內容的數據邏輯分離。
更多Thymeleaf的頁面語法,還請訪問Thymeleaf的官方文檔查詢使用。
若有須要修改默認配置的時候,只需複製下面要修改的屬性到 application.properties 中,並修改爲須要的值,如修改模板文件的擴展名,修改默認的模板路徑等。
# Enable template caching. spring.thymeleaf.cache=true # Check that the templates location exists. spring.thymeleaf.check-template-location=true # Content-Type value. spring.thymeleaf.content-type=text/html # Enable MVC Thymeleaf view resolution. spring.thymeleaf.enabled=true # Template encoding. spring.thymeleaf.encoding=UTF-8 # Comma-separated list of view names that should be excluded from resolution. spring.thymeleaf.excluded-view-names= # Template mode to be applied to templates. See also StandardTemplateModeHandlers. spring.thymeleaf.mode=HTML5 # Prefix that gets prepended to view names when building a URL. spring.thymeleaf.prefix=classpath:/templates/ # Suffix that gets appended to view names when building a URL. spring.thymeleaf.suffix=.html spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.
Spring Boot並不建議使用,但若是必定要使用,能夠參考此工程做爲腳手架: JSP支持