简体   繁体   English

JavaScript-找不到错误404资源

[英]JavaScript - Error 404 resource not found

I had a java script in my JSP, which worked fine. 我的JSP中有一个Java脚本,效果很好。 Now I want to move it to it's own file (abc.js). 现在,我想将其移动到它自己的文件(abc.js)。 I created js-Folder and placed it there. 我创建了js-Folder并将其放置在此处。 I tried to include it with <script language="JavaScript" type="text/javascript" src="./js/abc.js"></script> which links to http://localhost:8080/ProjectName/js/abc.js (from link in HTML-Code of the page). 我尝试将其包含到链接到http://localhost:8080/ProjectName/js/abc.js <script language="JavaScript" type="text/javascript" src="./js/abc.js"></script> http://localhost:8080/ProjectName/js/abc.js (来自页面HTML代码中的链接)。 I think this should be correct, isn't it? 我认为这应该是正确的,不是吗?

Nevertheless I get errors in the browser console ( Failed to load resource: the server responded with a status of 404 () abc.js ) 但是,我在浏览器控制台中遇到错误( Failed to load resource: the server responded with a status of 404 () abc.js

Do I have to register the .js somewhere to be recognized? 我是否必须在某个地方注册.js才能被识别?

Edit: My project structure is like this: 编辑:我的项目结构是这样的:

ProjectName
   |---WEB-INF
          |---page.jsp (that wants to include abc.js)
          |---js
               |---abc.js

The WEB-INF directory is not publicly accessible. WEB-INF目录不可公开访问。 Your HTML file accesses the JS file over an public URL. 您的HTML文件通过公共URL访问JS文件。 Details see here: Include javascript file from inside WEB-INF 详细信息请参见此处: 在WEB-INF中包含javascript文件

So the solution for your problem is to place the JS file on the same level as the WEB-INF directory, so that it is served publicly and adjust the src link accordingly: 因此,解决您的问题的方法是将JS文件放在与WEB-INF目录相同的级别上,以便将其公开提供并相应地调整src链接:

<script type="text/javascript" src="${pageContext.request.contextPath}/js/abc.js"></script>

( How include an external JS file in a JSP page ) 如何在JSP页面中包含外部JS文件

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

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