Spring mvc:resource not found *.ico files

在使用 spring3 mvc DispatcherServlet 进行根目录映射时,会发下ico文件显示不了。

<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*Servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

web.xml 增加文件类型映射

<mime-mapping>
<extension>ico</extension>
<mime-type>image/vnd.microsoft.icon</mime-type>
</mime-mapping>

解决方法:

spring mvc servlet 配置(此处是 mvcServlet.xml )

<mvc:default-servlet-handler/>
<mvc:resources mapping=”/*.ico” location=”/” />

在页面head部分加入(可选)ico声明

<link rel=”shortcut icon” type=”image/x-icon” href=”${pageContext.request.contextPath}/favicon.ico”/>