简体   繁体   English

如何将JavaScript库包含到Spring MVC项目中(例如jQuery或Dojo)

[英]How to include JavaScript library into Spring MVC project (jQuery or Dojo, for instance)

I have read more than 5 related threads here, but could not the answer: step-by step instructions. 我在这里阅读了超过5个相关的线程,但无法得到答案:逐步说明。 Till now I have the STS Spring MVC template structure and try to put jquery.js somewhere in my project (unfortunately, nobody says, where it should be). 直到现在我有STS Spring MVC模板结构并尝试将jquery.js放在我的项目中的某个地方(不幸的是,没有人说,它应该在哪里)。 So, please say: 所以,请说:

  1. where to place jquery.js in the project structure? 在项目结构中放置jquery.js的位置?
  2. how to refer to this location from .jsp? 如何从.jsp引用此位置?
  3. any other actions needed? 还需要其他任何行动? like maven, app config changes? 像maven一样,app config改变了吗?

My jsp works perfectly with http://code.jquery.com/jquery-1.8.3.js , but refuses to work with local file /js/jquery-1.8.3.js . 我的jsp与http://code.jquery.com/jquery-1.8.3.js完美配合,但拒绝使用本地文件/js/jquery-1.8.3.js And a strange thing - when jsp can not find the script, app server complaints about it, but when it (have found?) the library, no warnings, but jquery also doesn't work. 还有一件奇怪的事情 - 当jsp无法找到脚本时,app服务器会抱怨它,但是当它(已找到?)库时,没有警告,但jquery也无效。

The JavaScript files are just resources that must be downloaded by the browser. JavaScript文件只是浏览器必须下载的资源。 So you put them where you want under the webapp root directory. 因此,您可以将它们放在webapp根目录下的所需位置。

Suppose you put your file under /js/jquery.js (in the WebContent directory of your web project). 假设您将文件放在/js/jquery.js下(在Web项目的WebContent目录中)。 And suppose your webapp has /myFirstWebApp as context path. 假设您的webapp具有/myFirstWebApp作为上下文路径。 This means that the root of the webapp, once the application is deployed, will be at 这意味着一旦部署了应用程序,webapp的根目录就是

http://localhost:8080/myFirstWebApp/

and that your JS file will thus be at 并且你的JS文件将因此而存在

http://localhost:8080/myFirstWebApp/js/jquery.js

To generate a URL in a webapp, you typically use the <c:url> tag: 要在webapp中生成URL,通常使用<c:url>标记:

<script src="<c:url value='/js/jquery.js'/>></script>

The c:url tag takes care of the context path:it prepends it to the absolute URLs you give it in order to halp you change the context path later. c:url标记负责处理上下文路径:它将它预先设置为您提供的绝对URL,以便稍后更改上下文路径。

for includes, it's a better to write 对于包含,写起来更好

<script type="text/javascript" src="<c:url value='/js/jquery-1.8.3.min.js'/>"></script>

The js folder should be placed under WebContent folder. js文件夹应放在WebContent文件夹下。

  1. Copy the JQuery library under WebContent (like, WebContent\\jquery\\js\\jquery-1.9.1.js is valid) 在WebContent下复制JQuery库(比如,WebContent \\ jquery \\ js \\ jquery-1.9.1.js有效)

  2. In JSP: 在JSP中:

      <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> ... <head> <spring:url value="/jquery/js/jquery-1.9.1.js" var="jqueryUrl" /> <script src="${jqueryUrl}"></script> <spring:url value="/jquery/js/jquery-ui-1.10.3.custom.js" var="jqueryJsUrl" /> <script src="${jqueryJsUrl}"></script> </head> 
  3. In spring-context.xml add: 在spring-context.xml中添加:

      <mvc:resources location="/jquery/" mapping="/jquery/**" /></beans> 

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

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