Circular view path [home]: would dispatch back to the current handler URL [/home] again. Check your ViewResolver setup!
Circular view path [home]: would dispatch back to the current handler URL [/home] again. Check your ViewResolver setup!
(Hint: This may be the result of an unspecified view, due to default view name generation.)
1. 问题的现象
比如在webConfig中定义了一个viewResolver

public class WebConfig extends WebMvcConfigurerAdapter { //配置JSP视图解析器
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
}

然后定义了一个controller,URL路径为"/home", 它返回名字叫home的view

@Controller
public class HomeController {
@RequestMapping(value = "/home", method=GET)
public ModelAndView home() {
String message = "Hello";
return new ModelAndView("home", "home", message);
}
}

然后定义了个Test

public class HomeControllerTest {
@Test
public void testHomePage() throws Exception {
HomeController controller = new HomeController();
MockMvc mockMvc = standaloneSetup(controller).build();
mockMvc.perform(get("/home")).andExpect(view().name("home"));
} }

那么执行Test是就会报类似错误并抛出异常:
Circular view path [home]: would dispatch back to the current handler URL [/home] again. Check your ViewResolver setup!
(Hint: This may be the result of an unspecified view, due to default view name generation.)
2. 首先,首先说下原因:
-------------------------------
当没有声明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异常。
3. 其次,如何解决?
通过原因分析,造成问题有两个因素:1). 缺省转发, 2). view和path同名
那么消除这两个因素任何一个就可以解决这个问题。
3.1 解决办法一: 消除缺省转发
虽然在controller中已经定义了view, 但在使用Spring Test时却仍然无效,这个不知道什么原因,也许是Spring Test的Bug, 有待探究。既然无效,那就在Test中重新定义一下view
, 这样虽然麻烦点,但毕竟消除了缺省转发,所以可以解决问题。示例代码如下:

public class TestJavaConfig { private MockMvc mockMvc; @InjectMocks
private StudentController studentController; @Mock
private StudentService studentService; @Before
public void setUp(){
MockitoAnnotations.initMocks(this);
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); //在test中重新配置视图解析器
resolver.setPrefix("/WEB_INF/views");
resolver.setSuffix(".jsp");
mockMvc = MockMvcBuilders.standaloneSetup(studentController).setViewResolvers(resolver).build(); }
@Test
public void testList()throws Exception{
mockMvc.perform(get("/home")).andExpect(view().name("home"));
}

3.2 解决办法二: 修改view和path,让他们不同名
这个方法最简单,建议用这种办法,比如上面的home视图, 只要我们的path不是"/home"就可以,可以改view名字(比如改成homepage),或者修改/path(比如/root).
https://www.cnblogs.com/chry/p/6240965.html
Circular view path [home]: would dispatch back to the current handler URL [/home] again. Check your ViewResolver setup!的更多相关文章
- Circular view path [mydemo]: would dispatch back to the current handler URL [/mydemo] again. Check your ViewResolver setup!
简单创建一个springboot工程 pom.xml <?xml version="1.0" encoding="UTF-8"?><proje ...
- Circular view path xxx would dispatch back to the current handler URL,Check your ViewResolver setup
Circular view path xxx would dispatch back to the current handler URL 通过原因分析,造成问题有两个因素:1). 缺省转发, 2). ...
- javax.servlet.ServletException: Circular view path [index]: would dispatch back to the current handler URL [/pay/index] again. Check your ViewResolver setup!
2019-08-08 17:12:03.544 ERROR 13748 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Se ...
- mock测试出现Circular view path [trade_records]: would dispatch back to the current handler URL
这是因为你的Controller中返回的视图名称与你当前的requestMapping名称一样,这并没有很好的解决方案,除非你改掉其中一个名字. 因为springframework test时你并没有 ...
- 如何在Spring MVC Test中避免”Circular view path” 异常
1. 问题的现象 比如在webConfig中定义了一个viewResolver public class WebConfig extends WebMvcConfigurerAdapter { //配 ...
- 如何在Spring MVC Test中避免”Circular view path” 异常(转)
文章转自http://www.cnblogs.com/chry/p/6240965.html 1. 问题的现象 比如在webConfig中定义了一个viewResolver public class ...
- Spring Boot项目Circular view path问题解决
使用Spring Boot创建Spring MVC项目,访问url请求出现问题:Circular view path 1.问题描述 控制台打印: javax.servlet.ServletExcept ...
- SpringBoot 报错: Circular view path [readingList] 解决办法
spring boot报错: Circular view path [readingList]: would dispatch back to the current handler URL [/re ...
- web项目——javax.servlet.ServletException: Circular view path [registerForm]
报错: 控制台输出: 三月 21, 2019 10:12:32 上午 org.springframework.web.servlet.PageNotFound noHandlerFound 警告: N ...
随机推荐
- hadoop任务监控页面namenode:50030(在hadoop配置中查找集群jobtracker的ip,访问50030)
公司集群,配置的hadoop.执行job,想去看看运行状态,却不知道jobtracker的机器ip: 查询hadoop 的jobtrack机器的ip,就查看文件conf/mapred-site.xml ...
- Mahout SlopOne
关于推荐引擎 如今的互联网中,无论是电子商务还是社交网络,对数据挖掘的需求都越来越大了,而推荐引擎正是数据挖掘完美体现:通过分析用户历史行为,将他可能喜欢内容推送给他,能产生相当好的用户体验,这就是推 ...
- Android Studio Gradle Configuration Errors总结
初次看到这个错误,我从下手Error:Configuration with name 'default' not found. 只知道这是由于android的grad项目构建的时候出现的错误,但是具 ...
- 【6】-BAT面试之操作系统内存详解
本文主要参考两篇博客,读后整理出来,以供大家阅读,链接如下: http://blog.jobbole.com/95499/?hmsr=toutiao.io&utm_medium=toutiao ...
- multiset基础学习,可以有重复类型的多重集合容器
#include <set> #include <iostream> using namespace std; struct Student { char *name; int ...
- nasm预处理器(2)
多行宏 %macro: %macro foo 2 push rax push rbx mov rax,%1 mov rbx,%2 pop rbx pop rax %endmacro 宏名称后的数字代表 ...
- DB Query Analyzer 5.02 is distributed, 53 articles concerned have been published
DB Query Analyzer is presented by Master Gen feng, Ma from Chinese Mainland. It has English version ...
- Core Animation简介
一.Core Animation简介 * Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代 ...
- 万水千山ABP - 弹出对话框禁用回车
模态对话框中禁用回车 ABP Zero 中,使用弹出对话框进行实体编辑,回车时会自动保存并关闭对话框.那么如何禁用这个回车功能 ? 查看实体列表视图 index.cshtml 所对应加载的脚本文件 i ...
- Oracle——多表查询
本次预计讲解的知识点 1. 多表查询的操作.限制.笛卡尔积的问题: 2. 统计函数及分组统计的操作: 3. 子查询的操作,并且结合限定查询.数据排序.多表查询.统计查询一起完成各个复杂查询的操作: 一 ...