2016-11-03 53 views
0

我試着從名爲「Murach的Java Servlet和JSP」的書中嘗試示例代碼由於某些原因,我無法使CSS工作。 Netbeans似乎沒有找到使用relative href路徑的main.css文件。Netbeans WEB應用程序項目無法讀取CSS文件的相對路徑

的index.html

<html> 
    <head> 
     <title>Murcah's Java Servlet and JSP</title> 
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
     <link rel="stylesheet" href="resources/css/main.css" type="text/css" /> 
    </head> 
    <body> 
     <h1>Join our email list</h1> 
     <p>To join our email list, enter your name and email address below.</p> 

     <form action="emailList" method="post"> 

      <input type="hidden" name="action" value="add" /> 

      <label>Email: </label> 
      <input type="email" name="email" required /> <br /> 
      <label>First Name:</label> 
      <input type="text" name="firstName" required /> <br/> 
      <label>Last Name:</label> 
      <input type="text" name="lastName" required /> <br /> 

      <label>&nbsp;</label> 
      <input type="submit" value="Join Now" id="submit" /> 

     </form> 
    </body> 
</html> 

的main.css

body{ 
    font-family: Arial,Helvetica,sans-serif; 
    font-size: 11pt; 
    margin-left: 2em; 
    margin-right: 2em; 
    background-color: darkred; 
    background: yellow; 
} 

h1{ 
    color:teal; 
} 

label{ 
    float:left; 
    width:6em; 
    margin-bottom: 0.5em; 
} 

input[type="text"], input[type="email"]{ 
    width: 15em; 
    margin-left: 0.5em; 
    margin-bottom: 0.5em; 
} 

br{ 
    clear:both; 
} 

#submit{ 
    margin-left: 0.5em; 
} 

的context.xml

<?xml version="1.0" encoding="UTF-8"?> 
<Context path="/ch02email"/> 

這裏是如何的資源被組織到子文件夾中。

enter image description here

而且main.css路徑我認爲是ch02email/resources/css/main.css但因爲我已經設置的contextPath到

<?xml version="1.0" encoding="UTF-8"?> 
    <Context path="/ch02email"/> 

的index.html你可以看到,我的href<link rel="stylesheet" href="resources/css/main.css" type="text/css" />

出於某種原因,CSS不適用。另外,我試圖查看源打開CSS,但我得到了FF。

enter image description here

視圖源enter image description here

視圖源我得到一個404消息。

enter image description here

查看源

我想無論在FirefoxGoogle Chrome,但我得到了相同的結果。無法找到CSS enter code here文件。

我在這裏錯過了什麼?我甚至試圖把WEB-INF/resources/css/main.css,我不認爲是正確的,因爲我明白在引用時不包括WEB-INF

最後,我試圖用<base href="${pageContext.request.contextPath}/"/>沒有成功。

請幫忙。

謝謝。

+0

post web.xml以及 – developer

回答

0

我剛剛發現它無法訪問main.css文件的原因。我找到了信息here。我發現包含在WEB-INF中的文件無法公開訪問。所以我所做的就是移動resources文件夾,就像屏幕截圖上的內容一樣。

enter image description here

resources文件夾現在包含在Web Pages文件夾中。

enter image description here

我知道這是一個簡單的問題,但引起的頭痛對於非常簡單的細節,我不知道。

我希望這可以幫助其他人找到這篇文章。 :)

相關問題