JSF / Facelets:使用<h:outputstylesheet>标签</h:outputstylesheet>无法识别CSS文件

时间:2012-09-05 12:50:50

标签: html css jsf facelets

我正在使用JSF / Facelets开发一个项目。我想在我的View XHTML上做一些CSS更改,但是当我在Tomcat服务器中部署我的Web应用程序时没有任何反应。我尝试了很多技巧,但我得到了相同的结果。

无论如何,这是我的“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;
}

这是主要模板“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终于来了我的“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>

提前致谢:)

2 个答案:

答案 0 :(得分:49)

<h:outputStylesheet>(和<h:outputScript>)需要<h:head>,但您需要<head>。相应地修复它。

<h:head>
    <h:outputStylesheet name="css/styles.css" />
</h:head>

此外,您需要确保css/styles.css文件已放置在公共网页内容的/resources子文件夹中。

WebContent
 |-- resources
 |    `-- css
 |         `-- styles.css
 :

关于您尝试使用library属性,请注意这一点,在此上下文中使用library="css"并不完全正确。另见:What is the JSF resource library for and how should it be used?

答案 1 :(得分:-4)

WebContent

下添加资源文件夹

和内部资源创建css文件夹

然后访问像这样的文件

您未添加的h:outputStylesheet library="css" name="myNewStylesFile.css" target="head"部分下的

h:head