Spring Boot開發Web應用

itoj1613 8年前發布 | 146K 次閱讀 JEE框架 Spring Boot

來自: http://blog.didispace.com/springbootweb/

Spring Boot快速入門中我們完成了一個簡單的RESTful Service,體驗了快速開發的特性。在留言中也有朋友提到如何把處理結果渲染到頁面上。那么本篇就在上篇基礎上介紹一下如何進行Web應用的開發。

靜態資源訪問

在我們開發Web應用的時候,需要引用大量的js、css、圖片等靜態資源。

默認配置

Spring Boot默認提供靜態資源目錄位置需置于classpath下,目錄名需符合如下規則:

  • /static
  • /public
  • /resources
  • /META-INF/resources

舉例:我們可以在 src/main/resources/ 目錄下創建 static ,在該位置放置一個圖片文件。啟動程序后,嘗試訪問 http://localhost:8080/D.jpg 。如能顯示圖片,配置成功。

渲染Web頁面

在之前的示例中,我們都是通過@RestController來處理請求,所以返回的內容為json對象。那么如果需要渲染html頁面的時候,要如何實現呢?

模板引擎

在動態HTML實現上Spring Boot依然可以完美勝任,并且提供了多種模板引擎的默認配置支持,所以在推薦的模板引擎下,我們可以很快的上手開發動態網站。

Spring Boot提供了默認配置的模板引擎主要有以下幾種:

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

Spring Boot建議使用這些模板引擎,避免使用JSP,若一定要使用JSP將無法實現Spring Boot的多種特性,具體可見后文:支持JSP的配置

當你使用上述模板引擎中的任何一個,它們默認的模板配置路徑為: src/main/resources/templates 。當然也可以修改這個路徑,具體如何修改,可在后續各模板引擎的配置屬性中查詢并修改。

Thymeleaf

Thymeleaf是一個XML/XHTML/HTML5模板引擎,可用于Web與非Web環境中的應用開發。它是一個開源的Java庫,基于Apache License 2.0許可,由Daniel Fernández創建,該作者還是Java加密庫Jasypt的作者。

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的官方文檔查詢使用。

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.

支持JSP的配置

Spring Boot并不建議使用,但如果一定要使用,可以參考此工程作為腳手架: JSP支持

Spring Boot教程完整案例

 本文由用戶 itoj1613 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!