gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 11:25:49 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com