Java EE应用程序中的样式路径 [英] Style Paths In Java EE Applications

查看:104
本文介绍了Java EE应用程序中的样式路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java EE应用程序,在应用程序中我有以下结构。

I have a Java EE application and in the application I have the following structure.

WEB-INF
    layout
        header.jsp
    styles
        main.css

I想要在 header.jsp 中包含 main.css 。其中我试图做以下(其中...是路径):

I want to include the main.css in the header.jsp. Which I am attempting to do with the following (where ... is the path):

<link rel="stylesheet" href="..."> 

但是,我似乎找不到正确的路径。我尝试了下面的每一个没有运气:

However, I can't seem to get the right path. I have tried each of the following with no luck:

../styles/main.css
/styles/main.css
styles/main.css
/WEB-INF/styles/main.css
WEB-INF/styles/main.css

正确的路径是什么?

推荐答案

/ WEB-INF 文件夹中的资源不能直接公开访问,没有中介(前端控制器)servlet(这也包括JSP文件本身!< jsp:include> 在webserver而不是webbrowser运行)。所以你必须把CSS(和JS和图像)文件放在 / WEB-INF 文件夹之外。

First of all, resources in /WEB-INF folder are not directly publicly accessible without a intermediating (front controller) servlet (this also covers JSP files themselves! the <jsp:include> runs at webserver, not at webbrowser). So you've to put CSS (and JS and image) files outside the /WEB-INF folder.

WebContent
 |-- WEB-INF
 |    `-- layout
 |         `-- header.jsp
 |-- styles
 |    `-- main.css
 :

假设它现在在 /styles/main.css ,那么您可以如下引用它:

Assuming that it's now in /styles/main.css, then you can reference it as follows:

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

使用 $ {pageContext.request.contextPath} (使用前导斜杠打印当前上下文路径,从而使CSS URL相对于域根保证)不是必需的,但是它是您最安全的选择,因为当前请求URL未知。 HTML < link> < script> < img> ; < a> 等等元素,即通过相对于当前请求URL解析的webbrowser(如在浏览器的地址栏)。只要不清楚您的请求网址是什么,我不能回答正确的CSS路径,而不使用 $ {pageContext.request.contextPath}

Using ${pageContext.request.contextPath} (which prints the current context path with a leading slash and thus makes the CSS URL guaranteed relative to the domain root) is not necessary per se, but it is your safest bet as the current request URL is unknown. All relative paths in HTML <link>, <script>, <img>, <a>, etc elements are namely namely by the webbrowser resolved relative to the current request URL (as you see in browser's address bar). As long as it's unclear what your request URL is, I can't answer the right CSS path without using ${pageContext.request.contextPath}.

这篇关于Java EE应用程序中的样式路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆