Spring Boot開(kāi)發(fā)Web應(yīng)用詳解
Spring Boot快速入門(mén)中我們完成了一個(gè)簡(jiǎn)單的RESTful Service,體驗(yàn)了快速開(kāi)發(fā)的特性。在留言中也有朋友提到如何把處理結(jié)果渲染到頁(yè)面上。那么本篇就在上篇基礎(chǔ)上介紹一下如何進(jìn)行Web應(yīng)用的開(kāi)發(fā)。
靜態(tài)資源訪問(wèn)
在我們開(kāi)發(fā)Web應(yīng)用的時(shí)候,需要引用大量的js、css、圖片等靜態(tài)資源。
默認(rèn)配置
Spring Boot默認(rèn)提供靜態(tài)資源目錄位置需置于classpath下,目錄名需符合如下規(guī)則:
- /static
- /public
- /resources
- /META-INF/resources
舉例:我們可以在src/main/resources/目錄下創(chuàng)建static,在該位置放置一個(gè)圖片文件。啟動(dòng)程序后,嘗試訪問(wèn)http://localhost:8080/D.jpg。如能顯示圖片,配置成功。
渲染W(wǎng)eb頁(yè)面
在之前的示例中,我們都是通過(guò)@RestController來(lái)處理請(qǐng)求,所以返回的內(nèi)容為json對(duì)象。那么如果需要渲染html頁(yè)面的時(shí)候,要如何實(shí)現(xiàn)呢?
模板引擎
在動(dòng)態(tài)HTML實(shí)現(xiàn)上Spring Boot依然可以完美勝任,并且提供了多種模板引擎的默認(rèn)配置支持,所以在推薦的模板引擎下,我們可以很快的上手開(kāi)發(fā)動(dòng)態(tài)網(wǎng)站。
Spring Boot提供了默認(rèn)配置的模板引擎主要有以下幾種:
- Thymeleaf
- FreeMarker
- Velocity
- Groovy
- Mustache
Spring Boot建議使用這些模板引擎,避免使用JSP,若一定要使用JSP將無(wú)法實(shí)現(xiàn)Spring Boot的多種特性,具體可見(jiàn)后文:支持JSP的配置
當(dāng)你使用上述模板引擎中的任何一個(gè),它們默認(rèn)的模板配置路徑為:src/main/resources/templates。當(dāng)然也可以修改這個(gè)路徑,具體如何修改,可在后續(xù)各模板引擎的配置屬性中查詢并修改。
Thymeleaf
Thymeleaf是一個(gè)XML/XHTML/HTML5模板引擎,可用于Web與非Web環(huán)境中的應(yīng)用開(kāi)發(fā)。它是一個(gè)開(kāi)源的Java庫(kù),基于Apache License 2.0許可,由Daniel Fernández創(chuàng)建,該作者還是Java加密庫(kù)Jasypt的作者。
Thymeleaf提供了一個(gè)用于整合Spring MVC的可選模塊,在應(yīng)用開(kāi)發(fā)中,你可以使用Thymeleaf來(lái)完全代替JSP或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目標(biāo)在于提供一種可被瀏覽器正確顯示的、格式良好的模板創(chuàng)建方式,因此也可以用作靜態(tài)建模。你可以使用它創(chuàng)建經(jīng)過(guò)驗(yàn)證的XML與HTML模板。相對(duì)于編寫(xiě)邏輯或代碼,開(kāi)發(fā)者只需將標(biāo)簽屬性添加到模板中即可。接下來(lái),這些標(biāo)簽屬性就會(huì)在DOM(文檔對(duì)象模型)上執(zhí)行預(yù)先制定好的邏輯。
示例模板:
<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標(biāo)簽中,瀏覽器在解析html時(shí),當(dāng)檢查到?jīng)]有的屬性時(shí)候會(huì)忽略,所以Thymeleaf的模板可以通過(guò)瀏覽器直接打開(kāi)展現(xiàn),這樣非常有利于前后端的分離。
在Spring Boot中使用Thymeleaf,只需要引入下面依賴,并在默認(rèn)的模板路徑src/main/resources/templates下編寫(xiě)模板文件即可完成。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
在完成配置之后,舉一個(gè)簡(jiǎn)單的例子,在快速入門(mén)工程的基礎(chǔ)上,舉一個(gè)簡(jiǎn)單的示例來(lái)通過(guò)Thymeleaf渲染一個(gè)頁(yè)面。
@Controller
public class HelloController {
@RequestMapping("/")
public String index(ModelMap map) {
// 加入一個(gè)屬性,用來(lái)在模板中讀取
map.addAttribute("host", "http://blog.didispace.com");
// return模板文件的名稱,對(duì)應(yīng)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>
如上頁(yè)面,直接打開(kāi)html頁(yè)面展現(xiàn)Hello World,但是啟動(dòng)程序后,訪問(wèn)http://localhost:8080/,則是展示Controller中host的值:http://www.dbjr.com.cn,做到了不破壞HTML自身內(nèi)容的數(shù)據(jù)邏輯分離。
更多Thymeleaf的頁(yè)面語(yǔ)法,還請(qǐng)?jiān)L問(wèn)Thymeleaf的官方文檔查詢使用。
Thymeleaf的默認(rèn)參數(shù)配置
如有需要修改默認(rèn)配置的時(shí)候,只需復(fù)制下面要修改的屬性到application.properties中,并修改成需要的值,如修改模板文件的擴(kuò)展名,修改默認(rèn)的模板路徑等。
# 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支持
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java8中的LocalDateTime和Date一些時(shí)間操作方法
這篇文章主要介紹了Java8中的LocalDateTime和Date一些時(shí)間操作方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Eclipse 使用Maven構(gòu)建SpringMVC項(xiàng)目
本文主要介紹在Eclipse下創(chuàng)建Maven項(xiàng)目構(gòu)建SpringMVC框架的過(guò)程,講解的比較詳細(xì),需要的朋友可以參考下。2016-06-06
SpringBoot學(xué)習(xí)篇之@Valid與@Validated的區(qū)別
@Valid是使用Hibernate?validation的時(shí)候使用,@Validated是只用Spring?Validator校驗(yàn)機(jī)制使用,下面這篇文章主要給大家介紹了關(guān)于SpringBoot學(xué)習(xí)篇之@Valid與@Validated區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-11-11
java實(shí)現(xiàn)簡(jiǎn)單斗地主(看牌排序)
這篇文章主要介紹了java實(shí)現(xiàn)簡(jiǎn)單斗地主,看牌進(jìn)行排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2010-11-11
java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例
這篇文章主要介紹了java Arrays快速打印數(shù)組的數(shù)據(jù)元素列表案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
java實(shí)現(xiàn)ftp上傳 如何創(chuàng)建文件夾
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)ftp上傳的相關(guān)資料,教大家如何創(chuàng)建文件夾?具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
log4j如何根據(jù)變量動(dòng)態(tài)生成文件名
這篇文章主要介紹了log4j如何根據(jù)變量動(dòng)態(tài)生成文件名方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

