带有 Spring MVC 的 JavaScript 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10340392/
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
JavaScript with Spring MVC doesn't work
提问by jantobola
I downloaded login template which uses css and JavaScript. When I start it in default html format it displays ok, but when I put the same code to my Spring MVC 3 app and change format to jsp, javascript part is disabled. I can access external js files through my web browser, so I don't know where may be the problem.
我下载了使用 css 和 JavaScript 的登录模板。当我以默认 html 格式启动它时,它显示正常,但是当我将相同的代码放入 Spring MVC 3 应用程序并将格式更改为 jsp 时,javascript 部分被禁用。我可以通过我的网络浏览器访问外部js文件,所以我不知道问题出在哪里。
This is the right look:
这是正确的外观:
but this is what i get:
但这就是我得到的:
<script language="javascript" type="text/javascript" src="js/niceforms.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="css/niceforms-default.css" />
I only changed the location of external js and css resources but these files are accessable as I said.
我只更改了外部 js 和 css 资源的位置,但正如我所说,这些文件是可以访问的。
This is my structure:
这是我的结构:
In my web.xml
I have these lines:
在我的web.xml
我有这些行:
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml,
/WEB-INF/spring/security-context.xml,
</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.txt</url-pattern>
</servlet-mapping>
I also use Apache tiles2
我也使用 Apache tile2
<tiles-definitions>
<definition name="base.definition" template="/WEB-INF/views/layout/layout.jsp">
<put-attribute name="body" value="" />
</definition>
<definition name="contact" extends="base.definition">
<put-attribute name="body" value="/WEB-INF/views/contact.jsp" />
</definition>
<definition name="login" template="/WEB-INF/views/login/login.jsp">
</definition>
Thanks.
谢谢。
Edit:
编辑:
I know where is the problem now, but still don't know how to solve it... Problem is in paths in external css files. I have something like this: body {background-image:url('images/bg.jpg');}
but I get not found error in firebug because it tries to find the resource in localhost:8080/sheedo/css/images/bg.jpgand when I set absolute path of resource like body {background-image:url('/images/bg.jpg');}
I get the same error too but it tries to find it in localhost:8080/images/bg.jpg. When I use ./images/bg.jpg
it's ok, but does exist any other solution instead of rewrite all paths in css files? I mean something like <mvc:resources ... >
is it possible ?
我现在知道问题出在哪里,但仍然不知道如何解决...问题出在外部 css 文件中的路径中。我有这样的事情:body {background-image:url('images/bg.jpg');}
但我在 firebug 中没有找到错误,因为它试图在localhost:8080/sheedo/css/images/bg.jpg 中找到资源,当我设置资源的绝对路径时,body {background-image:url('/images/bg.jpg');}
我也遇到了同样的错误但它试图在localhost:8080/images/bg.jpg 中找到它。当我使用./images/bg.jpg
它时没问题,但是是否存在任何其他解决方案而不是重写 css 文件中的所有路径?我的意思<mvc:resources ... >
是有可能吗?
回答by Raghuveer
<script type="text/javascript" src="${pageContext.request.contextPath}/js/niceforms.js"></script>
<link href="${pageContext.request.contextPath}/css/niceforms-default.css" rel="stylesheet" />
Use the above code.. You need to specify it dynamically, not statically..
使用上面的代码..你需要动态指定它,而不是静态的..
回答by Ramesh Kotha
Use absolute path in your jsps for javascript and css
在 jsps 中为 javascript 和 css 使用绝对路径
add this in your jsp
在你的jsp中添加这个
<%
String path = request.getContextPath();
%>
Change these two lines
改变这两行
<script language="javascript" type="text/javascript" src="<%=path%>/js/niceforms.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="<%=path%>/css/niceforms-default.css" />
回答by raddykrish
There are two things i am seeing here
我在这里看到两件事
- The default servlet is not having any servlet mapping. The tomcat documentation says like this http://tomcat.apache.org/tomcat-5.5-doc/default-servlet.html(i believe you are using tomcat).
- If there is path problem in jsp try using the jstl core librarie's
<c:url/>
which is more cleaner. http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/tld-summary.html
- 默认 servlet 没有任何 servlet 映射。tomcat 文档这样说http://tomcat.apache.org/tomcat-5.5-doc/default-servlet.html(我相信你正在使用 tomcat)。
- 如果 jsp 中存在路径问题,请尝试使用
<c:url/>
更干净的 jstl 核心库。http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/tld-summary.html
回答by djangofan
Instead of that ugly ${pageContext.request.contextPath}
, if you use this in your template:
而不是那个丑陋的${pageContext.request.contextPath}
,如果你在你的模板中使用它:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
Then you can do this:
然后你可以这样做:
<spring:url value="/resources/jquery-2.1.4.js" var="jqueryJS" />
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
var script = document.createElement('script');
script.type = "text/javascript";
script.src = "${jqueryJS}";
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>