我知道这个问题一直在这里被问到,有几个解决方案。我尝试了其中的几个,除了那些建议为此编写自己的配置bean。我不想做所有这些只是为了显示一个小小的图标,它接缝过度。但我无法让它发挥作用。这些是我到目前为止尝试过的解决方案。
<link rel="icon" type="image/png" href="favicon.png" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
这些都不奏效。
当在浏览器中检查页面时,尽管没有显示图标,但有时我没有打印出任何错误,或者我收到错误说GET http://localhost:8080/myapp/favicon.png 404 ()
它在哪里引用类型为JSON(我觉得很奇怪)。
我的想法已经没了,所以如果有人能告诉我为什么这不起作用请告诉我。我是否忘记了其中一个神奇的春天注释? 这就是我的主要课程。
@SpringBootApplication
@ComponentScan
@Configuration
@EnableWebMvc
public class JobengineMonitorApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(JobengineMonitorApplication.class, args);
}
}
我使用百里香作为模板引擎
答案 0 :(得分:3)
我通过在main / resource / static中添加favicon.ico并将此行添加到我的安全配置中来解决此问题
httpSecurity
.authorizeRequests()
.antMatchers( "/favicon.ico").permitAll()
答案 1 :(得分:2)
我也使用SpringBoot配置并正在使用
<link rel="shortcut icon" type="image/png" th:href="@{/img/favicon.png}"/>
resources / public / img
下的favicon.png答案 2 :(得分:2)
将favicon.png
放在src/main/resources/public
下,并将其准确地添加到*.html
页的header
部分
<link rel="shortcut icon" type="image/png" th:href="@{favicon.png}"/>
答案 3 :(得分:2)
如果有人在使用较新版本的 Spring(在我的情况下为 spring boot 2.4.4)时遇到同样的问题,以下是适合我的场景:
/resources/static
文件夹中。我还尝试将它放在 /resourses/
文件夹中,效果也很好,所以不用太担心文件夹。FaviconConfiguration
:@Configuration
public class FaviconConfiguration {
@Bean
public SimpleUrlHandlerMapping customFaviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Integer.MIN_VALUE);
mapping.setUrlMap(Collections.singletonMap(
"/static/favicon.ico", faviconRequestHandler()));
return mapping;
}
@Bean
protected ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler
= new ResourceHttpRequestHandler();
requestHandler.setLocations(Collections.singletonList(new ClassPathResource("/")));
return requestHandler;
}
}
antMatcher
(如上面已经提到的 JaneXQ) :.antMatchers("/static/favicon.ico").permitAll()
<head>
部分(我在这里使用了百里香叶):<head>
...
<link rel="icon" type="image/ico" th:href="@{../static/favicon.ico}">
...
</head>
答案 4 :(得分:1)
好的,这似乎现在正在运作。当然,我在咆哮之后设法让它工作了:)。
无论如何,我做的是。
很抱歉浪费了人们的时间,但希望这对我这样的新秀来说很有用
答案 5 :(得分:0)
我发现我必须将favicon.ico
文件放入:
src/main/resources/public
答案 6 :(得分:0)
出于某种原因,.ico 格式不起作用。我只是放置了一个 png 图像,而 spring 会自动选择该图标。
我把 png 图片放在 \src\main\resources\public
Spring Boot + thymeleaf
答案 7 :(得分:0)
尝试用 href 替换 th:href。它对我有用。
<link rel="icon" href="/favicon.ico" type="image/ico">