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)
如果这不起作用,请检查你是否在classpath上有webjars(在7Zip中打开你的应用程序JAR并检查webjars资源是否在其中.)
小智 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)
在一个博客上找到的附加答案:
\n\n\n\n使用 Spring Framework 4.2 或更高版本时,它将自动检测类路径上的 webjars-locator 库,并使用它自动解析任何 WebJars 资产的版本。
\n\n为了启用此功能,我们\xe2\x80\x99 将添加 webjars-locator 库\n 作为应用程序的依赖项:
\n\nRun Code Online (Sandbox Code Playgroud)\n\n<dependency>\n <groupId>org.webjars</groupId>\n <artifactId>webjars-locator</artifactId>\n <version>0.30</version>\n</dependency>\n在这种情况下,我们可以引用WebJars资源而不使用\n版本;(...)
\n