Search code examples
javatomcatspring-mvcfavicon

favicon.ico not displaying in spring mvc 3.2.2 per Tomcat 7.0?


I'm new to spring mvc and my favicon.ico is not showing up in browser tab using spring 3.2.2 using tomcat 7.0. I have tried looking at related googling but still cannot get it to show up in the browser tab (FF, Chrome, IE...all latest versions to not work) as well as clearing cache and restarting browser.

  • favicon.ico

Located in root of webapp folder (src/main/webapp)

  • index.jsp
<!DOCTYPE html>  
      <html> 
         <head>         
            <link href="favicon.ico" rel="shortcut icon" >
  o o o
  • mvc-dispatcher-server.xml
<context:component-scan base-package="com.website.controllers" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
      <value>/WEB-INF/views/</value>
  </property>
  <property name="suffix">
      <value>.jsp</value>
  </property>
</bean>
  • web.xml
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

Solution

  • Make sure the icon is served, i.e. make a request to /favicon.ico and see if it renders.

    If it does, add this to your web.xml to make sure Tomcat sends the correct Content-Type with the response:

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

    Make sure <mvc:default-servlet-handler /> is present if you map the dispatcher servlet to /.