我的jsp文件找不到我的javascript文件

Kev*_*ent 0 java jsp servlets

我的jsp文件找不到我的javascript文件.在这里你可以看到我的jsp文件:

    <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="ressources/js/addMatrix.js"></script>
</head>
<body>
    <div ng-app="App" ng-controller="MainCtrl">
        <p>Add a new element : </p>
        <table>
            <tr>
                <td data-ng-repeat="data in texts">
                    <button>{{data.text}}</button>
                </td>
                <td data-ng-repeat="field in fields">
                    <form ng-submit="submit(text)">
                        <input type="text" ng-model="text" name="text"
                            placeholder="New Category" />
                    </form>
                </td>
                <td>
                    <button ng-click="addNewChoice()">+</button>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的javascript文件位于文件夹/ WebContent/ressources/js中,jsp文件位于文件夹/ WebContent中.

我的servlet只在get方法中包含这个调用:

this.getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);
Run Code Online (Sandbox Code Playgroud)

我的web.xml文件如下所示:

 <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <servlet>
    <servlet-name>Home</servlet-name>
    <servlet-class>com.pi.servlets.Index</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Home</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

等待你的帮助.谢谢

Psh*_*emo 5

我不是JavaEE dev,所以这可能不是最佳解决方案,但我认为这可能是一个好的开始

我的jsp文件找不到我的javascript文件

<script src="ressources/js/addMatrix.js"></script>在客户端执行.既然它不是绝对的路径

https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js
Run Code Online (Sandbox Code Playgroud)

但相对一个,它会尝试ressources根据当前位置(浏览器可用的URL)查找目录.所以,如果你的网址是这样的

http://server/project/someTask/yourServlet
Run Code Online (Sandbox Code Playgroud)

它会试着找到它

http://server/project/someTask/ 
Run Code Online (Sandbox Code Playgroud)

而不是resources内心

http://server/project/
Run Code Online (Sandbox Code Playgroud)

简单的方法是使您的路径相对于服务器的根目录(因此启动它/)并包含项目名称:

<script src="/project/ressources/js/addMatrix.js"></script>
Run Code Online (Sandbox Code Playgroud)

或者为了使它更具动态性,您可以/project使用EL 读取表单中的项目名称,因此您的代码可能看起来像

<script src="${pageContext.request.contextPath}/ressources/js/addMatrix.js"></script>
Run Code Online (Sandbox Code Playgroud)