External Javascript not working

我打算把javascript写到一个.js文件里,然后再引用他,如下面

<html>
<head>
</head>
<body>
	<script  type="text/javascript" src="${pageContext.request.contextPath}/WEB-INF/myjs/aa.js"></script>
	
</body>
</html>

aa.js

alert("aa");

但是始终没有"aa"这个提示

之后改成

<html>
<head>
</head>
<body>

<script  type="text/javascript" src="${pageContext.request.contextPath}/WEB-INF/myjs/aa.js"></script>
<script type="text/javascript">
gg();
</script>
</body>
</html>


aa.js

function gg(){
alert("zz");
}

但是提示

gg();            这行缺少对象

于是我怀疑是否aa.js没导入进去,但是检查路径没错。。

之后我把文件夹myjs存到WEB-INF外面,并更改相关的路径就找到了aa.js了

<html>
<head>
</head>
<body>

<script  type="text/javascript" src="${pageContext.request.contextPath/myjs/aa.js"></script>
<script type="text/javascript">
gg();
</script>
</body>
</html>

所以我怀疑是.js不能存入WEB-INF里,不然无法识别

于是我在tomcat网站找到这样一段话

To facilitate creation of a Web Application Archive file in the required format, it is convenient to arrange the "executable" files of your web application (that is, the files that Tomcat actually uses when executing your app) in the same organization as required by the WAR format itself. To do this, you will end up with the following contents in your application's "document root" directory:

注意:application's "document root" directory就相当于项目跟路径,即(${pageContext.request.contextPath)

*.html, *.jsp, etc. - The HTML and JSP pages, along with other files that must be visible to the client browser (such as JavaScript, stylesheet files, and images) for your application. In larger applications you may choose to divide these files into a subdirectory hierarchy, but for smaller apps, it is generally much simpler to maintain only a single directory for these files.
这里告诉用户:javascrip等文件对于用户的浏览器必须是可见的。

/WEB-INF/web.xml - The Web Application Deployment Descriptor for your application. This is an XML file describing the servlets and other components that make up your application, along with any initialization parameters and container-managed security constraints that you want the server to enforce for you. This file is discussed in more detail in the following subsection.
转自文档http://tomcat.apache.org/tomcat-7.0-doc/appdev/deployment.html#Deployment_With_Tomcat

而/WEB-INF/里对于用户是不可见的

Tomcat doesn't let you access anything in the META-INF or WEB-INF folders.  These folders have config for your system, and may contain passwords (such as META-INF/context.xml which has passwords for the datasources).  Besides, people can get into WEB-INF/lib and WEB-INF/classes and web.xml and see your implementation details.
上面一段话转自:http://tomcat.10.n6.nabble.com/Unable-to-access-images-stored-in-webapps-lt-myapp-gt-WEB-INF-images-td4334193.html

所以出现External Javascript not working是由于用户看不到WEB-INF文件夹里面的内容,注意不要讲js导入到web-inf里。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值