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

原文地址:https://www.cnblogs.com/softidea/p/8513780.html

时间: 2024-10-11 21:14:14

Circular view path [home]: would dispatch back to the current handler URL [/home] again. Check your ViewResolver setup!的相关文章

IDEA下spring boot+fremarker启动报错would dispatch back to the current handler URL [/hello] again. Check your ViewResolver setup!

详细报错代码(ps:标题中写不下报错主要代码) javax.servlet.ServletException: Circular view path [hello]: would dispatch back to the current handler URL [/hello] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default vie

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

这是因为你的Controller中返回的视图名称与你当前的requestMapping名称一样,这并没有很好的解决方案,除非你改掉其中一个名字. 因为springframework test时你并没有指定一个ViewResolver,默认启用的是InternalResourceViewResolver. Stack Overflow:https://stackoverflow.com/questions

javax.servlet.ServletException: Circular view path [account]: would dispatch back to the current han

这是在spring boot/MVC 中出现的一个异常原因:当没有声明ViewResolver时,spring会注册一个默认的ViewResolver,就是JstlView的实例, 该对象继承自InternalResoureView.JstlView用来封装JSP或者同一Web应用中的其他资源,它将model对象作为request请求的属性值暴露出来, 并将该请求通过javax.servlet.RequestDispatcher转发到指定的URL.Spring认为, 这个view的URL是可以用

Can't dispatch DDM chunk 52454151: no handler defined

[2010-07-12 10:10:06 - Hello Google Android]ActivityManager: DDM dispatch reg wait timeout [2010-07-12 10:10:06 - Hello Google Android]ActivityManager: Can't dispatch DDM chunk 52454151: no handler defined [2010-07-12 10:10:06 - Hello Google Android]

Spring Boot 添加JSP支持【转】

Spring Boot 添加JSP支持 大体步骤: (1)            创建Maven web project: (2)            在pom.xml文件添加依赖: (3)            配置application.properties支持jsp (4)            编写测试Controller (5)          编写JSP页面 (6)          编写启动类App.Java 1,FreeMarker2,Groovy3,Thymeleaf (s

Spring boot异常统一处理方法:@ControllerAdvice注解的使用

1.首先创建异常处理包和类 2.使用@ControllerAdvice注解,全局捕获异常类,只要作用在@RequestMapping上,所有的异常都会被捕获 package com.example.demo.exception; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; impor

Spring boot异常统一处理方法:@ControllerAdvice注解的使用、全局异常捕获、自定义异常捕获

https://www.cnblogs.com/goloving/p/9142222.html 一.全局异常 1.首先创建异常处理包和类 2.使用@ControllerAdvice注解,全局捕获异常类,只要作用在@RequestMapping上,所有的异常都会被捕获 package com.example.demo.exception; import org.springframework.web.bind.annotation.ControllerAdvice; import org.spri

【jsp+jpa】Check your ViewResolver setup!

困扰了好几天的坑 javax.servlet.ServletException: Circular view path [fileupload]: would dispatch back to the current handler URL [/web/fileupload] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view

《Spring技术内幕》笔记-第四章 Spring MVC与web环境

?上下文在web容器中的启动 1,IoC容器的启动过程 IoC的启动过程就是建立上下文的过程,该上下文是与ServletContext相伴.在Spring中存在一个核心控制分发器,DispatcherServlet,这是Spring的核心.在web容器启动Spring应用程序时,首先建立根上下文,然后ContextLoader建立WebApplicationContext. Web容器中启动Spring过程如下: 在web.xml中,已经配置了ContextLoadListener,该类实现了S