将外部 JavaScript 库添加到 Eclipse 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13442547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding external JavaScript library to Eclipse Project?
提问by JB Nizet
When adding an external JavaScript library to an Eclipse project should I be adding the .js file (d3.v2.js), or the tarballed master pulled from Github?
将外部 JavaScript 库添加到 Eclipse 项目时,我应该添加 .js 文件 (d3.v2.js) 还是从 Github 中提取的压缩母版?
Google tells me to Google it. Subsequent Googles remind me of the need to Google it.
谷歌告诉我谷歌它。随后的谷歌提醒我需要谷歌它。
Directory tree:
目录树:
.
|-build
|---classes
|-src
|-WebContent
|---foo.html
|---META-INF
|---WEB-INF
|-----lib
|---d3
|-----d3.v2.js
Relative path from foo.html to d3.v2.js should be: "../d3/d3.v2.js"
从 foo.html 到 d3.v2.js 的相对路径应该是:“../d3/d3.v2.js”
- But, maybe it's not. I don't know.
- Whenever I reference d3.v2.js from foo.html nothing happens. By nothing happens, I mean nothing visually appears in the browser which suggests that d3.js even exists.
- 但是,也许不是。我不知道。
- 每当我从 foo.html 引用 d3.v2.js 时,什么也没有发生。没有发生任何事情,我的意思是浏览器中没有任何视觉显示表明 d3.js 甚至存在。
To 'add' d3.v2.js to Eclipse I took the following steps:
要将 d3.v2.js“添加”到 Eclipse,我采取了以下步骤:
Foo -> New -> JavaScript Source File -> Advanced -> Link to File in Filesystem -> /home/tyler/workspace/foo/d3/d3.v2.js
Tried 5 different paths. None of them worked (do you have to use relative, absolute?)
- src="/home/tyler/workspace/Foo/d3/d3.v2.js"
- src="../d3/d3.v2.js"
- src="/Foo/d3/d3.v2.js"
- src="d3.v2.js"
So, I deleted the reference to d3.v2.js in Eclipse and attempted to add as a library.
Foo -> JavaScript Resources -> Add JavaScript Library -> User Library - Configure User Libraries -> New -> "D3" -> Add .js file -> d3.v2.js (location: /home/tyler/workspace/Snotra/d3
Tried a bunch of paths.
- src="d3.v2.js"
- src="../d3/d3.v2.js"
src="/Foo/d3/d3.v2.js"
- Rinse, repeat.
Foo -> New -> JavaScript Source File -> Advanced -> Link to File in Filesystem -> /home/tyler/workspace/foo/d3/d3.v2.js
尝试了 5 种不同的路径。他们都没有工作(你必须使用相对,绝对?)
- src="/home/tyler/workspace/Foo/d3/d3.v2.js"
- src="../d3/d3.v2.js"
- src="/Foo/d3/d3.v2.js"
- src="d3.v2.js"
因此,我在 Eclipse 中删除了对 d3.v2.js 的引用,并尝试添加为库。
Foo -> JavaScript 资源 -> 添加 JavaScript 库 -> 用户库 - 配置用户库 -> 新建 -> “D3” -> 添加 .js 文件 -> d3.v2.js(位置:/home/tyler/workspace/Snotra /d3
尝试了一堆路径。
- src="d3.v2.js"
- src="../d3/d3.v2.js"
src="/Foo/d3/d3.v2.js"
- 冲洗,重复。
Any ideas? I know that this is really easy, but I just don't understand the fundamentals of adding JavaScript libraries to Eclipse.
有任何想法吗?我知道这真的很简单,但我只是不了解将 JavaScript 库添加到 Eclipse 的基础知识。
foo.html
foo.html
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html" charset="utf-8" />
<title>Tyler J. Fisher</title>
<link rel="stylesheet" href="master.css" />
<script type="text/javascript" src="../d3/d3.v2.js"></script>
</head>
<body>
<section id="foo_view">
<script type="text/javascript">
document.write("test"); <!--Works-->
var dataset[1,2,3,4,5];
d3.select("body").selectAll("p") <!--Doesn't work-->
.data(dataset)
.enter()
.append("p")
.text("RABBLE");
</script>
</section>
</body>
</html>
document.write("test"); works.
document.write("测试"); 作品。
The d3.js code doesn't.
d3.js 代码没有。
So (maybe):
所以(也许):
- The relative path must be off (or maybe I have to use absolute paths)
- Tomcat6 isn't serving JavaScript properly (due to human error)
- Eclipse isn't serving JavaScript properly (due to human error)
- 相对路径必须关闭(或者我可能必须使用绝对路径)
- Tomcat6 未正确提供 JavaScript(由于人为错误)
- Eclipse 未正确提供 JavaScript(由于人为错误)
回答by JB Nizet
What Eclipse deploys to Tomcat is what is inside WebContent
. If your JS files are not in WebContent, they won't be part of the deployed web app.
Eclipse 部署到 Tomcat 的是里面的东西WebContent
。如果您的 JS 文件不在 WebContent 中,它们将不会成为部署的 Web 应用程序的一部分。
You need to understand that what Eclipse shows you is you development, source folders. What is deployed is not this folder, but a directory structure conforming the Java EE specs:
您需要了解 Eclipse 向您展示的是您的开发、源文件夹。部署的不是这个文件夹,而是一个符合 Java EE 规范的目录结构:
- Everything in WebContent is part of the deployed archive
- The Java source files are compiled and stored in the WEB-INF/classes of the deployed archive
- The non-Java files under a source directory ar copied to the WEB-INF/classes of the deployed archive
- All the other files are not part of the deployed archive.
- WebContent 中的所有内容都是已部署存档的一部分
- Java 源文件被编译并存储在部署存档的 WEB-INF/classes 中
- 将源目录下的非 Java 文件复制到部署存档的 WEB-INF/classes
- 所有其他文件都不是已部署存档的一部分。