【问题标题】:JSP - CSS file and JS files aren't loading at allJSP - CSS 文件和 JS 文件根本没有加载
【发布时间】:2021-10-04 13:22:43
【问题描述】:

我无法在 JSP 文件中加载 js 和 css 文件。在桌面上它以http://localhost/ 运行,但在服务器上它将以http://localhost/myapp/ 运行。如何确保它在任何地方都能正常工作?

GET http://localhost:8080/js/jquery.min.js net::ERR_ABORTED 404 (Not Found)

GET http://localhost:8080/css/bootstrap.min.css net::ERR_ABORTED 404 (Not Found)

在 head Head 标签中我有这个..

                <script src="/js/jquery.min.js" type="text/javascript"></script>
                <link rel="stylesheet" type="text/css"  href="/css/bootstrap.min.css">

编辑:我尝试了以下方法,但仍然无法正常工作..

编辑:甚至按照article中的建议尝试了以下操作

【问题讨论】:

  • 配置的 docroot 文件夹似乎没有指向路径 webapp/WEB-INF/pages。要么将这两个文件移动到配置的 docroot 文件夹中,要么更改 jsp 文件中的 url。
  • 在这种情况下,哪个是 docroot 文件夹? index.jsp 从 WEB-INF/pages 加载正常
  • http://localhost:8080/js/jquery.min.js 有效吗?在我看来,错误消息与现有源代码不同。确定只有一个页面使用了这个包含吗?
  • 不,它不起作用。顺便说一句,我将 css 和 js 添加到 src/main/resources/static/ 但它仍然无法正常工作
  • 如我所见,错误信息与您的代码不匹配。

标签: html css spring spring-boot jsp


【解决方案1】:

修复它。

我将cssjs 文件夹移至src/main/resoucres/static/,还必须从Application.java 中删除@EnableMVC,然后它才开始工作。

我还必须将{pageContext.request.contextPath} 添加到index.jsp,否则它将无法在服务器上运行,因为 URL 将是 http://localhost/myapp/

<script src="{pageContext.request.contextPath}/js/jquery.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css"  href="{pageContext.request.contextPath}/css/bootstrap.min.css">

【讨论】:

    猜你喜欢
    • 2018-07-22
    • 2017-05-07
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多