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. LeetCode之“动态规划”:Maximum Product Subarray

    题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has t ...

  2. PS 滤镜——平面坐标变换到极坐标

    %%% orthogonal coordinate to polar coordinate %%% 平面坐标转极坐标 clc; clear all; close all; addpath('E:\Ph ...

  3. Android绘图机制(二)——自定义View绘制形, 圆形, 三角形, 扇形, 椭圆, 曲线,文字和图片的坐标讲解

    Android绘图机制(二)--自定义View绘制形, 圆形, 三角形, 扇形, 椭圆, 曲线,文字和图片的坐标讲解 我们要想画好一些炫酷的View,首先我们得知道怎么去画一些基础的图案,比如矩形,圆 ...

  4. 安卓笔记-可以滚动的TextView

    本来是想做一个显示文字信息的,当文字很多时View的高度不能超过一个固定的值,当文字很少时View的高度小于那个固定值时,按View的高度显示.因为ScrollView没有maxHeight,无法满足 ...

  5. 关于Block的使用和5点注意事项

    一.概念 首先需要了解的是Block是一个代码块,是一个变量的形式存在的. 二.构成了解 我们需要在函数中声明block,因为是变量的形式,而且存在静态变量形式 类型1:  NSString* (^b ...

  6. javascript中通过元素id和name直接取得元素

    我们知道一些第三方的js库对如何快速选取html中的元素做了一些简化,貌似十分高深莫测,其实也不然.而且js本身自带了对于特殊元素的简便选取的方法,下面就为大家简单介绍下. 在html中,一般最直接的 ...

  7. git使用中checkout生成临时br的问题(吓出一身冷汗啊)

    git中几天前漫不经心的使用了git checkout ver_hash的命令,结果push到远程库都提示everything is up-to-date,实际神马都没提交上去啊!但看本地log中的确 ...

  8. SpringBoot功能持续更新

    [定时任务] 1.启动总开关 @EnableScheduling加在@SpringBootApplication注解的start入口处,表示启动总开关 @SpringBootApplication @ ...

  9. 转载一篇makefile,说的很详细

    March 3, 2015 8:19 PM 原文见:https://www.cnblogs.com/OpenShiFt/p/4313351.html Makefile 文件的编写 学习前的准备 需要准 ...

  10. 苹果公司揭秘首批列入 Swift 源代码兼容性开源项目清单

    源代码兼容性是 Swift 未来的目标.为了实现这一目标,(苹果公司的 swift 编译器团队)建立了一个源兼容性测试套件,用于根据 Swift 源代码(逐渐增加)语料库对编译器进行回归测试更改. 添 ...