Spring MVC3 + Velocity设置favicon

    xiaoxiao2025-09-10  511

    一、概述

    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。

    二、具体步骤

    1. 修改静态资源的handler mapping:

    在dispatcher-servlet.xml中加入以下配置:

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

    2. 放置favicon图标

    将favicon图标放到/resources(或其子目录)下,本文将favicon放置到如下位置:

    3. 在页面head中使用link引入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>

    4. 配置velocity的requestContextAttribute属性,使得在页面上可以访问contextPath

    要使上文中配置的${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

    转载请注明原文地址: https://ju.6miu.com/read-1302509.html
    最新回复(0)