如何将JavaScript库包含到Spring MVC项目中(例如,jQuery或Dojo) [英] How to include JavaScript library into Spring MVC project (jQuery or Dojo, for instance)

查看:107
本文介绍了如何将JavaScript库包含到Spring MVC项目中(例如,jQuery或Dojo)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里阅读了超过5个相关线程,但无法回答:逐步说明。到目前为止,我有STS Spring MVC模板结构,并尝试在我的项目中的某个地方放置 jquery.js (不幸的是没人说,应该在哪里)。所以,请说:


  1. 将jquery.js放在项目结构中的哪里?

  2. 来自.jsp?的这个位置?

  3. 需要其他任何操作?像maven,应用配置更改?

我的jsp与 http://code.jquery.com完美结合/jquery-1.8.3.js ,但拒绝使用本地文件 /js/jquery-1.8.3.js 。还有一个奇怪的事情 - 当jsp找不到脚本时,应用服务器对它的投诉,但是当它发现(找到?)库时,没有警告,但jquery也不行。

解决方案

JavaScript文件只是必须由浏览器下载的资源。所以你把它们放在你想要的webapp根目录下。



假设您将文件放在 /js/jquery.js (在Web项目的WebContent目录中) 。假设您的webapp具有 / myFirstWebApp 作为上下文路径。这意味着一旦应用程序部署,webapp的根将在

  http:// localhost:8080 / myFirstWebApp / 

并且您的JS文件将因此在

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

要在webapp中生成URL,您通常使用< c:url> 标签:

 < script src =< c:url value ='/ js / jquery.js'/>>< / script> 
c:url标签照顾上下文路径:它将它添加到你给它的绝对URL中,以便halp你稍后改变上下文路径。


I have read more than 5 related threads here, but could not the answer: step-by step instructions. 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). So, please say:

  1. where to place jquery.js in the project structure?
  2. how to refer to this location from .jsp?
  3. any other actions needed? like maven, app config changes?

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. 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.

解决方案

The JavaScript files are just resources that must be downloaded by the browser. So you put them where you want under the webapp root directory.

Suppose you put your file under /js/jquery.js (in the WebContent directory of your web project). And suppose your webapp has /myFirstWebApp as context path. This means that the root of the webapp, once the application is deployed, will be at

http://localhost:8080/myFirstWebApp/

and that your JS file will thus be at

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

To generate a URL in a webapp, you typically use the <c:url> tag:

<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.

这篇关于如何将JavaScript库包含到Spring MVC项目中(例如,jQuery或Dojo)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆