从bean在JSF页面中创建CSS样式

从bean在JSF页面中创建CSS样式,jsf,primefaces,Jsf,Primefaces,我有一个JSF页面,它是从一个模板创建的,我想从我的支持bean中添加CSS样式,因为一些CSS属性(如颜色等)是从配置数据库读取的。这些颜色通过{bean.getRowStyleClass(item)}用于p:dataTable中的rowStyleClass。仅当样式标记在页面中硬编码时,此操作才有效。如果从支持bean中检索样式标记,则返回的字符串将作为普通字符串打印在页面上 在中提到,bean可以使用return.redBackgroundColor{background color:#F

我有一个JSF页面,它是从一个模板创建的,我想从我的支持bean中添加CSS样式,因为一些CSS属性(如颜色等)是从配置数据库读取的。这些颜色通过
{bean.getRowStyleClass(item)}
用于
p:dataTable
中的
rowStyleClass
。仅当样式标记在页面中硬编码时,此操作才有效。如果从支持bean中检索样式标记,则返回的字符串将作为普通字符串打印在页面上

在中提到,bean可以使用
return.redBackgroundColor{background color:#FF0000;!important;}“
动态创建选择器,bean可以创建完整的css部分(我在
#{bean.styles}
中这样做),但这也会导致一个没有彩色行的数据表

template.xhtml

<html>
    <h:head>...</h:head>
    <h:body>
        <ui:insert name="styles"/>
        <ui:insert name="content"/>
    </h:body>
</html>

...
page.xhtml

<html>
    <ui:composition template="/WEB-INF/template.xhtml">
        <ui:define name="styles">
            <!-- this works as expected -->
            <style type="text/css">
                .redBackgroundColor   { background-color: #FF0000; !important; }
                .blueBackgroundColor  { background-color: #00FF00; !important; }
                .greenBackgroundColor { background-color: #0000FF; !important; }
            </style>
            <!-- this does not work - it prints the string on the page --> 
            #{bean.styles}
        </ui:define>
        <ui:define name="content">
            <!-- here comes the page content -->
            <p:dataTable var="item"
                         value={#bean.values}
                         rowStyleClass="#{bean.getRowStyleClass(item)}">
                ...
            </p:dataTable>
        </ui:define>
    </ui:composition>
</html>

.redBackgroundColor{背景色:#FF0000;!重要;}
.blueBackgroundColor{背景色:#00FF00;!重要;}
.greenBackgroundColor{背景色:#0000FF;!重要;}
#{bean.styles}
...
java

public class Bean {

    public String getStyles() {
        return new StringBuffer()
            .append("<style type=\"text/css\">")
            .append(".redBackgroundColor   { background-color: #FF0000; !important; }")
            .append(".blueBackgroundColor  { background-color: #00FF00; !important; }")
            .append(".greenBackgroundColor { background-color: #0000FF; !important; }") 
            .toString();
    }

    public String getRowStyleClass(Item item) {
        if (condition1) {
            return "redBackgroundColor";
        }
        if (condition2) {
            return "blueBackgroundColor";
        }
        if (condition3) {
            return "greenBackgroundColor";
        }
        return null;
    }

}
公共类Bean{
公共字符串getStyles(){
返回新的StringBuffer()
.附加(“”)
.append(“.redBackgroundColor{背景色:#FF0000;!重要;}”)
.append(“.blueBackgroundColor{背景色:#00FF00;!重要;}”)
.append(“.greenBackgroundColor{背景色:#0000FF;!重要;}”)
.toString();
}
公共字符串getRowStyleClass(项){
如果(条件1){
返回“redBackgroundColor”;
}
如果(条件2){
返回“blueBackgroundColor”;
}
如果(条件3){
返回“greenBackgroundColor”;
}
返回null;
}
}
那么,如何在我的支持bean中创建样式并在
p:dataTable rowStyleClass
属性中使用它们呢

我在Wildfly 10.0.0.Final上使用Primefaces 6.0


任何提示欢迎-谢谢

当我将页面更改为:

    <style type="text/css">
        #{bean.styles}
    </style>

当我将页面更改为:

    <style type="text/css">
        #{bean.styles}
    </style>

发布您的HTML输出。似乎您缺少样式结束标记发布您的HTML输出。似乎您缺少样式结束标记您是否尝试使用样式结束标记?可能是个愚蠢的问题,但是,如果有一个全局CSS文件,其中包含所有默认的背景颜色,并且通过创建您确定要显示的CSS,那么它有什么坏处呢?@Jaqen:缺少的endtag是由于复制粘贴错误造成的,在实际代码中它是存在的!太棒了,我一直在找这个。你有没有试过使用样式结束标记?也许是个愚蠢的问题,但是如果有一个带有所有默认背景颜色的全局CSS文件,并且创建了你想要显示的CSS,那会有什么伤害?@Jaqen:缺少的结束标记是由于复制粘贴错误造成的-在真实的代码中它是存在的!太棒了我一直在找这个