Circular view path [...]: would dispatch back to the current handler
2016-10-06 11:54
681 查看
在对mvc进行测试时,即便指定了配置文件中的视图解析依然没有作用,测试时会报异常:
javax.servlet.ServletException: Circular view path [list]: would dispatch back to the current handler URL [/list] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
网上提到此问题的有:
http://stackoverflow.com/questions/18813615/how-to-avoid-the-circular-view-path-exception-with-spring-mvc-test
以及
https://myshittycode.com/2014/01/17/mockmvc-circular-view-path-view-would-dispatch-back-to-the-current-handler-url-view-again/
摘录答案中的话,When you don’t declare a ViewResolver, Spring registers a default InternalResourceViewResolver which creates instances of JstlView for rendering the View.
当你没有声明ViewResolver时,spring会给你注册一个默认的ViewResolver,其是JstlView的实例。它通过RequestDispatcher寻找资源(视图),不过这个资源也可能是Servlet,也就是说,Controller中方法返回字符串(视图名),也可能会解析成Servlet。当你的请求路径与视图名相同时,就会发生死循环。
这就是异常提示的信息。不过我依然不明白为什么配置了视图解析还是不行,依据网上的方法可以解决问题,那就是在测试类中再配置一遍视图
javax.servlet.ServletException: Circular view path [list]: would dispatch back to the current handler URL [/list] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
网上提到此问题的有:
http://stackoverflow.com/questions/18813615/how-to-avoid-the-circular-view-path-exception-with-spring-mvc-test
以及
https://myshittycode.com/2014/01/17/mockmvc-circular-view-path-view-would-dispatch-back-to-the-current-handler-url-view-again/
摘录答案中的话,When you don’t declare a ViewResolver, Spring registers a default InternalResourceViewResolver which creates instances of JstlView for rendering the View.
当你没有声明ViewResolver时,spring会给你注册一个默认的ViewResolver,其是JstlView的实例。它通过RequestDispatcher寻找资源(视图),不过这个资源也可能是Servlet,也就是说,Controller中方法返回字符串(视图名),也可能会解析成Servlet。当你的请求路径与视图名相同时,就会发生死循环。
这就是异常提示的信息。不过我依然不明白为什么配置了视图解析还是不行,依据网上的方法可以解决问题,那就是在测试类中再配置一遍视图
@ContextConfiguration(classes = {com.wthfeng.hello.config.WebConfig.class}) //加载配置文件 @WebAppConfiguration //测试时启动一个web服务 @RunWith(SpringJUnit4ClassRunner.class) //表明用spring test进行测试 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(); //配置视图解析器 resolver.setPrefix("/WEB_INF/views"); resolver.setSuffix(".jsp"); mockMvc = MockMvcBuilders.standaloneSetup(studentController).setViewResolvers(resolver).build(); } @Test public void testList()throws Exception{ mockMvc.perform(get("/list")).andExpect(view().name("list")).andExpect(model().attributeExists("StudentList")); } }
相关文章推荐
- Circular view path : would dispatch back to the current handler URL again. Check your ViewResolver s
- Spring boot 报错:Circular view path [...]: would dispatch back to the current handler
- spring cloud 报[Circular view path [refundbbb]: would dispatch back to the current handler URL [/refu
- Circular view path [...]: would dispatch back to the current handler
- 《Springboot极简教程》问题解决:javax.servlet.ServletException: Circular view path [login]: would dispatch back to the current handler URL
- Circular view path [login3]: would dispatch back to the current handler URL [/Spring_Demo/login3]
- Circular view path [home]: would dispatch back to the current handler URL [/home] again. Check your ViewResolver setup!
- Circular view path [index]: would dispatch back to the current handler URL [/index] again.
- Circular view path [faults]: would dispatch back to the current handler URL [/pulsar/faults] again
- javax.servlet.ServletException: Circular view path [index]: would dispatch back to the current handl
- spring boot would dispatch back to the current handler URL [/hello] again. Check your ViewResolver s
- How to add subview to a webview so that the subview would scroll along with webview?
- Software Engineering: A Look Back and A Path to the Future
- 我的Android进阶之旅------>Android无第三方Jar包的源代报错:The current class path entry belongs to container ...的解决方法
- Switch view in the InfoPath form according to the whether current user is in SharePoint group admin
- 我的Android进阶之旅------>Android无第三方Jar包的源代报错:The current class path entry belongs to container ...的解决方法
- Ubuntu安装VMware-tools时出现the path "" is not a valid path would you like to change it-完美解决方案
- centos 7 The path "" is not a valid path to the 3.10.0-229.el7.x86_64 kernel headers. Would you like to change it?
- [Tutor] how to change the current working directory path in ipython.
- linux下安装PHP的CI框架,出现Your view folder path does not appear to be set correctly. Please open the following file and correct this: i