1. Introduction
1.绪论
In this tutorial, we’ll look at how we get and resolve Circular View Path errors in a Spring MVC application.
在本教程中,我们将研究如何在Spring MVC应用程序中获取和解决循环视图路径错误。
2. Dependencies
2.依赖性
To demonstrate this, let’s create a simple Spring Boot web project. First, we need to add the Spring Boot web starter dependency in our Maven project file:
为了证明这一点,让我们创建一个简单的Spring Boot网络项目。首先,我们需要在Maven项目文件中添加Spring Boot网络启动器依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3. Reproducing the Problem
3.重现问题
Then, let’s create a simple Spring Boot application with one Controller that resolves to one path:
然后,让我们创建一个简单的Spring Boot应用程序,其中有一个Controller,可解析到一个路径。
@Controller
public class CircularViewPathController {
@GetMapping("/path")
public String path() {
return "path";
}
}
The return value is the view name that will produce response data. In our case, the return value is path which is associated with the path.html template:
返回值是将产生响应数据的视图名称。在我们的例子中,返回值是path,它与path.html模板有关。
<html>
<head>
<title>path.html</title>
</head>
<body>
<p>path.html</p>
</body>
</html>
After we start the server, we can reproduce the error by making a GET request to http://localhost:8080/path. The result will be the Circular View Path error:
在我们启动服务器后,我们可以通过向http://localhost:8080/path发出GET请求来重现这个错误。其结果将是循环视图路径错误。
{"timestamp":"2020-05-22T11:47:42.173+0000","status":500,"error":"Internal Server Error",
"message":"Circular view path [path]: would dispatch back to the current handler URL [/path]
again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view,
due to default view name generation.)","path":"/path"}
4. Solutions
4.解决方案
By default, the Spring MVC framework applies the InternalResourceView class as the view resolver. As a result, if the @GetMapping value is the same as the view, the request will fail with the Circular View path error.
默认情况下,Spring MVC框架应用InternalResourceView类作为视图解析器。因此,如果@GetMapping值与视图相同,请求将因循环视图路径错误而失败。
One possible solution would be to rename the view and change the return value in the controller method.
一个可能的解决方案是重新命名视图并改变控制器方法的返回值。
@Controller
public class CircularViewPathController {
@GetMapping("/path")
public String path() {
return "path2";
}
}
If we don’t want to rename the view and change the return value in the controller method, then another solution is to choose another view processor for the project.
如果我们不想重命名视图和改变控制器方法的返回值,那么另一个解决方案是为项目选择另一个视图处理器。
For the most common cases, we can choose the Thymeleaf Java template engine. Let’s add the spring-boot-starter-thymeleaf dependency to the project:
对于最常见的情况,我们可以选择Thymeleaf Java模板引擎>。让我们把spring-boot-starter-thymeleaf依赖添加到项目中。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
After rebuilding the project we can run it again, and the request is successful. In this case, Thymeleaf replaces the InternalResourceView class.
重建项目后,我们可以再次运行它,请求成功了。在这种情况下,Thymeleaf替换了InternalResourceView类。
5. Conclusion
5.总结
In this tutorial, we looked at the Circular View path error, why it happens, and how to resolve the issue. As always, the full source code of the article is available over on GitHub.
在本教程中,我们研究了圆形视图路径错误,为什么会发生,以及如何解决这个问题。一如既往,该文章的完整源代码可在GitHub上获取。