javascript STS Spring MVC:如何在 JSP 中包含 JS 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7941880/
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
STS Spring MVC: How to include a JS file in a JSP
提问by Hagai Cibulski
I installed SpringSource Tool Suite 2.8.0. I'm trying to include a JS file in a JSP, using the Spring MVC template as a starting point. My JSP looks like this:
我安装了 SpringSource Tool Suite 2.8.0。我试图在 JSP 中包含一个 JS 文件,使用 Spring MVC 模板作为起点。我的 JSP 看起来像这样:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
<script type="text/javascript" src="/a.js"></script>
</head>
<body>
Hello world!
</body>
</html>
a.js is under src\main\resources and looks like this:
a.js 在 src\main\resources 下,看起来像这样:
window.alert("A");
The result is that "Hello world!" gets printed without the alert :-(
结果是“Hello world!” 在没有警报的情况下打印:-(
I tried putting the JS file in different places, changing the src to be with/without "/", and even adding a servlet-mapping in web.xml to use "default" servlet for "*.js". Nothing seems to work.
我尝试将 JS 文件放在不同的位置,将 src 更改为带/不带“/”,甚至在 web.xml 中添加一个 servlet 映射以使用“*.js”的“默认”servlet。似乎没有任何效果。
What am I doing wrong?
我究竟做错了什么?
采纳答案by digitaljoel
is the js file getting included in your .war file? I usually put my js and css in src/main/webapp. something like src/main/webapp/js and src/main/webapp/css.
js 文件是否包含在您的 .war 文件中?我通常把我的 js 和 css 放在 src/main/webapp 中。类似于 src/main/webapp/js 和 src/main/webapp/css。
Secondly, you can reference it appropriately using c:url which will take care of putting the app context on there and stuff.
其次,您可以使用 c:url 适当地引用它,它将负责将应用程序上下文放在那里等等。
<script type="text/javascript" src="<c:url value="/a.js" />" />
You can use firebug or chrome's developer tools to see if you are getting a 404 for a.js and see what path it is actually requesting.
您可以使用 firebug 或 chrome 的开发人员工具来查看是否为 a.js 获取了 404,并查看它实际请求的路径。
回答by krams
I advise placing those js files under webappfolder. (I usually put them under webapp/resources/js)
我建议将这些 js 文件放在webapp文件夹下。(我通常把它们放在webapp/resources/js 下)
And to make this path accessible, I use the mvc:resourcestag:
为了使这条路径可访问,我使用了mvc:resources标签:
This tag allows static resource requests following a particular URL pattern to be served by a ResourceHttpRequestHandler from any of a list of Resource locations. This provides a convenient way to serve static resources from locations other than the web application root, including locations on the classpath. The cache-period property may be used to set far future expiration headers (1 year is the recommendation of optimization tools such as Page Speed and YSlow) so that they will be more efficiently utilized by the client. The handler also properly evaluates the Last-Modified header (if present) so that a 304 status code will be returned as appropriate, avoiding unnecessary overhead for resources that are already cached by the client. For example, to serve resource requests with a URL pattern of /resources/** from a public-resources directory within the web application root, the tag would be used as follows:
此标记允许遵循特定 URL 模式的静态资源请求由来自任何资源位置列表的 ResourceHttpRequestHandler 提供服务。这提供了一种从 Web 应用程序根以外的位置(包括类路径上的位置)提供静态资源的便捷方法。cache-period 属性可用于设置远期到期标头(1 年是优化工具如 Page Speed 和 YSlow 的建议),以便客户端更有效地利用它们。处理程序还会正确评估 Last-Modified 标头(如果存在),以便适当地返回 304 状态代码,从而避免客户端已经缓存的资源的不必要开销。例如,
<mvc:resources mapping="/resources/**" location="/public-resources/"/>
Source: Spring Reference
来源:Spring 参考