为什么Tomcat 6 找不到我的styles.css
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7636360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't Tomcat 6 find my styles.css
提问by user976831
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.
试图确定在部署到 Tomcat 6 的 Web 应用程序中放置样式表的位置/方式,以便可以解析 styles.css。我已经尝试了所有我能想到的方法,但都没有成功。
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
上下文路径为 /microblog 基本路径为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
我正在使用 Spring 3。我有一个简单的 JSP
-- test.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>
I've placed the styles.css file at many directory locations to see where it might get resolved but none appear to be found.
我已将styles.css 文件放在许多目录位置,以查看它可能在何处得到解决,但似乎没有找到。
In Tomcat 6 the deployed exploded web app directory structure
在 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
So how to get Tomcat 6 to resolve .css files?
那么如何让 Tomcat 6 解析 .css 文件呢?
回答by BalusC
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):
可能相对的 CSS 路径是完全错误的。使其与域相关,而不是与路径相关。在它前面加上上下文路径(/microblog
在你的情况下):
<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
.
请注意,中的资源/WEB-INF
不可公开访问。它们只能RequestDispatcher
在 servlet 或<jsp:include>
JSP 中访问。把它们放在外面/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.
如果它仍然不起作用,那么很可能是一个 servlet 或过滤器映射到 的 URL 模式/
或/*
没有正确执行其工作。
回答by Enrichman
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:
我的 WebContent 中有一个简单的 html 文件和一个 css 文件夹:
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):
这是有效的,但它很丑(而且它也在 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">
回答by foa
none of the following will work deploying to http://192.168.5.24:9191/microblog
以下任何一项都无法部署到 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
这个指向 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
这个指向 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
这个指向 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
/WEB-INF 下的资源应删除,因为它们不可用于外部请求
回答by NS du Toit
回答by Ivan Huang
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.
这也发生在我身上。我通过简单地在 css 文件中的任何位置键入一个空格并保存它来解决它。路径应该只是“css/style.css”。在我看来,如果该文件(或文件的路径)被更改,例如重命名或重新定位,有时 Tomcat 不会立即识别您的文件。我发现让 Tomcat“意识到”这个更改的唯一方法是在文件中进行一些更改并保存它,如果没有这样做,重新部署或刷新将无济于事。