【问题标题】:Spring boot webjars: unable to load javascript library through webjarSpring boot webjars:无法通过webjar加载javascript库
【发布时间】:2015-12-17 14:21:25
【问题描述】:

我有一个 Spring Boot(我使用 Thymeleaf 进行模板)项目,我想在其中使用一些 jQuery 库。

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

这是我的 HTML 页面的代码 sn-p:

<!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>

我已经在 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>

但是在调用页面时,我在 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)

【问题讨论】:

    标签: java spring-boot thymeleaf webjars


    【解决方案1】:

    确保您在添加依赖项时更新了 bootstrap 或 jquery 的版本,您应该使用正确版本的 bootstrap 和 jquery 更新 jsp 或 html 中的 URL。

    【讨论】:

      【解决方案2】:

      在检查了 jquery 的 webjars 之后,我通过添加“POM.XML 文件中使用的 JQUERY 库的版本”来完成这项工作

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

      (在我的例子中,我使用的是 3.1.0 版本,只使用了你正在使用的那个版本)。

      【讨论】:

        【解决方案3】:

        如果您使用 servlet 3.x,只需添加:

        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- 或 xml 配置:

        <mvc:resources mapping="/webjars/**" location="/webjars/"> 
          <mvc:resource-chain resource-cache="false" /> 
        </mvc:resources>
        

        【讨论】:

          【解决方案4】:

          在一个博客上找到了其他答案:

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

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

          <dependency>
              <groupId>org.webjars</groupId>
              <artifactId>webjars-locator</artifactId>
              <version>0.30</version>
          </dependency>
          

          在这种情况下,我们可以不使用 版本; (...)

          【讨论】:

            【解决方案5】:

            我最终执行了 mvn clean install(从 cmd 提示符)来清理目标并正确填充所有 lib/jar。我正在使用带有 Intelij 的 Spring Boot。

            【讨论】:

              【解决方案6】:

              您正确地引用了 jquery 库。也许您缺少资源处理程序配置。

              <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
              

              或者如果你使用 JavaConfig

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

              Webjars documentation

              如果这不起作用,请检查您的类路径中是否有 webjars(在 7Zip 中打开您的应用程序 JAR 并检查其中是否有 webjars 资源。)

              【讨论】:

              • 我遇到了同样的问题,添加
              • @EnableWebMvc 不应用于 Spring Boot 应用程序。如果您使用 Swagger UI,可能会导致问题。来源 -> springfox.github.io/springfox/docs/current(第 5.5 节)
              • 是的,spring boot 应用中不需要。通常你会为 Spring MVC 应用程序添加 @EnableWebMvc,但是当 Spring Boot 在类路径上看到 spring-webmvc 时会自动添加它。这会将应用程序标记为 Web 应用程序并激活关键行为,例如设置 DispatcherServlet。
              • 在将 webapp 作为 spring-boot jar 启动时不需要此资源映射,这有点奇怪。只有在部署到应用服务器的 .war 文件中运行它时才需要它。
              • @mindhaq 您能否引用 Spring Boot 文档,该文档指出在作为 Spring Boot JAR 启动时不需要此资源映射,并且仅在部署到外部服务器时才需要它?除了您的评论之外,我找不到任何提及此内容的内容。
              【解决方案7】:

              在检查了 jquery 的 webjar 之后,我通过添加一个“dist”子路径来实现这一点。

              <script src="webjars/jquery/2.1.4/dist/jquery.min.js" type="text/javascript"></script>
              

              【讨论】:

                【解决方案8】:

                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" />
                

                【讨论】:

                猜你喜欢
                • 2014-11-13
                • 1970-01-01
                • 2018-12-15
                • 2022-08-13
                • 2019-04-26
                • 2019-10-30
                • 2018-08-15
                • 2018-10-12
                • 1970-01-01
                相关资源
                最近更新 更多