Spring boot webjars:无法通过webjar加载javascript库

Mas*_*ind 9 java thymeleaf webjars spring-boot

我有一个spring boot(我使用Thymeleaf进行模板化)项目,我想使用一些jQuery库.

不幸的是,webjars根本没有加载.我尝试了很多配置,但都失败了.

这是我的HTML页面的代码片段:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">

<title>JAC</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

<script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.js"
        th:src="@{/webjars/jquery/2.1.4/jquery.min.js}" type="text/javascript"></script>
<script src="http://cdn.jsdelivr.net/webjars/jquery-file-upload/9.10.1/jquery.fileupload.js"  type="text/javascript"
        th:src="@{/webjars/jquery-file-upload/9.10.1/jquery.fileupload.min.js}"></script>
<link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.5/css/bootstrap.min.css"
      th:href="@{/webjars/bootstrap/3.3.5/css/bootstrap.min.css}"
      rel="stylesheet" media="screen" />
<link href="http://cdn.jsdelivr.net/webjars/jquery-file-upload/9.10.1/jquery.fileupload.css"
      rel="stylesheet" media="screen" />
</head>
Run Code Online (Sandbox Code Playgroud)

我已将它们添加到pom文件中:

<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>jquery</artifactId>
    <version>2.1.4</version>
</dependency>
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>3.3.5</version>
</dependency>

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery-file-upload</artifactId>
    <version>9.10.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

但是在调用页面时,我在jquery.min.js和jquery.fileupload.min.js上获得了404.

GET http://localhost:8888/webjars/jquery-file-upload/9.10.1/jquery.fileupload.min.js 
2015-09-21 02:02:04.059 home:9 
GET http://localhost:8888/webjars/jquery/2.1.4/jquery.min.js 404 (Not Found)
Run Code Online (Sandbox Code Playgroud)

Gon*_*ndy 23

您正在引用jquery库.也许您缺少资源处理程序配置.

<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
Run Code Online (Sandbox Code Playgroud)

或者如果您使用JavaConfig

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  }

}
Run Code Online (Sandbox Code Playgroud)

Webjars文档

如果这不起作用,请检查你是否在classpath上有webjars(在7Zip中打开你的应用程序JAR并检查webjars资源是否在其中.)

  • @EnableWebMvc不应用于Spring Boot应用程序。如果使用Swagger UI,可能会导致问题。来源-&gt; https://springfox.github.io/springfox/docs/current/(第5.5节) (2认同)
  • @mindhaq你能不能引用一下Spring Boot文档,它说明你在作为Spring Boot JAR启动时不需要这个资源映射,只有在部署到外部服务器时才需要它?除了你的评论之外,我还没有找到任何提及. (2认同)

小智 5

检查 jquery 的 webjar 后,我通过添加“dist”子路径来实现此功能。

<script src="webjars/jquery/2.1.4/dist/jquery.min.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)


Tom*_*ski 5

在一个博客上找到的附加答案:

\n\n
\n

使用 Spring Framework 4.2 或更高版本时,它将自动检测类路径上的 webjars-locator 库,并使用它自动解析任何 WebJars 资产的版本。

\n\n

为了启用此功能,我们\xe2\x80\x99 将添加 webjars-locator 库\n 作为应用程序的依赖项:

\n\n
<dependency>\n    <groupId>org.webjars</groupId>\n    <artifactId>webjars-locator</artifactId>\n    <version>0.30</version>\n</dependency>\n
Run Code Online (Sandbox Code Playgroud)\n\n

在这种情况下,我们可以引用WebJars资源而不使用\n版本;(...)

\n
\n