Java springboot favicon.ico icons do not appear inquiry

Recently encountered this problem in the project, not how attention before, some own inquiry to view previously written demo, and some even displayed some do not show, then view previous projects, old projects are no problem, why, deep .

 

First, naturally, it is Baidu, see the argument are two kinds,

First, the program ico named favicon.ico, put under static, automatic display.

Second embodiment spring.mvc.favicon.enabled = false, then

Add the following in html file

<link rel="icon" th:href="/favicon.ico" type="image/x-icon">

<link rel="bookmark" th:href="/favicon.ico" type="image/x-icon">

A test is invalid, and second, you can, but there are problems, there is no reference to the master page with them too strenuous. Remember when using iis as long as the ico named favicon.ico, in the root directory can be, so that this request should be made of the initiative to the browser, page references should not only continue to explore.

 

Baidu + View springboot source code, has found WebMvcAutoConfiguration processing favicon, and understand a program into the upper Why is automatically displayed under static, it will automatically match the default path four static resources

classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
Plus a "/" root 
locations.add (new ClassPathResource ( "/" ));

So into the nature of these directories are the same

 

 But did not resolve the issue, continue to study the source code, no solution.

Look back and think, mechanisms of favicon.ico, and did not think to render ico By default view rendering, but the browser that initiated the request alone, then the problem should not be the code, but certainly not a browser issue, Why browser sometimes requests, sometimes do not request it, first of all think of the cache, but normal asynchronous request even if the cache will request normal, but tips from the cache, ico always thought it should be so, but the browser also checked disable caching. Check the information, and seeing a statement, Google there will no longer request, while Firefox is approximately two minutes request once, you can clear the cache or ctrl + F5 to force a refresh request, then ctrl + F5 test, and sure enough Google to re-request the favicon .ico, reported 404 viewing path, http: //localhost/favicon.ico, Huang Wu this moment, or the problem of the mechanism, the default browser requests http: // domain + favicon.ico, but the project is often accompanied by project name, which is the actual path http: // domain + project name + favicon.ico, wrong path, not a natural request, since Google's caching mechanism, request one did not request that the back is not requested, and naturally there will be no.

 

 

 

 

solution:

Since this is a strategy browser issue, so there is no direct solution,

方案一:不配项目名,http://域名(或者IP+端口)/favicon.ico能访问到图片,就没有问题

方案二:页面明确指定ico

<link rel="icon" th:href="/favicon.ico" type="image/x-icon">

<link rel="bookmark" th:href="/favicon.ico" type="image/x-icon">

但是这样比较费劲,所有页面都要加,折中方案就是用母版页,统一添加,已有项目的话改动也比较大,

折中方案二:通过拦截器,在页面渲染完成后追加一段js,

可以继承HandlerInterceptor接口,重写afterCompletion方法,添加以下代码通过js写入

String link = "<script>" +
                            "var link = document.createElement('link');" +
                            "link.type = 'image/x-icon';" +
                            "link.rel = 'shortcut icon';" +
                            "link.href = '/nascan/images/favicon.ico';" +
                            "document.getElementsByTagName('head')[0].appendChild(link);" +
                            "</script>";

                    response.getWriter().append(link);

注:有空继续研究,看有没有其他方法,看了一些网站,全都是通过link指定的,默认的方式确实问题多多,还是指名道姓的好。

Guess you like

Origin www.cnblogs.com/jusha/p/11979734.html