Struts2时代,在web.xml中配置mime-mapping即可设置favicon。
<mime-mapping> <extension>ico</extension> <mime-type>image/x-icon</mime-type> </mime-mapping>Spring Boot也提供了favicon的自动配置,只需将favicon.ico放置到resources目录下即可。 而使用Spring MVC时,则需要在每个页面上手动配置。 本文介绍在使用Spring MVC3 + Velocity(JSP也适用)下,如何设置web应用的favicon。
在dispatcher-servlet.xml中加入以下配置:
<mvc:resources mapping="/resources/**" location="/resources/" />将favicon图标放到/resources(或其子目录)下,本文将favicon放置到如下位置:
使用velocity时:
<!DOCTYPE html> #html("static/common/mod.js") #head() <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="Cache-control" content="no-cache"> <title>xxx系统</title> #require("static/common/reset.css") #require("static/common/common.css") <link rel="icon" type="image/x-icon" href="${rc.contextPath}/resources/img/favicon.ico"/>使用jsp时:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Home</title> <link rel="icon" type="image/x-icon" href="<s:url value="/static/images/favicon.ico"/>" /> </head>要使上文中配置的${rc.contextPath}生效,需要在velocity的配置内容里增加如下属性:
<beans:bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> ... <beans:property name="requestContextAttribute" value="rc"/> ...http://www.deroneriksson.com/tutorials/java/spring/additional/adding-favicon-to-a-spring-application http://lianzerong.iteye.com/blog/2035659