spring 我尝试在Sping Boot 中使用webjars,但收到ERR_ABORTED 404

bihw5rsg  于 2024-01-05  发布在  Spring
关注(0)|答案(2)|浏览(137)

我尝试使用Webjars在我的spring boot project中使用jQuery
然而,jQuery不工作,我通过按F12检查它。
GET http:// localhost:8100/webjars/jquery/3.4.1/jquery.min.js net::ERR_ABORTED 404
首先,我将Webjars添加到pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.webjars</groupId>
  4. <artifactId>jquery</artifactId>
  5. <version>3.4.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.webjars</groupId>
  9. <artifactId>jquery-ui</artifactId>
  10. <version>1.12.1</version>
  11. </dependency>
  12. <!-- <dependency>
  13. <groupId>org.webjars</groupId>
  14. <artifactId>webjars-locator</artifactId>
  15. <version>0.30</version>
  16. </dependency> -->
  17. </dependencies>

字符串
html header部分,我添加了:

  1. <link rel="stylesheet" href="/webjars/jquery-ui/1.12.1/jquery-ui.min.css" />
  2. <script type="text/javascript" src="/webjars/jquery-ui/1.12.1/jquery-ui.min.js"></script>
  3. <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.min.js"></script>
  4. <script type="text/javascript">
  5. $(document).ready(function(){
  6. //alert("Hello~!?");
  7. });
  8. </script>


从上面的pom.xml代码中可以看到,我尝试使用webjars-locator,但又得到了相同的错误。
当我使用webjars-locator时,我删除了版本名并使用了它。
即使它被列在https://www.webjars.org/documentation#springboot?上,

fjaof16o

fjaof16o1#

你需要告诉spring你的js库文件在哪里。
默认情况下,当您使用webjars时,所有lib都添加到webjars文件夹中
所以你需要加上这个

  1. @Configuration
  2. @EnableWebMvc
  3. public class WebConfig implements WebMvcConfigurer {
  4. @Override
  5. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  6. registry
  7. .addResourceHandler("/webjars/**")
  8. .addResourceLocations("/webjars/");
  9. }
  10. }

字符串
或XML

  1. <mvc:resources mapping="/webjars/**" location="/webjars/"/>


webjars-locator将在classpath上找到library,并使用它自动解析任何WebJars资产的版本。
现在你可以像这样在HTML中使用它

  1. <script src="/webjars/jquery/3.1.1/jquery.min.js"></script>

展开查看全部
hkmswyz6

hkmswyz62#

4年后,使用Sping Boot starter 3.2.0
我忘了把这个依赖添加到pom.xml中:

  1. <dependency>
  2. <groupId>org.webjars</groupId>
  3. <artifactId>webjars-locator-core</artifactId>
  4. </dependency>

字符串

相关问题