我在tomcat上运行了一个JavaEE项目。我的JSP如下。
它在我的WEB-INF文件夹中,并通过servlet调用。我的资源文件夹中的WEB-INF之外有静态内容,例如CSS。到目前为止,一切都还不错-JSP正在访问CSS并显示了一个漂亮的网页,但是样式突然间停止显示。我真的不知道为什么,因为我什么都没做。当我在多个设备上的多个浏览器中查看source和inspect元素时,我发现JSP甚至没有加载CSS中的样式。好像样式表不存在。
JSP绝对不是问题,因为我尝试了${pageContext.request.contextPath}
以及尝试了<c:url ... />
,但是没有人解决这个问题。我还尝试将CSS移到WEB-INF目录中,但这也不起作用。
我使用Intellij的香草JSP模板创建了一个新的JSP,在访问CSS文件时似乎也遇到了一些问题,因此该问题似乎在整个项目范围内都存在。
问题是我不知道还有什么可能影响JSP而不获取CSS文件。
这是我的JSP
<%--
Created by IntelliJ IDEA.
User: micro
Date: 2/6/2020
Time: 9:19 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="<c:url value = '/resources/css/mystyles.css' />">
<title>Chirag Kawediya - Homepage</title>
</head>
<body>
<nav class="navbar sticky-top navbar-expand-md navbar-dark bg-dark container" style="margin-bottom: 5px !important;">
<a href="#" class="navbar-brand" id="main-logo" >Chirag Kawediya</a>
<button class="navbar-toggler" data-toggle="collapse" data-target="#navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navigation">
<ul class="navbar-nav ml-auto mr-2">
<li class="nav-item"><a href="#" class="nav-link">international movie blog</a></li>
<li class="nav-item"><a href="#" class="nav-link">stuff i made</a></li>
</ul>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-lg-9 col-md-8 col-sm-12 bg-white border border-dark" style="background-image: url('${pageContext.request.contextPath}/resources/images/MyPortrait.JPG'); background-size: cover;">
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>
<div class="col-lg-3 col-md-4 col-sm-12 bg-white pt-3">
<h1>welcome</h1>
<p>I am Chirag Kawediya. I made this website to keep track of my multiple interests simultanenously. As an avid movie-goer,
I started to watch foreign language films. Now I write about them in my <a href="#">International Movie Blog.</a> In addition,
I code (mostly in Java), and I will be putting some of my creations on my website <a href="#">here.</a></p>
</div>
</div>
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-12 bg-dark pt-1">
<nav class="navbar navbar-dark bg-dark">
<a href="#" class="navbar-brand mt-1 mb-auto">recent posts</a>
<ul class="navbar-nav">
<c:forEach var="post" items="${posts}">
<li class="nav-item"><a href="${pageContext.request.contextPath}/post?id=${post.id}" class="nav-link"><c:out value="${post.title}" /></a></li>
</c:forEach>
</ul>
</nav>
</div>
<div class="col-lg-9 col-md-9 col-sm-12 bg-white pt-3">
<h2>about me</h2>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. </p>
</div>
</div>
<footer>
<div class="row" style="margin-bottom: 0 !important;">
<div class="col-lg-4 col-md-4 bg-dark text-white pt-3 pl-4">
<b>Links</b>
<nav class="navbar navbar-dark bg-dark ml-n3">
<ul class="navbar-nav" style="line-height: 0.5 !important;">
<li class="nav-item"><a href="#" class="nav-link">Homepage</a></li>
<li class="nav-item"><a href="#" class="nav-link">International Movie Blog</a></li>
<li class="nav-item"><a href="#" class="nav-link">Stuff I Made</a></li>
<li class="nav-item"><a href="#" class="nav-link">Resume</a></li>
</ul>
</nav>
</div>
<div class="col-lg-4 col-md-4 bg-dark text-white pt-3 pl-4">
<b>Contact Information</b>
<p>Phone: +1 (XXX) XXX-XXXX</p>
<p>Email: example@gmail.com</p>
</div>
<div class="col-lg-4 col-md-4 bg-dark text-white pt-3 pl-4 pr-auto">
<b>Other Stuff</b>
<nav class="navbar navbar-dark bg-dark ml-n3">
<ul class="navbar-nav" style="line-height: 0.5 !important;">
<li class="nav-item"><a href="http://coolmathgames.com" class="nav-link">CoolMathGames</a></li>
<li class="nav-item"><a href="http://google.com" class="nav-link">Google</a></li>
</ul>
</nav>
</div>
</div>
</footer>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
这是我的web.xml,以防万一。
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>homepage</welcome-file>
</welcome-file-list>
<context-param>
<param-name>DBusername</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>DBpassword</param-name>
<param-value>chirag12</param-value>
</context-param>
<context-param>
<param-name>uploadFilePath</param-name>
<param-value>/usr/share/websitestuff</param-value>
</context-param>
...
a ton of servlet mappings that I edited out
...
<servlet>
<servlet-name>CommentCreator</servlet-name>
<servlet-class>CommentUploader</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CommentCreator</servlet-name>
<url-pattern>/new-comment</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/404</location>
</error-page>
<servlet>
<servlet-name>testservlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testservlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
另外,我引用的引导CSS似乎是由JSP注册并加载的。因此,似乎我尝试加载的项目的webapps文件夹中的内容未加载。任何帮助将不胜感激!谢谢!
tl; dr JSP完全忽略了webapps文件夹(甚至是WEB-INF)中的资源,就好像它们不存在一样
更新:问题变得更加陌生。我还意识到,除非它是一个servlet,否则服务器似乎甚至不解释任何以.jsp结尾的内容。如果我访问mywebsite.com/page-that-not-exist存在,即使可以,我也不会得到404。如果我访问mywebsite.com/page-that-doesn't-exist.jsp,我会得到404。基本上,仅JSP文件结尾不被忽略,其余的则视webapp而定。我真的不知道这意味着什么或如何解决。