有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

jstl在JSP中添加外部资源(CSS/JavaScript/images等)

我在我的项目中添加了一个外部CSS样式表,并将其放在Eclipse中项目的WEB-CONTENTS文件夹中。当我在Tomcat上部署它时,没有应用样式表。当我在Chrome中调试并打开它时,它给了我404 file not found错误。这是为什么?如何解决

以下是代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>joined now </title>

<link href="globalCSS.css" rel="stylesheet" type="text/css"/>

</head>
<body>
<div>this is at the top</div>
<c:import url="header.jsp" />
<c:import url="navigationBar.jsp" />  
<c:import url="leftpane.jsp" /> 
<c:import url="mainContent.jsp" /> 
<c:import url="rightpane.jsp" />
<c:import url="footer.jsp" />  
</body>
</html>

共 (1) 个答案

  1. # 1 楼答案

    使用以下代码可以解决这个问题。。。。如果您使用localhost server运行一个文件,那么这个问题可以通过以下Jsp页面代码来解决。这段代码放在jsp文件的Head标记之间

    <style type="text/css">
        <%@include file="css/style.css" %>
    </style>
    <script type="text/javascript">
        <%@include file="js/script.js" %>
    </script>