Spring boot webjars:无法通过webjar加载javascript库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32685819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot webjars: unable to load javascript library through webjar
提问by Master Mind
I have a spring boot (I use Thymeleaf for templating) project where I want to use some jQuery libraries.
我有一个 Spring Boot(我使用 Thymeleaf 进行模板化)项目,我想在其中使用一些 jQuery 库。
Unfortunately, the webjars aren't loading at all. I have tried many configuration but all of them failed.
不幸的是,webjars 根本没有加载。我尝试了很多配置,但都失败了。
Here is the code snippet of my HTML page:
这是我的 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>
I have added them in the pom file:
我已将它们添加到 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>
But when calling the page I got a 404 on jquery.min.js and jquery.fileupload.min.js.
但是在调用页面时,我在 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)
采纳答案by Gondy
You are referencing jquery library correctly. Maybe you are missing resource handler configuration.
您正在正确引用 jquery 库。也许您缺少资源处理程序配置。
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
Or if you use JavaConfig
或者,如果您使用 JavaConfig
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
If this will not work, please check if you have webjars on classpath (open your application JAR in 7Zip and check if webjars resources are inside it.)
如果这不起作用,请检查您的类路径上是否有 webjars(在 7Zip 中打开您的应用程序 JAR 并检查其中是否有 webjars 资源。)
回答by marminto
The webjars dependencies should be available on the spring boot classpath, so you should try referencing the webjars using the src attribute like so:
webjars 依赖项应该在 spring boot 类路径上可用,因此您应该尝试使用 src 属性引用 webjars,如下所示:
<script src="webjars/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
<script src="webjars/jquery-file-upload/9.10.1/jquery.fileupload.min.js"></script>
<link href="webjars/bootstrap/3.3.5/css/bootstrap.min.css"
rel="stylesheet" media="screen" />
<link href="webjars/jquery-file-upload/9.10.1/jquery.fileupload.css"
rel="stylesheet" media="screen" />
回答by Markus Schaffner
After inspecting the webjar for jquery, I got this working by adding a "dist" subpath.
在检查了 jquery 的 webjar 之后,我通过添加一个“dist”子路径来让它工作。
<script src="webjars/jquery/2.1.4/dist/jquery.min.js" type="text/javascript"></script>
回答by Ajay Menon
I ended up doing a mvn clean install (from cmd prompt) to get the target cleaned and all the lib/jars populated correctly. I am using Spring boot with Intelij.
我最终做了一个 mvn clean install(从 cmd 提示)来清理目标并正确填充所有的 lib/jars。我在 Intelij 中使用 Spring Boot。
回答by Tomasz Przybylski
Additional answer found on one blog:
在一篇博客上找到的其他答案:
When using Spring Framework version 4.2 or higher, it will automatically detect the webjars-locator library on the classpath and use it to automatically resolve the version of any WebJars assets.
In order to enable this feature, we'll add the webjars-locator library as a dependency of the application:
<dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator</artifactId> <version>0.30</version> </dependency>
In this case, we can reference the WebJars assets without using the version; (...)
当使用 Spring Framework 4.2 或更高版本时,它会自动检测类路径上的 webjars-locator 库并使用它来自动解析任何 WebJars 资产的版本。
为了启用此功能,我们将添加 webjars-locator 库作为应用程序的依赖项:
<dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator</artifactId> <version>0.30</version> </dependency>
在这种情况下,我们可以在不使用版本的情况下引用 WebJars 资产;(……)
回答by Jacob
if you use servlet 3.x just add :
如果您使用 servlet 3.x,只需添加:
1- using java config :
1-使用java配置:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/").resourceChain(false);
registry.setOrder(1);
}
}
2- or xml config :
2- 或 xml 配置:
<mvc:resources mapping="/webjars/**" location="/webjars/">
<mvc:resource-chain resource-cache="false" />
</mvc:resources>