无法在Spring Boot 1.3或1.2.5中设置我的自定义favicon.ico

时间:2015-07-02 19:53:26

标签: java spring spring-mvc spring-boot favicon

我在几天前首次使用 FreeMarker 并使用 Spring Boot 1.3 启动了全新的新项目。但是,我正在努力展示我自己的图标。事实上,它在项目的最初阶段运作良好但是从几天前开始,它没有,我无法找到原因。我已经通过stackoverflow上的三个线程谈论它,但没有解决我的问题。我在Google上搜索过,但我找不到任何解决方案。

如何重现

试图摆脱这个问题,我已经开始了一个新项目(这次是Spring 1.2.5),我遇到了同样的问题。 使用Spring Tool Suite:新的►SpringStarterProject►然后我勾选了Web和FreeMarker,►完成。

项目准备好后,我在demo.web包中创建了 HomeController ,其中一个测试函数返回" home"。我还在src / main / resources / templates 中创建了 home.ftl,并将两个文件放在 src / main / resources / static 中: demo .png favicon.ico (我也尝试将其放在src / main / resources下)。

demo.png正确显示但不显示favicon.ico 。也许我做错了,因为我是网络开发的新手。

  

HomeController.java

package demo.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String get() {
        return "home";
    }
}
  

home.ftl

<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
    <link rel="icon" type="image/x-icon" href="/favicon.ico">
</head>
<body>
    <img src="/demo.png" alt="">
</body>
</html>
  

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.test</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

如果您需要更多信息,请询问我。 我事先感谢你们的帮助。

祝你好运, Stilleur

修改

实际上,看起来Spring Boot会覆盖他在资源位置找到的每个favicon.ico。

2 个答案:

答案 0 :(得分:8)

请将您的favicon.ico重命名为其他ico个文件,例如mycustomfavicon.ico。在href中替换正确的名称也会有效。

答案 1 :(得分:3)

由于@ M.Deinum没有将他的评论作为答案重新发布,我会这样做。

对于Spring Boot 1.3.0.RELEASE,只需将以下行添加到application.properties spring.mvc.favicon.enabled=false

根据@Stilleur的说法,至少从Spring Boot 1.3.0.M1开始就有效。