使用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.)
![](https://www.codeprj.com/image/aHR0cHM6Ly9pbWFnZXMyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvMTQyNzcyMS8yMDE4MDYvMTQyNzcyMS0yMDE4MDYyOTE0NDEyMTAxMS0xNjI0Nzg1NzcxLnBuZw==.png)
浏览器访问:
![](https://www.codeprj.com/image/aHR0cHM6Ly9pbWFnZXMyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvMTQyNzcyMS8yMDE4MDYvMTQyNzcyMS0yMDE4MDYyOTE0NDIyMTEwOC03ODA2NzM1MTAucG5n.png)
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
![](https://www.codeprj.com/image/aHR0cHM6Ly9pbWFnZXMyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvMTQyNzcyMS8yMDE4MDYvMTQyNzcyMS0yMDE4MDYyOTE0NTIwNTgzNC05NTQ0NTA1MTMucG5n.png)
Donate捐赠
如果我的文章帮助了你,可以赞赏我 1 元,让我继续写出更好的内容)
![](https://www.codeprj.com/image/aHR0cHM6Ly9pbWFnZXMyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvMTQyNzcyMS8yMDE4MDYvMTQyNzcyMS0yMDE4MDYyNzAwMDkyMzU3Ni0xMjc3OTQyNDc2LnBuZw==.png)
(微信) (支付宝)
微信/支付宝 扫一扫
|