mvc:view-controller標籤
如果發送的請求不想通過controller,只想直接地跳轉到目標頁面,這時候就可以使用mvc:view-controller標籤
在配置文件中配置:
<mvc:view-controller path="/hello" view-name="hello"></mvc:view-controller>
path=”/hello” 就是你訪問的路徑(相當於RequestMapping(“/hello”))
view-name=”hello”是你所要的視圖(如hello.jsp,相當於return “hello”) 配置了這個後對於/hello請求,就會直接交給dispatcherServlet,然後使用ViewResolver進行解析。
相當於以下代碼
@RequestMapping(value="/hello")
public String hello(){
System.out.println("hello");
return "hello";
}
也可以配置成重定向或者轉發:
<mvc:view-controller path="/index" view-name="redirect:hello"></mvc:view-controller>
相當於
@RequestMapping("/index")
public String index(){
return "redirect:hello";
}
注意:
使用了這個標籤後必須配置 <mvc:annotation-driven />
否則會造成所有的@Controller註解無法解析,導致404錯誤。
如果請求存在處理器,則這個標籤對應的請求處理將不起作用。因爲請求是先去找處理器處理,如果找不到纔會去找這個標籤配置。