简体   繁体   English

我们可以在Maven依赖的jar文件中引用JavaScript吗?

[英]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. 我正在使用Apache Tiles开发Spring MVC Web应用程序。 I downloaded some JavaScript libraries such as JQuery using pom.xml dependency entries. 我使用pom.xml依赖项条目下载了一些JavaScript库,例如JQuery My question is can I refer to the scripts inside the jar in my tiles configuration? 我的问题是我可以在我的tile配置中引用jar中的脚本吗? 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? 如果我将jarjs文件的完整路径给出,是否可以解决?

<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 . 示例场景:我下载了datatables-1.9.4-2.jar并将Maven构建放在\\ 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? 在罐子中,我想要的javascript是\\ META-INF \\ resources \\ webjars \\ datatables \\ 1.9.4 \\ media \\ js \\ jquery.dataTables.min.js在这种情况下,什么是我的资源映射以及如何引用它在add-attribute元素中?

You should be able to do this by defining a resource mapping in Spring MVC: 您应该能够通过在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 : 另外,由于您使用的是webjar,因此我引用了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. WebJarsResourceResolver也支持WebJarsResourceResolver ,当"org.webjars:webjars-locator"库位于类路径上时, "org.webjars:webjars-locator"将自动注册。 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" . 此解析器允许资源链从HTTP GET请求解析版本不可知库"GET /jquery/jquery.min.js"将返回资源"/jquery/1.2.0/jquery.min.js"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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