spring boot報錯:
Circular view path [readingList]: would dispatch back to the current handler URL [/readingList] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.
圖示:
原因如下:
當沒有聲明ViewResolver時,spring會給你注冊一個默認的ViewResolver,就是JstlView的實例, 該對象繼承自InternalResoureView。
JstlView用來封裝JSP或者同一Web應用中的其他資源,它將model對象作為request請求的屬性值暴露出來, 並將該請求通過javax.servlet.RequestDispatcher轉發到指定的URL.
Spring認為, 這個view的URL是可以用來指定同一web應用中特定資源的,是可以被RequestDispatcher轉發的。
也就是說,在頁面渲染(render)之前,Spring會試圖使用RequestDispatcher來繼續轉發該請求。
if (path.startsWith("/") ? uri.equals(path) : uri.equals(StringUtils.applyRelativePath(uri, path))) { throw new ServletException("Circular view path [" + path + "]: would dispatch back " + "to the current handler URL [" + uri + "] again. Check your ViewResolver setup! " + "(Hint: This may be the result of an unspecified view, due to default view name generation.)"); }
從這段代碼可以看出,如果你的view name和你的path是相同的字符串,根據Spring的轉發規則,就等於讓自己轉發給自己,會陷入死循環。所以Spring會檢查到這種情況,於是拋出Circular view path異常。
boot中,使用thymeleaf,會加入starter依賴,引入這個依賴的時候了,boot會自動進行配置轉發規則,所以只要記得在pom文件中聲明依賴即可
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
spring中也會遇到這種情況的,原理同上,如果不聲明viewResolver,就會出現這個問題的。我在controller中,不加@ResponseBody注解,直接返回"index"字符串,來映射
index.jsp 就會進入上面的代碼,判斷uri 和 jsp 的name相同,都是index