Circular view path

缺乏、安全感 2022-06-07 00:58 274阅读 0赞

最近学习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来继续转发该请求。如下代码:

  1. if (path.startsWith("/") ? uri.equals(path) : uri.equals(StringUtils.applyRelativePath(uri, path))) {
  2. throw new ServletException("Circular view path [" + path + "]: would dispatch back " +
  3. "to the current handler URL [" + uri + "] again. Check your ViewResolver setup! " +
  4. "(Hint: This may be the result of an unspecified view, due to default view name generation.)");
  5. }

从这段代码可以看出,如果你的view name和你的path是相同的字符串,根据Spring的转发规则,就等于让自己转发给自己,会陷入死循环。所以Spring会检查到这种情况,于是抛出Circular view path异常。

boot中,使用thymeleaf,会加入starter依赖,引入这个依赖的时候了,boot会自动进行配置转发规则,所以只要记得在pom文件中声明依赖即可

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>
  5. spring中也会遇到这种情况的,原理同上,如果不声明viewResolver,就会出现这个问题的。我在controller中,不加@ResponseBody注解,直接返回"index"字符串,来映射
  6. index.jsp 就会进入上面的代码,判断uri 和 jsp 的name相同,都是index

发表评论

表情:
评论列表 (有 0 条评论,274人围观)

还没有评论,来说两句吧...

相关阅读