Spring mvc:resource not finding *.ico files
Asked Answered
S

1

13

I have had a really hard time getting my Spring 3.0 application to recognize favicon.ico type files as a resource. I have defined my resource directory in my spring-context.xml file as follows:

<mvc:resources mapping="/ui/**" location="/ui/" />

This directory structure looks like:

/ui
  /images
  /styles
  /scripts
  ...

Spring hosts my images, scripts, and styles just fine. However, I get a 404 error when trying to retrieve any *.ico files in the images directory. All PNG, GIF, and JPG images work just fine in that same directory. I tried being more specific on which directories to host and even specified .ico files as resources in the context.xml file and still get the same results:

<mvc:resources mapping="/ui/images/*.ico" location="/ui/images" />

I've also tried adding a servlet mapping to the default servlet. This seemed to work for some when I researched online, but has not proven successful for me.

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.ico</url-pattern>
</servlet-mapping>

EDIT: I have also added the favicon.ico file to the root path of the web app. If I use a png file for the favicon, it works in every browser but IE. I would like to solve this problem for all browsers if possible. Any help at this point would be greatly appreciated.

EDIT2: I already have a link tag in the XHTML document:

<link rel="shortcut icon" type="image/vnd.microsoft.icon" href="/ui/images/favicon.ico" />
Sire answered 10/3, 2011 at 15:10 Comment(9)
You realise that favicon.ico has to go in the root path, right? i.e. /favicon.ico, not /ui/favicon.ico.Wist
Which AppServer or Web Server are you using?Wanids
@Wist - That's archaic. The new W3C recommendation doesn't have any such restriction.Wanids
@adarshr: Archaic it may well be, but it might also be the issue.Wist
@Wist - agree, favicons have always been a pain. @userXXX - best way to rule out such issues is to directly invoke the URL yoursite.com/context/path/name.ico in a browser, rather than depending on it being rendered in the favicon area.Wanids
Sorry guys, I forgot to add that detail. As part of troubleshooting the issue, I copied the favicon into the webapp root directory and still got no results. I think part of the problem is due to spring being mapped to the root path as well.Sire
@Wist I'm using tomcat 6 to host the application.Sire
Check if there are any MIME settings required for ICO extensions on Tomcat.Wanids
@Wist That worked! Thanks for your help! I will post the official answer.Sire
S
14

The solution for me, since I was using Tomcat 6 to host the application, was to add the MIME type to the web.xml file of the application as shown below.

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

Thanks skaffman!

Sire answered 10/3, 2011 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.