CSS eclipse动态web项目中,如何将css链接到webcontent文件夹中的jsp文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8395941/
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
In eclipse dynamic web project, how to link css to jsp file in webcontent folder
提问by user1056648
In Eclipse, I created a Dynamic Web Project and a JSP file under WebContent folder. I also created a CSS file under the WebContent folder. Then I use <link rel="stylesheet" type="text/css" href="XXX.css">
in the JSP to link to the CSS file but when I run on web server (Tomcat) the CSS didn't apply. Can someone tell me why?
在 Eclipse 中,我在 WebContent 文件夹下创建了一个动态 Web 项目和一个 JSP 文件。我还在 WebContent 文件夹下创建了一个 CSS 文件。然后我<link rel="stylesheet" type="text/css" href="XXX.css">
在 JSP 中使用链接到 CSS 文件,但是当我在 Web 服务器(Tomcat)上运行时,CSS 不适用。有人能告诉我为什么吗?
回答by kia kaviani
You must put your web project name before the address path of your css file
你必须把你的 web 项目名称放在你的 css 文件的地址路径之前
Example:
例子:
<link rel="stylesheet" href="/YourProjectName/XXX.css" type="text/css">
or in more dynamic way:
或更动态的方式:
<link rel="stylesheet" href="${pageContext.request.contextPath}/XXX.css" />
Have fun :)
玩得开心 :)
回答by Nhat Dinh
You can use: With style.css file in WEB-INF/jsp folder
您可以使用: WEB-INF/jsp 文件夹中的 style.css 文件
<style type="text/css">
<%@include file="css/style.css" %>
</style>
NOTE
笔记
This however copies the entire source of the CSS file into the HTML output of the JSP page. In other words, this is a server-side include, not a client-side resource reference. So you effectively miss the advantage that the browser can cache static resources and this way you end up with a bandwidth waste because the very same CSS file is embedded in every single page. In other words, a bad idea in terms of performance and efficiency.
但是,这会将 CSS 文件的整个源复制到 JSP 页面的 HTML 输出中。换句话说,这是一个服务器端包含,而不是客户端资源引用。所以你实际上错过了浏览器可以缓存静态资源的优势,这样你最终会浪费带宽,因为每个页面都嵌入了完全相同的 CSS 文件。换句话说,就性能和效率而言,这是一个坏主意。
as @BalusC described in comment! you want to test your style.css file anyway, this is a solution.
正如@BalusC 在评论中所描述的那样!无论如何你想测试你的 style.css 文件,这是一个解决方案。
回答by Deepak Singh
you can use
您可以使用
<link rel="stylesheet" type="text/css" href="path/css">
回答by Vaibhav Shah
You should restart eclipse so that it maps all css and javascript files again. I worked for me.
您应该重新启动 eclipse,以便它再次映射所有 css 和 javascript 文件。我为我工作。