Joh*_*din 3 favicon spring-mvc thymeleaf spring-boot
我知道这个问题已经一遍又一遍地提出,并且有几种解决方案。除了建议您为此编写自己的配置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);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用thymeleaf作为模板引擎
如果有人使用较新版本的 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;
}
}
Run Code Online (Sandbox Code Playgroud)
antMatcher通过将以下代码添加到SpringSecurityConfiguration来添加您的 favicon 资源(正如JaneXQ上面已经提到的):.antMatchers("/static/favicon.ico").permitAll()
Run Code Online (Sandbox Code Playgroud)
<head>html 页面的部分中(我在这里使用了 thymeleaf):<head>
...
<link rel="icon" type="image/ico" th:href="@{../static/favicon.ico}">
...
</head>
Run Code Online (Sandbox Code Playgroud)
我也有 SpringBoot 配置,并且正在工作
<link rel="shortcut icon" type="image/png" th:href="@{/img/favicon.png}"/>
Run Code Online (Sandbox Code Playgroud)
还有resources/public/img下的favicon.png
我通过将favicon.ico放在main / resource / static中并将此行添加到我的安全配置中解决了这个问题
httpSecurity
.authorizeRequests()
.antMatchers( "/favicon.ico").permitAll()
Run Code Online (Sandbox Code Playgroud)
好的,这似乎现在有效。当然,我在咆哮之后就设法让它工作:)。
无论如何,我所做的是。
抱歉浪费了大家的时间,但希望这对像我这样的新手有用
| 归档时间: |
|
| 查看次数: |
6730 次 |
| 最近记录: |