Reputation: 1139
I am trying to add a favicon.ico using spring mvc 4 and apache tomcat 7 (in firefox). I examined many solutions but no one seems to work.
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">
(I also tried without MYPROJECT and other variations...).
I placed favicon.ico file right under webapps (tried in other folders as well).
When loading the page, no icon displayed.
Note: I tried to load directly the icon http://localhost:8080/MYPROJECT/favicon.ico
but received the following error message: the image 'http://localhost:8080/MYPROJECT/favicon.ico' cannot be displayed because it contains errors
. I downloaded other favicon.ico but the icon looks corrupted.
When inspecting elements in firefox I don't ant call to the favicon.ico.
Any suggestion?
Update: in my eclipse console I see:
Looking up handler method for path / 15:48:05.622 [http-bio-8080-exec-6] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/] 15:48:05.622 [http-bio-8080-exec-6] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path / 15:48:05.623 [http-bio-8080-exec-6] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/] 15:48:05.623 [http-bio-8080-exec-6] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Mapping [/] to HandlerExecutionChain with handler [org.springframework.web.servlet.mvc.ParameterizableViewController@380baa3a] and 1 interceptor
Update2 mvc config 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>
The included 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>
update 3 I placed the file under resources/images and it seems to work PARTIALLY (without shortcut) only on chrome but not on firefox.
Thanks, Mike
Upvotes: 5
Views: 9268
Reputation: 697
You need to add below tag in your web application dispatcher servlet i.e.
springmvc-dispatcher-servlet.xml:
Specifying the Resource location to load JS, CSS, Images etc
<mvc:resources mapping="/resources/**" location="/resources/"
cache-period="45556999"/>
Your web application structure should be like below
Your web application WebContent folder structure should be like below
WebContent
+resources
-->images
-->favicon.ico
Put these line in your web application jsp pages in-between <header></header>
tag like below
<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>
Upvotes: 1
Reputation: 880
Tested in FireFox and Chrome: I had the same problem. Here is how to solve it. I've done what 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/" />
(No need for <mvc:default-servlet-handler />
in my case)
genericpage.tag (I use templates, but this can be your jsp file):
<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">
NB: Note that you have to use the <c:rul value=
bit in the href else it will not work.
Project structure:
Web pages
+--META-INF
+--WEB-INF
+--resources
+--mytheme
+--css
+--images
+--favicon.ico
For some reason it doesn't work when it is just in /resources/mytheme/
Upvotes: 3
Reputation: 1661
store favicon.ico - one up from WEB-INF directory.
<link href="/favicon.ico" rel="icon" type="image/x-icon">
Upvotes: 1