STS Spring MVC:如何在JSP中包含JS文件 [英] STS Spring MVC: How to include a JS file in a JSP

查看:70
本文介绍了STS Spring MVC:如何在JSP中包含JS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了SpringSource Tool Suite 2.8.0.我正在尝试使用Spring MVC模板作为起点在JSP中包含一个JS文件.我的JSP如下所示:

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:

<%@ 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位于src \ main \ resources下,看起来像这样:

a.js is under src\main\resources and looks like this:

window.alert("A");

结果就是你好,世界!"在没有警报的情况下被打印:-(

The result is that "Hello world!" gets printed without the alert :-(

我尝试将JS文件放在不同的位置,将src更改为带有/不带有"/",甚至在web.xml中添加一个servlet映射,以将"* .js"使用默认" servlet.似乎没有任何作用.

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.

我在做什么错了?

推荐答案

.war文件中是否包含js文件?我通常将js和CSS放在src/main/webapp中.诸如src/main/webapp/js和src/main/webapp/css之类.

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.

第二,您可以使用c:url适当地引用它,这将确保将应用程序上下文放在那里.

Secondly, you can reference it appropriately using c:url which will take care of putting the app context on there and stuff.

<script type="text/javascript" src="<c:url value="/a.js" />" />

您可以使用firebug或chrome的开发人员工具查看a.js是否获取404,并查看其实际请求的路径.

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.

这篇关于STS Spring MVC:如何在JSP中包含JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆