Referencing a js maven dependency from a JSP

dependencies, jsp, maven, struts2

Solution

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.

Problem

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.

Original source