我不能停滯不前
前言
- 心心念念的Spring Boot初體驗終于要寫了;
- Spring Boot項目的具體創(chuàng)建過程不再贅述鳖敷,因為網(wǎng)上有一堆了。。本文主要介紹我在創(chuàng)建過程中的踩坑皆看,還有填坑
- 本文中用的了一個 ResponseEntity類型,不懂的戳下面鏈接:
【Spring】源碼淺析 - ResponseEntity.ok - 各位看官背零,在讀文過程中腰吟,若有建議,請不吝賜教
創(chuàng)建Spring Boot項目的三種方法
不論idea,還是eclipse毛雇,創(chuàng)建Spring Boot項目都是基于以上三種方法之一
使用idea嫉称,基于Spring Initializr創(chuàng)建Spring Boot項目
關(guān)于創(chuàng)建Spring Boot項目,網(wǎng)上已有一堆相關(guān)資料灵疮,不再贅述织阅;
個人覺得這一篇寫的不錯了,各位看官可以戳這里:Spring Boot【快速入門】震捣;
踩坑
坑一:Failed to configure a DataSource
原因:
我在創(chuàng)建項目的時候荔棉,導(dǎo)入了數(shù)據(jù)庫的相關(guān)jar包,在創(chuàng)建后蒿赢,沒有配置數(shù)據(jù)庫
- 數(shù)據(jù)庫:MySQL
-
ORM框架:MyBatis
選擇SQL相關(guān)jar包
解決方案:
方案一:刪除pom.xml中數(shù)據(jù)庫jar包的配置
方案二:在application.properties中配置數(shù)據(jù)庫
#MySQL數(shù)據(jù)庫配置
spring.datasource.url=jdbc:mysql://localhost/blog
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
由于只是初體驗润樱,用不到數(shù)據(jù)庫,所以我采用方案一羡棵;
坑二:addViewControllers無法返回頁面
由于我不想在Controller層寫返回頁面的方法壹若,所以決定自定義一個類實現(xiàn)WebMvcConfigurer
接口,重寫addViewControllers
方法去返回頁面皂冰;沒想到踩坑了┑( ̄Д  ̄)┍
自定義類實現(xiàn)WebMvcConfigurer
接口
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author sincH
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/h/test").setViewName("test");
}
}
結(jié)果無法返回頁面
原因:
通過控制臺的日志輸出店展,分析出
InternalResourceView
渲染頁面失敗了那么深層分析一下,為啥子會失敗呢秃流?通過網(wǎng)上沖浪得知:
templates文件夾下的的頁面要配置了模板引擎才能訪問到赂蕴;
由此推測一下,因為InternalResourceView
無法訪問到頁面剔应,導(dǎo)致渲染失敗睡腿,是嗎?峻贮?
解決方案:
既然知道了原因是沒有配置模板引擎席怪,那么配置模板引擎即可,我選擇Thymeleaf
- 修改pom.xml
<!--Thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-->Thymeleaf的非嚴格HTML格式包-->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
- 修改application.properties
#Thymeleaf 配置
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
#緩存設(shè)置為false, 這樣修改之后馬上生效纤控,便于調(diào)試挂捻;生產(chǎn)環(huán)境下可以設(shè)置為true
spring.thymeleaf.cache=false
成功解決!船万!
對比配置Thymeleaf前后的控制臺日志輸出
發(fā)現(xiàn):
配置Thymeleaf后刻撒,用ThymeleafView
去渲染頁面,然后就成了耿导,真是声怔。。怎么說舱呻,甚是妙啊
填坑
- 關(guān)于頁面static文件夾與templates文件夾的區(qū)別
- static存放靜態(tài)頁面醋火,templates存放動態(tài)頁面
-
static下的頁面悠汽,不論是前端,還是后端芥驳,都可以用xxx.html 可以訪問到(前提是必須在Web應(yīng)用根目錄下)
例如:我的Web應(yīng)用根目錄是/blog
柿冲,那么訪問static下的test頁面,就該這么寫/blog/test.html
兆旬;
templates下的頁面需要配置了模板引擎才能訪問到
- 當(dāng)類上注解@RestController時假抄,該類方法返回值類型不同,則返回結(jié)果不同
當(dāng)返回值類型是- String丽猬;返回(json)字符串宿饱,而不是渲染視圖;
- ResponseEntity;返回(json)字符串
- ModelAndView脚祟;返回頁面
后記
真的 寫篇技術(shù)文章真心不容易刑棵;不過這樣也挺好;學(xué)習(xí)知識點后愚铡,寫技術(shù)文章可以加深記憶。嗯胡陪。沥寥。挺好的。
參考文章
Spring Boot【快速入門】
ViewControllerRegistry的用法
spring boot(4)-html和templates
Spring boot找不到template下面的html文件解決方法
SpringBoot訪問不了resources/templates下的頁面
下篇預(yù)告
【Spring Boot】搭建個人博客 - 需求分析