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

标签 css tomcat

尝试确定在网络应用程序中放置样式表的位置/方式 部署到 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

上下文路径是/microblog 基本路径是 http://192.168.5.24:9191/microblog

这是我使用的测试代码。

我正在使用 Spring 3。我有一个简单的 JSP

-- 测试.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 中部署的 exploded 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文件呢?

最佳答案

可能相对 CSS 路径是完全错误的。使其与域相关而不是与路径相关。在它前面加上上下文路径(在你的例子中是 /microblog):

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

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

如果它仍然不起作用,则可能是一个 servlet 或过滤器映射到 / 的 URL 模式上或 /*没有正确地完成它的工作。

关于css - 为什么 Tomcat 6 找不到我的 styles.css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7636360/

相关文章:

java - 是否可以使用 JSP/JSTL 生成动态 css/javascript 文件?

jquery - 如何制作一个宽度动态调整的html表格

javascript - 使用 Javascript 更改 PHP 中元素的样式

java - 无法为连接 URL ' ' : I do not understand this exception 创建类 'null' 的 JDBC 驱动程序

html - 向下滚动页面时导航菜单重叠

javascript - YOUTUBE 在移动设备上自动播放

tomcat - 带有 JSF2 的 Grails : WAR cannot run on Tomcat 6

spring - org.springframework.aop.framework.Cglib2AopProxy WARN - 无法代理方法

java - XSD 断言可以用于指定 WSDL 文件吗

java - React.js 服务器端渲染与 Java [无 Node.js]