之前自己寫的springmvc 默認首頁都是偷懶方式:
web.xml 中定義的默認首頁:
<welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list>
然后在index.html 中跳轉到springmvc 的動態鏈接
這樣地址上就有 http://www.xxx.com/index/home
今天客戶不想要/index/home。問題:welcome-file-list一般情況下只能使用靜態網頁,如果非要把他配置成SpringMVC的控制器URL就會報錯.
查了一下資料,得到解決辦法如下
web.xml 中如下改動
<welcome-file-list>
<welcome-file>index</welcome-file> <!-- 這里是index 沒有后綴名-->
</welcome-file-list>
<servlet-mapping>
<!-- 這個Servlet的名字是myproject-dispatcher,可以有多個DispatcherServlet,是通過名字來區分的。
每一個DispatcherServlet有自己的WebApplicationContext上下文對象。同時保存到ServletContext中和Request對象中 -->
<servlet-name>myproject-dispatcher</servlet-name>
<!-- 攔截*.do結尾的請求。 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myproject-dispatcher</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
注意:welcome-file-list配置的是沒有 / 的 index,下面為SpringMVC控制器單獨注冊了一個 /index 的URL(這個有 “/”)