1

I'm using Spark as framework to build a Java web server and rely on Bootstrap and jQuery for frontend. I'm using Webjars to bring dependencies in my pom.xml.

The problem omes with importing static files. While importing Bootstrap's CSS and JS files works well, it doesn't for jQuery JS file. I import the static files using staticFileLocation("/META-INF/resources"); and HTML header contains:

<link rel="stylesheet" href="webjars/bootstrap/3.3.6/css/bootstrap.min.css">
<script type="text/javascript" src="webjars/jquery/2.2.1/jquery.min.js"></script>
<script type="text/javascript" src="webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script>

webjars/bootstrap/3.3.6/css/bootstrap.min.css and webjars/bootstrap/3.3.6/js/bootstrap.min.js routes work well but webjars/jquery/2.2.1/jquery.min.js answers 404.

Somehow, it looks like jQuery JS file isn't present in classpath. Any clue what I'm missing ?

Edit:

The pom.xml dependencies are the following:

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>3.3.6</version>
</dependency>

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>2.2.1</version>
</dependency>

Edit 2:

I also call staticFileLocation() two times in my controller:

staticFileLocation("/static");
staticFileLocation("/META-INF/resources");

The two calls seem to conflict with each other.

Augier
  • 352
  • 4
  • 14
  • Not sure, but I think apache-spark is the wrong tag - if by "Spark" you're referring to http://sparkjava.com/, then remove apache-spark tag, which refers to a different project (http://spark.apache.org/) – Tzach Zohar Mar 18 '16 at 12:58
  • Hmm... Spark tag is automatically turned into apache-spark. Thx for highlight. – Augier Mar 18 '16 at 13:05
  • What does your jquery dependency in the `pom.xml` look like? – James Ward Mar 18 '16 at 16:32

3 Answers3

1

The jquery version 2.2.1 dependency is probably being overridden via a transitive dependency on jquery in bootstrap. You can either set an exclusion in the bootstrap dependency:

<dependency>
  <groupId>org.webjars</groupId>
  <artifactId>bootstrap</artifactId>
  <version>3.3.6</version>
  <exclusions>
    <exclusion>
      <groupId>org.webjars</groupId>
      <artifactId>jquery</artifactId>
    </exclusion>
  </exclusions> 
</dependency>

Or you can remove the explicit dependency on jquery and use the transitive one which is 1.11.1 per the bootstrap pom.xml.

James Ward
  • 29,283
  • 9
  • 49
  • 85
  • None of your solutions do work. The problem remains: jquery.min.js is not linked. – Augier Mar 21 '16 at 18:05
  • You need to find out what version of the jquery dependency is being pulled into the classpath. You can do that via a variety of ways like with https://maven.apache.org/plugins/maven-dependency-plugin/ – James Ward Mar 21 '16 at 18:21
1

I've found that if you run Spark from an IDE which is used mvn exec:exec to run project then you get only bootstrap in /META-INF/resources/webjars.

But if you make a jar-with-dependencies first and then run it with java -jar <your-project-name-and-version>-jar-with-dependencies.jar you'll get whole set of resources (both bootstrap and jquery in your case).

Actually you can easily check available resources with this code:

get("/list", (req, res) -> {
    Resource resource = Resource.newClassPathResource("/META-INF/resources");
    return resource.getListHTML("/", true);
});

Accessing /list you will get whole list of available resources.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Sergey Irisov
  • 468
  • 4
  • 7
0

Diving into Spark's source code, I found that one can refer to only one static folder location. Hence, only one webjar can be used at the same time and it will make Spark to ignore any CSS or JS file in any other location.

Augier
  • 352
  • 4
  • 14