为什么Tomcat 6找不到我的styles.css

use*_*831 5 css tomcat

尝试确定在部署到Tomcat 6的Web应用程序中放置样式表的位置/方式,以便可以解析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
Run Code Online (Sandbox Code Playgroud)

contextPath是/ microblog和basepath是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>
Run Code Online (Sandbox Code Playgroud)

我已将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
Run Code Online (Sandbox Code Playgroud)

那么如何让Tomcat 6解析.css文件呢?

Bal*_*usC 9

可能相对的CSS路径是完全错误的.使其相对于域相对而不是路径相对.前缀为上下文路径(/microblog在您的情况下):

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/styles.css">
Run Code Online (Sandbox Code Playgroud)

请注意,资源/WEB-INF不可公开访问.它们只能通过RequestDispatcherservlet或<jsp:include>JSP访问.把它们放在外面/WEB-INF.

如果它仍然不起作用,则可能是一个servlet或过滤器,它映射到URL模式//*正在正常工作.

  • 如果你在`/`上映射一个servlet,那么你基本上覆盖了servletcontainer自己的默认servlet,它用于所有与所有其他声明的servlet的URL模式不匹配的请求(例如,静态资源,如图像,CSS,JS,等文件).如果是Tomcat,请查看http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html (3认同)

Enr*_*man 6

可能现在已经很晚了,但我设法以不同的方式解决了这个问题.

我的WebContent和css文件夹中有一个简单的html文件:

这不起作用:

<link rel="stylesheet" type="text/css" href="css/style.css">
Run Code Online (Sandbox Code Playgroud)

这不是:

<link rel="stylesheet" type="text/css" href="/css/styles.css">
Run Code Online (Sandbox Code Playgroud)

这是工作,但它很难看(它也在Eclipse中给我一个警告):

<link rel="stylesheet" type="text/css" href="myproject/css/styles.css">
Run Code Online (Sandbox Code Playgroud)

所以我发现这个也正常,对我来说似乎是最好的方法:

<link rel="stylesheet" type="text/css" href="./css/styles.css">
Run Code Online (Sandbox Code Playgroud)