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!的更多相关文章

  1. 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 ...

  2. 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). ...

  3. 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 ...

  4. mock测试出现Circular view path [trade_records]: would dispatch back to the current handler URL

    这是因为你的Controller中返回的视图名称与你当前的requestMapping名称一样,这并没有很好的解决方案,除非你改掉其中一个名字. 因为springframework test时你并没有 ...

  5. 如何在Spring MVC Test中避免”Circular view path” 异常

    1. 问题的现象 比如在webConfig中定义了一个viewResolver public class WebConfig extends WebMvcConfigurerAdapter { //配 ...

  6. 如何在Spring MVC Test中避免”Circular view path” 异常(转)

    文章转自http://www.cnblogs.com/chry/p/6240965.html 1. 问题的现象 比如在webConfig中定义了一个viewResolver public class ...

  7. Spring Boot项目Circular view path问题解决

    使用Spring Boot创建Spring MVC项目,访问url请求出现问题:Circular view path 1.问题描述 控制台打印: javax.servlet.ServletExcept ...

  8. SpringBoot 报错: Circular view path [readingList] 解决办法

    spring boot报错: Circular view path [readingList]: would dispatch back to the current handler URL [/re ...

  9. web项目——javax.servlet.ServletException: Circular view path [registerForm]

    报错: 控制台输出: 三月 21, 2019 10:12:32 上午 org.springframework.web.servlet.PageNotFound noHandlerFound 警告: N ...

随机推荐

  1. Shell编程入门(第二版)(下)

    ... ... command n done #select把关键字中的每一项做成类似表单,以交互的方式执行do和done之间的命令 示例-select.sh [python] view plainc ...

  2. linux服务搭建---yum源服务搭建

    yum源服务 1.本地yum源 2.yum源不在本地          1>  ftp服务器     2>  nfs服务器 1.本地yum源 前提:    linux系统   找到一个相应 ...

  3. Linux中find的使用(转)

    本文转自:迷途花开 另一值得参考的是吴秦先生的博文linux中强大且常用命令:find.grep. find命令用于查找文件和目录,任何位于参数之前的字符串都将被视为欲查找的目录. find 可以指定 ...

  4. 地产IT人福利:帆软地产BI解决方案全解析

    解决方案下载地址 帆软大型地产集团项目解决方案 下载地址:http://pan.baidu.com/s/1pJGeqKF帆软地产BI解决方案之KPI考核系统 下载地址:http://pan.baidu ...

  5. BMP 转 YUV (BMP2YUV)

    本文介绍BMP 转 YUV.其实这是以前"数据压缩"实验课上的内容,前几天有人问我相关的问题,突然发现自己有一段时间没有接触BMP也有些生疏了,因此翻出资料总结一下. BMP文件格 ...

  6. Android开发技巧——自定义控件之使用style

    Android开发技巧--自定义控件之使用style 回顾 在上一篇<Android开发技巧--自定义控件之自定义属性>中,我讲到了如何定义属性以及在自定义控件中获取这些属性的值,也提到了 ...

  7. 安卓TV开发(前言)— AndroidTV模拟器初识与搭建

    原文:http://blog.csdn.net/sk719887916/article/details/39612577skay 前言:移动智能设备的发展,推动了安卓另一个领域,包括智能电视和智能家居 ...

  8. nasm预处理器(4)

    nasm定义了一套标准宏,当开始处理源文件时,这些宏都已经被定义了,如果希望程序在执行前没有预定义的宏存在,可以使用%clear清空预处理器的一切宏. __NASM_MAJOR__ 主版本号 __NA ...

  9. 恶补web之二:css知识(2)

    css字体属性定义文本的字体系列,大小,加粗,风格和变形等. css中包含两种字体系列:通用字体系列和特定字体系列. font-family属性定义文本的字体系列: body {font-family ...

  10. balanced binary tree(判断是否是平衡二叉树)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...