在项目的WebContent下放了一个index.html文件和404.html文件,我希望访问springMvc访问项目根路径的文件时,能够打开这个index.html文件,同样,当网页无法找到报404错误时,返回404.html这个网页。
很郁闷的是,项目启动以后,控制台会打印
1
|
2014
-
08
-
31
13
:
38
:
20
[ WARN] - org.springframework.web.servlet.PageNotFound -DispatcherServlet.java(
1108
) -No mapping found
for
HTTP request with URI [/page404.html] in DispatcherServlet with name
'springMvc3'
|
并且访问localhost时,总是不能找到index.html,也无法返回404.html,网上找了很久也没找到原因,后来自己研究了将近一个中午,总算解决问题了。
解决方案就是:
在spring的配置文件中,一定要加上这句
1
|
<mvc:
default
-servlet-handler />
|
这句的意思是:访问项目时,首先走默认的 web.xml 配置的servlet,没有的话才找对应controller
我的web.xml中配置了以下内容
1
2
3
4
5
6
7
8
9
|
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 通过错误码来配置error-page ,配置了当系统发生
404
错误时,跳转到错误处理页面NotFound.jsp。 -->
<error-page>
<error-code>
404
</error-code>
<location>/page404.html</location>
</error-page>
|
在spring的配置文件中,如果有这句配置
1
|
<mvc:
default
-servlet-handler />
|
则代表着访问项目根路径localhost的话,spring会直接返回index.html,如果index.html不存在的话,则会返回相应的controller所返回的页面。
相反,如果没有这句配置
1
|
<mvc:
default
-servlet-handler />
|