我与Spring MVC 的恩怨一生 1

来源:互联网 发布:如何在淘宝上买东西 编辑:程序博客网 时间:2024/05/16 13:01

今天学习了Spring MVC以及使用Spring Test来测试控制器。兴冲冲的写了一个控制器,准备使用Spring Test来尝尝鲜。
代码如下:

@Controllerpublic class IndexController {    @RequestMapping(value = "/index", method = RequestMethod.GET)    public String index(){        return "index";    }}
public class HomeControllerTest {    @Test    public void testIndex() throws Exception{        IndexController controller = new IndexController();        MockMvc mockMvc = standaloneSetup(controller).build();        ResultActions actions = mockMvc.perform(get("/index"));        actions.andExpect(view().name("index"));    }}

运行,结果

javax.servlet.ServletException: Circular view path [index]: would dispatch back to the current handler URL [/index] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

怎么回事?

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。当你的请求路径与视图名相同时,就会发生死循环。

原创粉丝点击