0

我正在使用 Maven 构建 SpringMVC3.2、JSP 和 Tomcat。我的 Web 应用程序结构如下所示。

tomcat
  |-- webapps
    |-- MyApp
      |-- css
      |-- js
        |-- jquery.js
      |-- images
      |-- META-INF
      |-- WEB-INF
        |-- classes
        |-- lib
        |-- src
        |-- web.xml
        |-- pages
             |-- index.jsp

我在 index.jsp 中设置了 JavaScript 路径,如下所示:

<script src="../js/jquery.js"></script>

但它无法加载 Javascript 文件。我更改了路径并在这里和那里移动了文件,但一切都没有奏效。我错过了什么?请帮我。

提前致谢。

编辑实际上,我通过不同的社区找到了答案。

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
    <script src="${contextPath}/js/jquery-1.9.1.js"></script>

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

这不是一个漂亮的方式,但它对我来说很好,如果你想要不同的解决方案,请在下面查看我的答案。

4

2 回答 2

0
tomcat
  |-- webapps
    |-- YourApplicatipName
      |-- css
      |-- js
        |-- jquery.js
      |-- images
      |-- META-INF
      |-- WEB-INF
        |-- classes
        |-- lib
        |-- src
        |-- web.xml

th js 文件夹应该只在你的根文件夹中

现在你给定的路径可以工作

于 2013-03-06T10:49:33.457 回答
0

web.xml

<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
<scripting-invalid>true</scripting-invalid>
<include-prelude>/WEB-INF/common/commonDefinition.jspf</include-prelude>
</jsp-property-group>
</jsp-config>

commonDefinition.jspf

<%@ page language="java" contentType="text/html; charset=UTF-8" %><%@ 
taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ 
taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %><%@ 
taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %><%@ 
taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
于 2013-03-07T04:19:40.407 回答