Spring mvc:资源找不到 *.ico 文件
我很难让我的 Spring 3.0 应用程序将 favicon.ico
类型文件识别为资源。我在 spring-context.xml
文件中定义了我的资源目录,如下所示:
<mvc:resources mapping="/ui/**" location="/ui/" />
此目录结构如下所示:
/ui
/images
/styles
/scripts
...
Spring 很好地托管我的图像、脚本和样式。但是,当我尝试检索图像目录中的任何 *.ico
文件时,出现 404 错误。所有 PNG、GIF 和 JPG 图像在同一目录中都可以正常工作。我尝试更具体地指定要托管的目录,甚至指定 .ico
文件作为 context.xml
文件中的资源,但仍然得到相同的结果:
<mvc:resources mapping="/ui/images/*.ico" location="/ui/images" />
我也尝试过添加 servlet 映射到默认 servlet。当我在网上研究时,这似乎对某些人有用,但对我来说并不成功。
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ico</url-pattern>
</servlet-mapping>
编辑:我还将 favicon.ico 文件添加到网络应用程序的根路径中。如果我使用 png 文件作为图标,它可以在除 IE 之外的所有浏览器中使用。如果可能的话,我想为所有浏览器解决这个问题。 此时任何帮助将不胜感激。
EDIT2:我在 XHTML 文档中已经有一个链接标签:
<link rel="shortcut icon" type="image/vnd.microsoft.icon" href="/ui/images/favicon.ico" />
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" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我使用 Tomcat 6 来托管应用程序,因此我的解决方案是将 MIME 类型添加到应用程序的 web.xml 文件中,如下所示。
谢谢斯卡夫曼!
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.
Thanks skaffman!