2011-10-03 27 views
5

試圖確定在部署到Tomcat 6的web應用程序 中放置樣式表的位置/方式,以便可以解析styles.css。 我已經嘗試了所有我能想到但沒有成功的事情。爲什麼Tomcat 6找不到我的styles.css

我做了這些測試揪出來的是把文件,但 一點成功。

1.) put css in-line with style attribute to verify text display green. 
    <div id="xxx" style="color:green;"> This worked. 
    Then I removed the attribute and 
2.) moved it into a in-file <style></style> stmt in the jsp. This also worked. 
    I copied the css stmt into styles.css and disabled the in-line stmt in the jsp. 
3.) added <link></link> stmts to file. I tried several path refs to the file. 
    And I put the file in several different directory locations to see where 
    it would get resolved. (none were found) 
     <link rel="stylesheet" type="text/css" href="styles.css"> 
     <link rel="stylesheet" type="text/css" href="css/styles.css"> 
     <link rel="stylesheet" type="text/css" href="/css/styles.css"> 

Using FireBug (css tab) I see the follow information for these links 
    * <link rel="stylesheet" type="text/css" href="styles.css"> 
    this displays the src to index.html in the root dir 

    * <link rel="stylesheet" type="text/css" href="css/styles.css"> 
    this displays the msg 
     Apache Tomcat/6.0.13 - Error report 
     HTTP Status 404 
     The requested resource() is not available. 

    * <link rel="stylesheet" type="text/css" href="/css/styles.css">  
    this displays 
     Failed to load source for: http://192.168.5.24:9191/css/clStyles.css 

contextPath中的/微博 和基本路徑是http://192.168.5.24:9191/microblog

這裏是我使用的測試代碼。

我使用Spring 3.我有一個簡單的JSP

- test.jsp的 -

<% 
    String path = request.getContextPath(); 
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
    %> 
    <html> 
     <head> 
     <base href="<%=basePath%>"> 

     <link rel="stylesheet" type="text/css" href="styles.css"> 
     <link rel="stylesheet" type="text/css" href="css/styles.css"> 
     <link rel="stylesheet" type="text/css" href="/css/styles.css"> 
     <style> 
      #Yxxx {color:green;} 
     </style> 

     </head> 

     <body> 
     <div id="xxx"> 
      <h2>Test <%=path%> <%=basePath%></h2> 
     </div> 
     </body> 
    </html> 

我放在styles.css的文件在許多目錄位置看到 哪裏它可能會得到解決,但似乎沒有發現。

在Tomcat中6的部署展開的Web應用程序的目錄結構

webapps/microblog 
    webapps/microblog/styles.css 
    webapps/microblog/index.html 
    webapps/microblog/css/styles.css 
    webapps/microblog/WEB-INF/css/styles.css 
    webapps/microblog/WEB-INF/jsp/admintest/styles.css 
    webapps/microblog/WEB-INF/jsp/admintest/test.jsp 

那麼如何讓Tomcat的6解決.css文件?

回答

8

可能相對的CSS路徑是錯誤的。使其成爲域相對而不是相對路徑。與上下文路徑(這是/microblog你的情況)加前綴:

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/styles.css"> 

注意,在/WEB-INF資源不publicitly訪問。它們只能通過servlet中的RequestDispatcher或JSP中的<jsp:include>訪問。把它們放在/WEB-INF之外。

如果它仍然不起作用,那麼映射到//*的URL模式的servlet或過濾器可能無法正常工作。

+0

謝謝你們兩位。這個信息很有幫助。 根據您的建議更改以及對我的 (將格式從/更改爲 * .html)進行的更改,此操作正常。 我想了解爲什麼url-pattern更改啓用 Tomcat來解決refs。任何一個人都可以指向 文章或提供解釋。 在此先感謝。 – user976831

+2

如果你在'/'上映射一個servlet,那麼你基本上覆蓋了servletcontainer自己的默認servlet,它用於所有與所有其他聲明servlet的URL模式不匹配的請求(例如,靜態資源如圖像,CSS,JS ,等文件)。如果是Tomcat,請查看http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html – BalusC

3

有下列情形的將工作部署到http://192.168.5.24:9191/microblog

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

這一個點http://192.168.5.24:9191/styles.css

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

這一個點http://192.168.5.24:9191/css/styles.css

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

這一個點http://192.168.5.24:9191/css/styles.css

您需要前綴上下文路徑:

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/styles.css"> 

或完整路徑:在/ WEB-INF

<link rel="stylesheet" type="text/css" href="<%=basePath%>/styles.css"> 

的資源應該被刪除,因爲它們是不可用於外部請求

6

大概是很晚了,但我設法以不同的方式解決這個問題。

我有我的WebContent內一個簡單的HTML文件和CSS文件夾:

這是行不通的:

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

,並沒有這樣的:

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

這是工作,但這是醜陋的(它也給了我一個Eclipse的警告):

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

,所以我發現這也是一個工作,而在我看來,最好的辦法:

<link rel="stylesheet" type="text/css" href="./css/styles.css"> 
+0

這非常好! $ {pageContext.request.contextPath}導致一些隨機編譯錯誤,這使我瘋狂......謝謝! – TacB0sS

+0

真棒簡單的HTML頁面我使用它給出確切的路徑 –

0

也可以考慮使用HTML base tag,這樣你就不需要輸入$ {pageContext.request.contextPath } 每時每刻。

0

這也發生在我身上。我只需在css文件的任意位置輸入空格並保存即可解決問題。路徑應該是「css/style.css」。在我看來,如果該文件(或文件路徑)發生更改(如重命名或重定位),Tomcat有時不會立即識別您的文件。我發現讓Tomcat「意識到」這一改變的唯一方法是對文件進行一些更改並保存,而重新部署或刷新對此沒有幫助。