Html 如何将我的 css 连接到存储在 WEB-INF 文件夹中的 JSP 文件?网络领域/JSP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10355292/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:01:22  来源:igfitidea点击:

how can i connect my css to my JSP files stored in the WEB-INF folder? Websphere/JSP

htmlcssjsp

提问by Jimmy Servlet

I am using ibm websphere and creating a Dynamic web project. All of my JSPfiles are in my WEB-INF folder and i use servlet mapping in my web.xmlfile to make them accessible. This has worked fine so far. however i have problem with my CSS. as always, my CSSfile is located in WebContentin a folder named css. heres my link for my jsp

我正在使用 ibm websphere 并创建一个动态 web 项目。我的所有JSP文件都在我的 WEB-INF 文件夹中,我在我的web.xml文件中使用 servlet 映射 来使它们可访问。到目前为止,这工作得很好。但是我的 CSS 有问题。与往常一样,我的CSS文件位于WebContent名为 css 的文件夹中。这是我的链接jsp

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

I'm having no luck getting my css to show...
what am i missing?

我没有运气让我的 css 显示......
我错过了什么?

回答by BalusC

The relative URLs in the generated HTML output are by the browser interpreted relative to the request URL (as you see in browser's address bar), not to their physical location in the server's disk file system. It's namely the webbrowser who has got to download them by a HTTP request, it's not the webserver who has got to include them from disk somehow.

生成的 HTML 输出中的相对 URL 由浏览器相对于请求 URL(如您在浏览器的地址栏中看到)而不是它们在服务器磁盘文件系统中的物理位置进行解释。即网络浏览器必须通过 HTTP 请求下载它们,而不是网络服务器必须以某种方式从磁盘中包含它们。

One of the ways is to use a domain-relative path for those resources, i.e. start with /. You can use ${pageContext.request.contextPath}to dynamically inline the current webapp's context path.

其中一种方法是对这些资源使用域相关路径,即以/. 您可以使用${pageContext.request.contextPath}动态内联当前 webapp 的上下文路径。

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

This will end up in the generated HTML output as follows:

这将在生成的 HTML 输出中结束,如下所示:

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

This way the browser will be able to download them properly.

这样浏览器就可以正确下载它们。

See also:

也可以看看:

回答by Juan Alberto López Cavallotti

I think you need to see it from the browser's perspective, how it is the URL of the page, the context path and the current path.

我认为你需要从浏览器的角度来看它,它是页面的 URL、上下文路径和当前路径。

If your app context path is for example "myApp" then you can do something like this to make it work:

例如,如果您的应用程序上下文路径是“myApp”,那么您可以执行以下操作以使其工作:

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

If you want to make it relative so it does not depend on the context path, then if your url looks like http://localhost:8080/myApp/myservlet/file.jsp

如果你想让它相对所以它不依赖于上下文路径,那么如果你的 url 看起来像http://localhost:8080/myApp/myservlet/file.jsp

Then your link tag would be

那么你的链接标签将是

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

Firebug or the chrome console may be really helpful to understand what the browser is trying to fetch.

Firebug 或 chrome 控制台可能对了解浏览器试图获取的内容非常有帮助。

Hope this helps!

希望这可以帮助!