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

bihw5rsg  于 5个月前  发布在  Spring
关注(0)|答案(2)|浏览(36)

我尝试使用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

<dependencies>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>3.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery-ui</artifactId>
        <version>1.12.1</version>
    </dependency>   
    <!-- <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>webjars-locator</artifactId>
        <version>0.30</version>
    </dependency> -->
</dependencies>

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

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


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

fjaof16o

fjaof16o1#

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

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
          .addResourceHandler("/webjars/**")
          .addResourceLocations("/webjars/");
    }
}

字符串
或XML

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


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

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

hkmswyz6

hkmswyz62#

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

<dependency>
        <groupId>org.webjars</groupId>
        <artifactId>webjars-locator-core</artifactId>
    </dependency>

字符串

相关问题