eclipse 使用 Tomcat 引用 Javascript 库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1021290/
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
Referencing Javascript libraries with Tomcat
提问by Red33mer
I am using Eclipse Ganymedeand Tomcat 5.5. I would like to add some javascript and especially ajax functionality to a dynamic web project and need some help.
我正在使用Eclipse Ganymede和Tomcat 5.5。我想向动态 Web 项目添加一些 javascript,尤其是 ajax 功能,需要一些帮助。
I would like to use jquery (but I am open to other suggestions, if you tell me why another library would be better in this case, but I have chosen jquery because it is supposed to be simple (which on the first look it seems to be)).
我想使用 jquery(但我愿意接受其他建议,如果你告诉我为什么在这种情况下另一个库会更好,但我选择了 jquery,因为它应该很简单(乍一看似乎是))。
I am having two problems:
我有两个问题:
1- Tomcat can't find the jquery library. I tried several things in my jsp file like:
1- Tomcat 找不到 jquery 库。我在我的 jsp 文件中尝试了几件事,例如:
<script type="text/javascript" src="WEB-INF/lib/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/WEB-INF/lib/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="./WEB-INF/lib/jquery-1.3.2.min.js"></script>
As you can see, I threw the jquery library in /WEB-INF/lib
. Executing the jsp file within a browser without tomcat (with the last path version) works, so the path is correct.
如您所见,我将 jquery 库放入/WEB-INF/lib
. 在没有tomcat的浏览器中执行jsp文件(最后一个路径版本)是可以的,所以路径是正确的。
2- There is no proper syntax highlighting within the dynamic web project for jquery and no popup suggestions. I also tried the information in this article, but it didn't change much.
2- jquery 的动态 Web 项目中没有正确的语法突出显示,也没有弹出窗口建议。我也尝试了这篇文章中的信息,但并没有太大变化。
回答by Tacroy
To be more specific (because it took me about half an hour to figure this out after getting to this point):
更具体地说(因为我花了大约半个小时才弄明白这一点):
When you create a Dynamic Web Project with Tomcat in Eclipse, among other things in the project you get a folder named "WebContent". That's the actual folder that gets deployed to the Tomcat server, in Eclipse's equivalent of Tomcat/webapps/<project name>
(I'm not sure where it really exists). For security reasons, as a special case nobody can access the META-INF
and WEB-INF
folders in there, so putting your scripts in those places will not help.
当您在 Eclipse 中使用 Tomcat 创建动态 Web 项目时,除了项目中的其他内容外,您会得到一个名为“WebContent”的文件夹。这是部署到 Tomcat 服务器的实际文件夹,在 Eclipse 中相当于Tomcat/webapps/<project name>
(我不确定它真正存在的位置)。出于安全原因,作为特殊情况,没有人可以访问其中的META-INF
和WEB-INF
文件夹,因此将脚本放在这些位置无济于事。
What you have to do is create a folder inside of WebContent, and stick your Javascript in there. This folder will be globally visible, so visitors to your site (like you, when you test it) can actually get to the Javascript.
您需要做的是在 WebContent 中创建一个文件夹,并将您的 Javascript 粘贴在其中。该文件夹将是全局可见的,因此您网站的访问者(如您,在您测试时)实际上可以访问 Javascript。
What I did, for instance, was create a folder named "script" in WebContent and put my Javascript in there; then, when I needed to reference it in a page, I put in src="ProjectName/script/AwesomesauceJavascript.js"
例如,我所做的是在 WebContent 中创建一个名为“script”的文件夹并将我的 Javascript 放在那里;然后,当我需要在页面中引用它时,我输入src="ProjectName/script/AwesomesauceJavascript.js"
回答by vincent
I'd like to add to what @Tacroy responded with. Within the server you're using in Eclipse, check the server.xml. Make sure:
我想补充@Tacroy 的回应。在 Eclipse 中使用的服务器中,检查 server.xml。确保:
Context docBase="SomeProjectName" path="/SomeProjectName"
<-- path and docBase attributes need to be the same.
Context docBase="SomeProjectName" path="/SomeProjectName"
<-- path 和 docBase 属性需要相同。
I had two different things there, and had to make them identical for the src attribute to work in the jsp.
我在那里有两个不同的东西,并且必须使它们相同才能使 src 属性在 jsp 中工作。
回答by anna
First you must to add resource mapping to your folder where you put jquery.js script library. That folder must be public.
首先,您必须将资源映射添加到您放置 jquery.js 脚本库的文件夹中。该文件夹必须是公共的。
To make folder public use this line of code:
要使文件夹公开,请使用以下代码行:
<resources mapping="/scripts/**" location="/WEB-INF/scripts/**" />
Now you just need to add reference in your page to this path:
现在您只需要在页面中添加对此路径的引用:
<script type="text/javascript" src="scripts/jquery-1.10.2.js" ></script>
回答by Rahul Saini
Below are the steps to enable jQuery syntax highlighting and content assist highlighting in Eclipse.
以下是在 Eclipse 中启用 jQuery 语法高亮和内容辅助高亮的步骤。
Download jqueryWTP0.40foEn.jar.
Find your Eclipse Plugin
org.eclipse.wst.jsdt.core_version.jar
, backup the plugin. (e.g. C:\DEV\EclipseIndigo37\eclipse\plugins \org.eclipse.wst.jsdt.core_1.1.100.v201104272153.jar)Double click the JAR file or run with command java -jar jqueryWTP0.40foEn.jar.
On the opened swing UI, choose
org.eclipse.wst.jsdt.core_version.jar
, and output directory.Click the generate button.
Replace the old
org.eclipse.wst.jsdt.core_version.jar
file with the generated file.Delete the directory
workspace/.metadata/.plugins/org.eclipse.wst.jsdt.core
Start Eclipse.
Open a HTML file or a JavaScript file, edit JavaScript content.
找到你的 Eclipse Plugin
org.eclipse.wst.jsdt.core_version.jar
,备份插件。(例如 C:\DEV\EclipseIndigo37\eclipse\plugins\org.eclipse.wst.jsdt.core_1.1.100.v201104272153.jar)双击 JAR 文件或使用命令 java -jar jqueryWTP0.40foEn.jar 运行。
在打开的swing UI 上,选择
org.eclipse.wst.jsdt.core_version.jar
,然后输出目录。单击生成按钮。
用
org.eclipse.wst.jsdt.core_version.jar
生成的文件替换旧文件。删除目录
workspace/.metadata/.plugins/org.eclipse.wst.jsdt.core
启动日食。
打开 HTML 文件或 JavaScript 文件,编辑 JavaScript 内容。
jQuery content assist is now available.
Plugin Developer & Source
jQuery 内容辅助现已可用。
插件开发者和来源