JavaScript文件未加载到.jsp文件中

时间:2017-03-29 12:57:09

标签: javascript jquery html scala

目前我在前端编程,我遇到了一个问题,我的javascript文件没有加载。我的文件是.jsp,因为scala / scalatra后端。我有我的index.jsp,其中我在顶部包含我的头文件,在底部包含我的页脚。

我把它们包括在内:

<jsp:include page="header.jsp"/>
// CODE //
<jsp:include page="footer.jsp"/>

我的标题如下:

<!-- HEADER -->
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="styles/cssfile.css">
    <link rel="stylesheet" href="css/cssfile.css">
</head>
<body>

我的页脚看起来像这样:

  <!-- FOOTER -->
      <script type="text/javascript" src="js/jquery.js"></script>
      <script type="text/javascript" src="js/main.js"></script>
</body>
</html>

我的main.js看起来像这样:

$(document).ready(function () {
'use strict';
$(function() {

    $('a[href*=#]:not([href=#])').click(function () {

          if (
                  location.pathname.replace(/^//,'') == this.pathname.replace(/^//,'')
                || location.hostname == this.hostname
            ) {

            var target = $(this.hash);

            target = target.length ? target : $('[name=' + this.hash.slice(1) +']');

            if ( target.length ) {

                  $('html,body').animate({
                    scrollTop: target.offset().top
                  }, 1000);
                  return false;

            }

          }

    });

});

});

我的文件结构是这样的:

css
  -cssfile1
  -cssfile2
js
  -jquery.js
  -main.js
header.jsp
index.jsp
footer.jsp

先生src路线应该是正确的,当我查看网站的源代码时,我可以看到main.js文件并点击它,这是正确的js。 我试过console.log(&#34; test&#34;);没有任何东西出现,所以它不会加载。 我在其中添加了jquery.js和jquery javascript,并在页脚中首先添加它,如你所见。

所以我想要的是我的main.js应该加载。

提前致谢:)

1 个答案:

答案 0 :(得分:0)

JSP EL提供了一个变量($ {pageContext.request.contextPath}),可以在设计时使用它来帮助在运行时解析正确的路径。试试这可能有帮助,你使用正确的jquery库吗?

<script type="text/javascript" src="${pageContext.request.contextPath}/js/main.js"></script>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js">