Spring Boot項目Circular view path問題解決


使用Spring Boot創建Spring MVC項目,訪問url請求出現問題:Circular view path

1、問題描述

控制台打印:

javax.servlet.ServletException: Circular view path [greeting]: would dispatch back to the current handler URL [/greeting] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

瀏覽器訪問:

2、項目代碼

(1)Spring MVC的controller

 1 package com.hello.web;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.ui.Model;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestParam;
 7 
 8 @Controller
 9 public class GreetingController {
10     @RequestMapping("/greeting")
11     public String greeting(@RequestParam(value="name",required=false,defaultValue="World")String name, Model model) {
12         model.addAttribute("name", name);
13         return "greeting";
14     }
15 }

 

(2)頁面代碼

使用 thymeleaf模板框架,負責服務端渲染html頁面

 1 <!DOCTYPE html>
 2 <html xmlns:th="http://www.thymeleaf.org">
 3 <head>
 4 <title>Spring Boot thymeleaf 應用</title>
 5 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
 6 </head>
 7 <body>
 8     <p th:text="'hello, '+${name}+'!'" />
 9 </body>
10 </html>

3、解決方法

在 項目的 pom.xml中添加 thymeleaf 的 spring boot starter

1 <dependency>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-thymeleaf</artifactId>
4 </dependency>

4、問題解決

再次通過瀏覽器地址欄,訪問: http://localhost:8080/greeting?name=zhangsan

Donate捐贈

如果我的文章幫助了你,可以贊賞我 1 元,讓我繼續寫出更好的內容)

   

  (微信)                                        (支付寶)

微信/支付寶 掃一掃

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM