javax.servlet.ServletException: Circular view path []: would dispatch back to the current

javax.servlet.ServletException: Circular view path []: would dispatch back to the current

springboot頁面訪問出錯

先解釋一下springboot項目resources文件夾下static目錄和templates目錄,
static目錄默認是用來放靜態文件的,如css,js,html等;
templates目錄用來放動態文件,兩個文件夾中的文件訪問是有區別的。

訪問static目錄下的文件
//請求路徑與返回視圖名相同時,就會發生死循環

    @RequestMapping("/Hello") 
    public String login() {
        System.out.println("Hello");
        return "hello.html";
    }

訪問鏈接:http://localhost:8080/hello.html,返回的是hello.html
http://localhost:8080/Hello也返回hello.html,如果pom.xml中加了thymeleaf依賴會出現404錯誤.

訪問templates目錄下的文件
要先在pom.xml中添加thymeleaf依賴,否則訪問不到,出現404

     <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
    @RequestMapping("/Hi")
    public String sayHello() {
        return "succeeded";
    }

瀏覽器輸入http://localhost:8080/Hi,會返回templates下的succeed.html,頁面,如果templates下沒有succed.html這個文件或succeed.html放在static目錄下,會出現404錯誤

終上所述,以hello爲例,若訪問templates下的文件hello.html
瀏覽器輸入:http:localhost:8080/Hi
@RequestMapping("/Hi")
public String sayHello() {
return “hello”;
}
同時加上thymeleaf依賴
注意, @RequestMapping("/Hi")中的路徑“/Hi”,頁面返回return 中不能是"Hi",否則會出現死循環 Circular view path
若訪問static下的hello.html
瀏覽器輸入:http:localhost:8080/hello.html,不需要方法體
也可以這樣訪問,瀏覽器:http:localhost:8080/Hello

	@RequestMapping("/Hello")
    public String Hello() {
        System.out.println("Hello");
        return "hello";
    }

同時去掉thymeleaf依賴

參考:
https://blog.csdn.net/weixin_43655481/article/details/94667649
https://blog.csdn.net/RuigeO/article/details/83926287

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章