简体   繁体   中英

Can we refer to a JavaScript inside the jar file of a Maven dependency?

I am developing a Spring MVC web application with Apache Tiles. I downloaded some JavaScript libraries such as JQuery using pom.xml dependency entries. My question is can I refer to the scripts inside the jar in my tiles configuration? If yes, how do I add it? If I give the entire path to the js file in the jar and will it be resolved?

<tiles-definitions>  
    <definition name="base.definition"  
                template="/WEB-INF/jsp/layout.jsp">  
        <put-attribute name="title" value="" />  
        <put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />  
        <put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" />  
        <put-attribute name="body" value="" />  
        <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />
        <put-list-attribute name="javascripts">
            <add-attribute value="<CAN THIS BE A LOCATION TO JAR??>" />
        </put-list-attribute>
    <definition>
    <!--More stuff-->
</tiles-definitions>

Example scenario: I downloaded datatables-1.9.4-2.jar and the maven build put's it under \\WEB-INF\\lib . Within the jar the javascript I want is \\META-INF\\resources\\webjars\\datatables\\1.9.4\\media\\js\\jquery.dataTables.min.js In this situation what would be my resource mapping and how do I refer to it in add-attribute element?

You should be able to do this by defining a resource mapping in Spring MVC:

<mvc:resources mapping="/js/**" location="classpath:/scripts"/>

And then access it as follows:

<add-attribute value="/js/myscript.js"/>

Also, since you're using webjars, I've quoted a relevant part of the Spring Framework Reference Documentation, 22.16.9 Serving of Resources :

Webjars are also supported with WebJarsResourceResolver , which is automatically registered when the "org.webjars:webjars-locator" library is on classpath. This resolver allows the resource chain to resolve version agnostic libraries from HTTP GET requests "GET /jquery/jquery.min.js" will return resource "/jquery/1.2.0/jquery.min.js" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM