Spring boot webjars: unable to load javascript library through webjar

后端 未结 7 1269
失恋的感觉
失恋的感觉 2020-12-30 08:37

I have a spring boot (I use Thymeleaf for templating) project where I want to use some jQuery libraries.

Unfortunately, the webjars aren\'t loading at all. I have tr

相关标签:
7条回答
  • 2020-12-30 08:45

    You are referencing jquery library correctly. Maybe you are missing resource handler configuration.

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

    Or if you use JavaConfig

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

    Webjars documentation

    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.)

    0 讨论(0)
  • 2020-12-30 08:57

    The webjars dependencies should be available on the spring boot classpath, so you should try referencing the webjars using the src attribute like so:

    <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" />
    
    0 讨论(0)
  • 2020-12-30 08:59

    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.

    0 讨论(0)
  • 2020-12-30 09:02

    After inspecting the webjar for jquery, I got this working by adding a "dist" subpath.

    <script src="webjars/jquery/2.1.4/dist/jquery.min.js" type="text/javascript"></script>
    
    0 讨论(0)
  • 2020-12-30 09:03

    After inspecting the webjars for jquery, I got this working by adding a "THE VERSION OF JQUERY LIBRARY USED IN POM.XML FILE"

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

    (in my case I used 3.1.0 version, used that version only that you are using).

    0 讨论(0)
  • 2020-12-30 09:05

    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; (...)

    0 讨论(0)
提交回复
热议问题