SpringBoot 报错: Circular view path [readingList] 解决办法
发布日期:2021-05-08 23:20:18 浏览次数:22 分类:博客文章

本文共 1436 字,大约阅读时间需要 4 分钟。

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来继续转发该请求。

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

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

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

org.springframework.boot
spring-boot-starter-thymeleaf

spring中也会遇到这种情况的,原理同上,如果不声明viewResolver,就会出现这个问题的。我在controller中,不加@ResponseBody注解,直接返回"index"字符串,来映射

index.jsp 就会进入上面的代码,判断uri 和 jsp 的name相同,都是index

 
上一篇:javax.servlet.ServletException: No adapter for handler
下一篇:使用 EPPlus 封装的 excel 表格导入功能 (二) delegate 委托 --永远滴神

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年04月16日 09时36分14秒