spingmvc項目根路徑訪問不到

 問題: 如何改mvc中項目的歡迎頁,或者叫做根路徑

一個東西快弄完了,就剩下一個問題,應該是個小問題。就是mvc項目的歡迎頁,怎麼給改下呢

訪問根路徑http://localhost/demo 怎麼都訪問不到webapp下index.html

 讓其跳轉到http://localhost:8080/demo/index


1. 缺省的流程


先看看缺省的根路徑流程:

web.xml 中什麼沒有配置任何有關歡迎頁的信息!其實這時等效於如下配置:這個會由Web容器最先訪問!

歡迎頁配置

<welcome-file-list>

<welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list>

servlet攔截配置

<servlet> <servlet-name>demo-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>demo-servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

而項目目錄下,有個index.html文件,進行了跳轉:

<html> <head>   <meta http-equiv="Refresh" content="0; URL=index/"> </head> </html>

按照上面的配置,使用jetty服務器沒有生效,http://localhost/demo訪問不到根目錄下index.html 

然後嘗試下面的步驟

2. 如何直接對根路徑進行攔截

還是直接說流程吧:

必須在web.xml中加入如下:

<welcome-file-list>     <welcome-file>index.jsp</welcome-file> </welcome-file-list>

在web.xml中加入這段代碼後發現並沒有生效,不清楚原因,使用的是jetty7服務器,後來查了相關資料,發現有兩種方式。


@RequestMapping("/")
public ModelAndView index() {
Map<String, Object> modelMap = new HashMap<String, Object>();
System.out.println("**********************");
/*
* modelMap.put("basPostTypeList",homeService.findBasPostTypeList());
* modelMap
* .put("busHotCompanyPostList",busHotCompanyPostService.getAllByOrderNum
* ());
*/
return new ModelAndView("index", modelMap);
}

或者,沒有controller,只有view,也可以簡化,在servlet的配置文件中加入:

<mvc:view-controller path="/" view-name="index"/>

如果同時都有。反正只會有一個起作用。一般是先掃描的起作用。誰會先掃描到,就是看和<mvc:annotation-driven />比較,誰在前面。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章