【发布时间】:2014-03-09 02:02:12
【问题描述】:
我浏览了针对此问题发布的所有 SO 问题。但我没有找到解决办法。
我使用 IntelliJ Idea 创建了项目 Spring MVC 项目,并且我使用的是 Tomcat 7.42 我的项目结构如图所示。
我的 JSP 代码
<html>
<head>
<script src='/js/index.js' type="text/javascript"/>
</head>
<body>
<h1>${message}
${pageContext.request.contextPath}
${pageContext.request.contextPath}
</h1>
<form:form action="/personDetails" commandName="command">
<p><form:errors path="*" cssClass="error"/></p>
Age :<form:input path="age" cssErrorClass="myErrorClass"></form:input><br>
Name:<form:input path="name"></form:input><br>
City:<form:input path="city"></form:input><br>
<input type="submit" value="Save">
</form:form>
</body>
</html>
web.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
现在我的问题是我无法找到 js 文件,而且 ${pageContext.request.contextPath} 是空白。
同样的原因是什么?
【问题讨论】:
-
您是否在 tomcat 根目录中运行您的应用程序?您用于查看该页面的 URL 是什么?另外,请尝试将
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>添加到 JSP 的顶部。 -
我正在使用 IDE 运行应用程序,我的 jsp 完全可以缩短我没有在上面的代码中添加 java.sun.com/jsp/jstl/core" %> 的代码.我删除并更新了代码以避免混淆
-
您正在查看带有上下文路径的页面吗?页面地址是什么!!!!
-
${pageContext.request.contextPath} 给我空白,我的网址是 localhost:9999/pageDetails
-
所以您正在将您的应用程序作为 ROOT tomcat 应用程序运行 - 因此
contextPath应该为空白。见stackoverflow.com/a/11581460/249327
标签: javascript jsp spring-mvc intellij-idea tomcat7