The ultimate perfect solution for java projects to import css or js and other resource failures (no effect)

The ultimate perfect solution for java projects to import css or js and other resource failures (no effect)

When I was writing a project yesterday, I found that the configuration of the relative path of the .css, .js and other files introduced from the outside was correct, but it didn’t work, so I finally found a perfect solution after various groping. Come share it with everyone.

1. The most important thing is that you must ensure that your externally imported files cannot be placed in the WEB-INF directory. This directory is protected, so if your externally imported files are located in this directory, please create one in WebContent Directory for storing resource files

2. Secondly, the relative path of your import must be written correctly. If this is wrong, the gods will not be able to correct your mistake. Remember to use to ${pageContext.request.contextPath}get your project name. For example, I created a storage login resource in WebContet The directory login, I want to quote the files inside, so I just write<link type="text/css" href="${pageContext.request.contextPath}/login/style.css" rel="stylesheet" />

3. If there are no problems in the above two steps, then the next step is to talk about the key solution. In fact, there are mainly two reasons, one is your site-wide encoding filter, and the other is caused by the security filter.

Let's discuss the encoding filter first. If the encoding for processing the response is written in this way in your encoding filter, congratulations.response.setContentType("text/html; charset="+encoding);

Open the Firefox browser (debugging is strongly recommended), and you can see the following prompt. You can find that the Debug resultsprompt style sheet has been obtained, but "text"/css is forcibly converted to "text"/html by your site-wide encoding filter It is not loaded, and the solution is very simple. In the whole site encoding filter, change response.setContentType("text/html; charset="+encoding);to

 response.setCharacterEncoding(encoding); 

Let’s discuss the second reason below. The second reason is very simple, that is, your css, js and other externally imported files are blocked by your website security filter filter. You only need to add the following code to the filter to release it. Canelse if (path.contains(".css") || path.contains(".js") || path.contains(".gif")) { chain.doFilter(request, response); }

After the above steps, you feel that there is no problem. Go back to refresh and find that it still doesn’t work, then you are right, because of the browser’s caching mechanism, the normal refresh of F5 may not be effective immediately, please press your ctrl+ F5 to clear the cache and refresh, hahahahaha, did you find that it has succeeded!

(ps: The first article, to commemorate that many things are not yet familiar, and similar articles will be posted for problems encountered in the future, everyone supports it - -)

Guess you like

Origin blog.csdn.net/weixin_45185577/article/details/106327880