【问题标题】:favicon.ico doesn't displayed in spring mvcfavicon.ico 不显示在 spring mvc 中
【发布时间】:2015-03-10 13:21:57
【问题描述】:

我正在尝试使用 spring mvc 4 和 apache tomcat 7(在 firefox 中)添加 favicon.ico。 我检查了许多解决方案,但似乎没有一个有效。

web.xml:

<mime-mapping>
    <extension>ico</extension>
    <mime-type>image/x-icon</mime-type>
</mime-mapping>

mvc-core-config.xml:

<mvc:default-servlet-handler/>
<!-- <mvc:resources mapping="/resources/**" location="/resources/css" /> -->
<mvc:resources location="/favicon.ico" mapping="/favicon.ico" />

jsp:

<link href="MYPROJECT/favicon.ico" rel="icon" type="image/x-icon">

(我也尝试过不使用 MYPROJECT 和其他变体...)。

我将 favicon.ico 文件放在 webapps 下(也在其他文件夹中尝试过)。

加载页面时,不显示图标。

注意:我尝试直接加载图标http://localhost:8080/MYPROJECT/favicon.ico,但收到以下错误消息:the image 'http://localhost:8080/MYPROJECT/favicon.ico' cannot be displayed because it contains errors。我下载了其他 favicon.ico,但图标看起来已损坏。

在 Firefox 中检查元素时,我不会调用 favicon.ico。

有什么建议吗?

更新: 在我的 Eclipse 控制台中,我看到:

查找路径/的处理程序方法 15:48:05.622 [http-bio-8080-exec-6] 调试 o.s.w.s.m.m.a.RequestMappingHandlerMapping - 没有找到 [/] 的处理程序方法 15:48:05.622 [http-bio-8080-exec-6] 调试 o.s.w.s.m.m.a.RequestMappingHandlerMapping - 查找路径 / 的处理程序方法 15:48:05.623 [http-bio-8080-exec-6] 调试 o.s.w.s.m.m.a.RequestMappingHandlerMapping - 没有找到 [/] 的处理程序方法 15:48:05.623 [http-bio-8080-exec-6] 调试 o.s.w.s.h.SimpleUrlHandlerMapping - 使用处理程序 [org.springframework.web.servlet.mvc.ParameterizableViewController@380baa3a] 和 1 个拦截器将 [/] 映射到 HandlerExecutionChain

更新2 mvc 配置 xml:

<import resource="mvc-view-config.xml"/>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>     
<mvc:annotation-driven conversion-service="conversionService"/>   
<context:component-scan base-package="controllers,logic.preprocess"/>
<mvc:view-controller path="/" view-name="index" />
<mvc:default-servlet-handler/>

<mvc:resources location="/favicon.ico" mapping="/favicon.ico" />

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
    p:basename="messages/messages"/>

</beans>

包含的mvc-view-config.xml:

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="viewResolvers">
        <list>
            <bean
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="viewClass"
                    value="org.springframework.web.servlet.view.JstlView" />
                <property name="prefix" value="/WEB-INF/jsp/" />
                <property name="suffix" value=".jsp" />
                <property name="contentType" value="text/html;charset=UTF-8"></property>
            </bean>


<bean       class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="order" value="1"/> </bean>

 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="order" value="2" /> </bean>

 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
     <property name="order" value="3" />
     <property name="alwaysUseFullPath" value="true" />
     <property name="mappings">
     <props>
         <prop key="controllers/TaskController">taskController</prop>
         <prop key="controllers/ResultController">resultController</prop>
     </props>
     </property>
 </bean>

更新 3 我将文件放在资源/图像下,它似乎只能在 chrome 上部分工作(没有快捷方式),但不能在 firefox 上工作。

谢谢, 迈克

【问题讨论】:

    标签: java spring-mvc favicon


    【解决方案1】:

    在 FireFox 和 Chrome 中测试:我遇到了同样的问题。以下是如何解决它。我已经做了this answer suggested:

    web.xml:

    <mime-mapping>
        <extension>ico</extension>
        <mime-type>image/x-icon</mime-type>
    </mime-mapping>
    

    mvc-dispatcher-servlet.xml:

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

    (在我的情况下不需要&lt;mvc:default-servlet-handler /&gt;

    genericpage.tag(我使用模板,但这可以是你的jsp文件):

    <link rel="shortcut icon" href='<c:url value="/resources/images/favicon.ico" />' type="image/x-icon">
    <link rel="icon" href='<c:url value="/resources/images/favicon.ico" />' type="image/x-icon">
    

    注意:请注意,您必须在 href 中使用 &lt;c:rul value= 位,否则它将不起作用。

    项目结构:

    Web pages
    +--META-INF
    +--WEB-INF
    +--resources
       +--mytheme
          +--css
          +--images
             +--favicon.ico
    

    由于某种原因,它在 /resources/mytheme/ 中时不起作用

    【讨论】:

    • 感谢您的解释。太荒谬了,要让一个网站图标显示出来需要这么多工作。
    【解决方案2】:

    存储 favicon.ico - WEB-INF 目录中的一个。

     <link href="/favicon.ico" rel="icon" type="image/x-icon">
    

    【讨论】:

    • 还是不行。查看我的更新Did not find handler method for [/] - 也许它是相关的?
    • 你能发布你的 mvc config.xml 的其余部分吗?
    • 在 Firefox 中检查元素时,我不会调用 favicon.ico。
    【解决方案3】:

    您需要在您的 Web 应用程序调度程序 servlet 中添加以下标记,即

    springmvc-dispatcher-servlet.xml:
    

    指定资源位置以加载 JS、CSS、图像等

    <mvc:resources mapping="/resources/**" location="/resources/" 
            cache-period="45556999"/>
    

    您的网络应用程序结构应如下所示

    您的 Web 应用程序 WebContent 文件夹结构应如下所示

    WebContent
    
    +resources
         -->images
        -->favicon.ico
    

    将这些行放在您的 Web 应用程序 jsp 页面之间的 &lt;header&gt;&lt;/header&gt; 标记之间,如下所示

    <head>
    
        <link rel="shortcut icon" href='<c:url value="/resources/images/favicon.ico" />'     type="image/x-icon">
        <link rel="icon" href='<c:url value="/resources/images/favicon.ico" />' type="image/x-icon">
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Login</title>
        <link href="<c:url value="/resources/css/bootstrap.css" />" rel="stylesheet">
        <link href="<c:url value='/resources/css/app.css' />" rel="stylesheet">
    </head>
    

    【讨论】:

      猜你喜欢
      • 2013-06-15
      • 2018-02-28
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      • 2011-08-28
      • 2010-10-03
      相关资源
      最近更新 更多