Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of invalid js called by springboot html

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "springboot html call js invalid 400 problem". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

springboot html calls js invalid 400

html board under templates, js file under static, reference in the template does not need to add static this path.

For example src-main ├─java └─resources ├─static │ ├─css │ ├─img │ └─js │ └─test.js └─templates

To refer to test.js in a template, you should refer to the address/js/test.js, instead of thymeleaf attributes th:src="@{/js/test.js}", where absolute addresses can be replaced by relative addresses.

Unable to access css,js, images and other static resources of the three scenarios

Today, I found a problem when writing a Spring Boot project. I couldn't access static resources in folders under static directory, such as css, js and images, and I couldn't find the addresses of these static files in the website. I tried several solutions online but it didn't work. However, this problem did not occur when I used Spring Boot before.

So I looked up the previous projects, compared them, and finally found the problem:

In previous projects, I put all static files under static, so I accessed them directly. But now I have several more folders under the static folder, the structure is as follows:

The first solution

After discovering the difference, I tried putting the css file directly outside static and changing the file reference address to

I found that I could visit it normally.

The second solution

But this method is not what I want, but after discovering the problem, it will be easy to deal with it. I found another way to preserve this file system structure:

Add spring.mvc.static-path-pattern=/static to the application.properties configuration file/**

Add if your profile is an application.yml file

spring: mvc: static-path-pattern: /static/**

After adding recompile, it can be accessed in the subfolder. The format of the reference is:

The third solution

Because I am using the thymeleaf template, I can use the Thymeleaf import file directly, as follows:

This way, you can reference the file you want even if you don't configure it in the application.properties file.

reason

Why did this problem arise?

Because Spring Boot's default scan path is/static, it does not contain files in the static folder, so if you put files in the location/static/css/me.css, they will not be recognized.

The function of this code in method 1 and method 2 is to add all folders and corresponding subfolders under/static folder to the scan path, so it can be used normally.

"springboot html call js invalid 400 problem how to solve" the content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report