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

发布于 2024-12-07 19:23:06 字数 2963 浏览 0 评论 0原文

尝试确定在 Web 应用程序中放置样式表的位置/方式 部署到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 是 /microblog 基本路径是 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文件呢?

Trying to determine where/how to place style sheets in web app
deployed to Tomcat 6 so that the styles.css can be resolved.
I've tried every thing I can think of without success.

I did these tests to ferret out were to put the file but
little has been successful.

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

The contextPath is /microblog
And basepath is http://192.168.5.24:9191/microblog

Here is the test code I am using.

I am using Spring 3. I have a simple 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>

I've placed the styles.css file at many directory locations to see
where it might get resolved but none appear to be found.

In Tomcat 6 the deployed exploded web app directory structure

    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

So how to get Tomcat 6 to resolve .css files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

硪扪都還晓 2024-12-14 19:23:07

相对 CSS 路径可能是完全错误的。使其与域相关而不是与路径相关。在其前面添加上下文路径(在您的情况下为 /microblog):

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

请注意,/WEB-INF 中的资源不可公开访问。它们只能由 Servlet 中的 RequestDispatcher 或 JSP 中的 访问。将它们放在 /WEB-INF 之外。

如果仍然不起作用,则可能是映射到 //* URL 模式的 servlet 或过滤器未正确执行其工作。

Likely the relative CSS path is plain wrong. Make it domain-relative instead of path-relative. Prepend it with the context path (which is /microblog in your case):

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

Note that resources in /WEB-INF are not publicitly accessible. They're only accessible by RequestDispatcher in servlets or by <jsp:include> in JSPs. Put them outside /WEB-INF.

If it still doesn't work, then likely a servlet or filter which is mapped on an URL pattern of / or /* is not doing its job properly.

滿滿的愛 2024-12-14 19:23:07

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

我的 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">

Probably it's quite late but I managed to resolve this in a different way.

I have a simple html file inside my WebContent and a css folder:

this was not working:

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

and neither this:

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

this IS working, but it's ugly (and it's also giving me a warning in Eclipse):

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

so I found that also this one is working, and seems the best approach to me:

<link rel="stylesheet" type="text/css" href="./css/styles.css">
花海 2024-12-14 19:23:07

以下内容均无法部署到 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

您需要为上下文添加前缀path :

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

或完整路径 :

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

/WEB-INF 下的资源应被删除,因为它们不可用于外部请求

none of the following will work deploying to http://192.168.5.24:9191/microblog

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

This one points to http://192.168.5.24:9191/styles.css

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

This one points to http://192.168.5.24:9191/css/styles.css

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

This one points to http://192.168.5.24:9191/css/styles.css

You need to prefix the context path :

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

or the full path :

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

The resources under /WEB-INF should be removed as they're not available for external requests

那些过往 2024-12-14 19:23:07

另请考虑使用 HTML 基本标记,这样您就不需要键入 ${pageContext .request.contextPath} 始终。

Also consider using the HTML base tag so that you don't need to type ${pageContext.request.contextPath} all the time.

笑饮青盏花 2024-12-14 19:23:07

这也发生在我身上。我通过简单地在 css 文件中的任意位置键入一个空格并保存来解决它。路径应该只是“css/style.css”。在我看来,有时如果该文件(或文件的路径)发生更改(例如重命名或重新定位),Tomcat 不会立即识别您的文件。我发现让 Tomcat “意识到”此更改的唯一方法是在文件中进行一些更改并保存它,如果尚未完成此操作,则重新部署或刷新将无济于事。

This happened to me too. I solved it by simply typing a space anywhere in the css file and saving it. The path should just be "css/style.css". It seems to me that sometimes Tomcat wouldn't recognize your file immediately if that file (or the path to the file) is changed such as renamed or relocated. The only way I find to make Tomcat "realize" this change is to make some change in the file and save it, and redeploying or refreshing wouldn't help if this hasn't been done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文