Html JSF/Facelets:使用 <h:outputStylesheet> 标签无法识别 CSS 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12281839/
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
JSF/Facelets : CSS file is not being recognized using <h:outputStylesheet> tag
提问by ayoubuntu
I'm working on a project using JSF/Facelets. I want to do some CSS changes on my View XHTML, but nothing happen when i deploy my web application in my Tomcat Server. I've tried many tricks but i've got the same result.
我正在使用 JSF/Facelets 开发一个项目。我想在我的 View XHTML 上做一些 CSS 更改,但是当我在我的 Tomcat 服务器中部署我的 Web 应用程序时没有任何反应。我尝试了很多技巧,但得到了相同的结果。
Anyway, here's my "styles.css" :
无论如何,这是我的“styles.css”:
body { width: 750px; }
#header
{
width: 100%;
font-size: 36px;
font-weight: bold;
line-height: 48px;
background-color: navy;
color: white;
}
#footer
{
width: 100%;
font-weight: bold;
background-color: navy;
color: white;
}
And this is the main template "Template.html" including "Header.html" and "Footer.html", where i put my "styles.css" using the tag :
这是主模板“Template.html”,包括“Header.html”和“Footer.html”,我在其中使用标签放置了我的“styles.css”:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<head>
<h:outputStylesheet name="css/styles.css" />
<!-- i've also tried this one, using the "library" attribute -->
<!--
<h:outputStylesheet library="css" name="styles.css" />
-->
</head>
<h:body>
<h:panelGroup id="page" layout="block">
<h:panelGroup id="header" layout="block">
<ui:insert name="header">
<ui:include src="Header.html" />
</ui:insert>
</h:panelGroup>
<h:panelGroup id="container" layout="block">
<h:panelGroup id="content" layout="block">
<ui:insert name="content">CONTENT</ui:insert>
</h:panelGroup>
</h:panelGroup>
<h:panelGroup id="footer" layout="block">
<ui:insert name="footer">
<ui:include src="Footer.html" />
</ui:insert>
</h:panelGroup>
</h:panelGroup>
</h:body>
</html>
Anf finally here's my "Main.xhtml" which include the template "Template.html" :
Anf 最后是我的“Main.xhtml”,其中包括模板“Template.html”:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich" template="Template.html">
<h:body>
<ui:define name="content">
<h:form>
<h:inputText title="inputText"></h:inputText>
<h:commandButton value="OK"></h:commandButton>
</h:form>
</ui:define>
</h:body>
</ui:composition>
Thanks in advance :)
提前致谢 :)
回答by BalusC
The <h:outputStylesheet>
(and <h:outputScript>
) requires a <h:head>
, but you've there a <head>
. Fix it accordingly.
在<h:outputStylesheet>
(和<h:outputScript>
)要求<h:head>
,但你有没有<head>
。相应地修复它。
<h:head>
<h:outputStylesheet name="css/styles.css" />
</h:head>
Further, you need to ensure that the css/styles.css
file is been placed in the /resources
subfolder of the public webcontent.
此外,您需要确保css/styles.css
文件被放置在/resources
公共网页内容的子文件夹中。
WebContent
|-- resources
| `-- css
| `-- styles.css
:
As to your attempt to use the library
attribute, be careful with this, using library="css"
isn't entirely correct in this context. See also: What is the JSF resource library for and how should it be used?
至于您尝试使用该library
属性,请注意这一点,library="css"
在这种情况下使用并不完全正确。另请参阅:JSF 资源库的用途是什么,应该如何使用?
回答by Aman birjpuriya
Add resourcesfolder under the WebContent
在WebContent下添加资源文件夹
and inside resources create css folder
并在资源内部创建 css 文件夹
then access the files like this
然后像这样访问文件
h:outputStylesheet library="css" name="myNewStylesFile.css" target="head"
under h:head
section that you did not add
h:outputStylesheet library="css" name="myNewStylesFile.css" target="head"
在h:head
您没有添加的部分下