Question

I am trying to use favicon in my spring application hosted locally. As per favicon.ico not displaying in spring mvc 3.2.2 per Tomcat 7.0? , I have added

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

in my web.xml:

 <display-name>Coaching</display-name>
<servlet>
    <servlet-name>Coaching</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

<mime-mapping>
    <extension>ico</extension>
    <mime-type>image/x-icon</mime-type>
</mime-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Adding

       <mvc:default-servlet-handler /> 

in config.xml is giving me error while running the application or application is not running actually. My applicationContext.xml is :

              <context:component-scan base-package="com.coaching.controller" />

<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views 
    directory -->
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">



    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

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

My favicon.ico is in the root of webapp (aka the one up from WEB-INF) directory (Add favicon from Spring MVC) . But it is not appearing in web address bar. I am rendering it as

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

In head of the jsp page. I have even tried https://stackoverflow.com/a/17039121/2116229 but the application was not running again.

I have tried several locations of the icon but no result. It must be requiring some spring mvc configuration. Can someone tell me what I am missing.

I hope favicon appears also in localhost too.

Was it helpful?

Solution

The mapping of your dispatcher servlet is the problem. It only reacts to *.html everything else is ignored.

<servlet-mapping>
  <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  <url-pattern>*.html</url-pattern>
</servlet-mapping>

This basically makes your <mvc:resources */> and <mvc:default-servlet-handler /> unused. You should map the servlet to / to have everything pass through the DispatcherServlet.

<servlet-mapping>
  <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

Next to that you have a mismatch in your servlet element and servlet-mapping. The names should match. So I guess the name should be 'Coaching' instead of 'Spring MVC Dispatcher Servlet'

<servlet-mapping>
  <servlet-name>Coaching</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top