JorgeGRC
JorgeGRC

Reputation: 1062

Referencing a js maven dependency from a JSP

I don't know if this a dumb question but as I'm new to Maven I don't really know how to deal with this:

I'm currently working on a webapp (struts2) project using Maven project structure, and I've just added a new dependency I want to work with: jqgrid-4.6.0 (Jquery grid-view plugin).

I added the dependency to my pom.xml just fine and now problems come when I have to include the js files inside that dependency into a JSP. If I had done it the old way (without using Maven dependencies) I would put all the js files comming inside the jar into the Project_root/src/main/webapp/js directory in my project and reference them like this:

<script src="../js/some_file_name.js" type="text/javascript"></script>

But I'm not sure how to deal with this by using the dependency instead of putting the extracted .jar inside my js directory..

I hope you understand what I'm trying to do.

Upvotes: 3

Views: 5042

Answers (2)

Jeroen
Jeroen

Reputation: 3146

You might want to consider using webjars (maybe you already do so). It allows you to add any type of Javascript library as a dependency within your Maven project.

In your case you can just add the following dependency to your maven pom.xml file:

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jqgrid</artifactId>
    <version>4.6.0</version>
</dependency>

Now I'm not sure how this would work in a struts 2 application, but in a normal servlet 3 compliant server you could reference the file by using:

<script src="webjars/jqgrid/4.6.0/jquery.jqGrid.js" type="text/javascript"/>

It might be that you need to put the applications context path in front of the relative URL.

The URL should then be something like:

<script src="${pageContext.request.contextPath}/webjars/jqgrid/4.6.0/jquery.jqGrid.js" type="text/javascript"/>

See the webjars documentation for more information.

Upvotes: 7

loshad vtapkah
loshad vtapkah

Reputation: 459

Why do you want to put them as a dependency? Are js file is not the part of you project?

Usually non-java (eq non jar/war/ear etc) packages are added as a part of a project. I've seen some packages of frameworks like jQuery for maven, but this is not a typical. You may build your own war for jQuery, but it would be a dummy separate war with its own life independent from yours in terms of deployment.

Upvotes: -1

Related Questions