【问题标题】:JSF-2 Custom tag to change the styleClass for the child's elementJSF-2 自定义标签更改子元素的 styleClass
【发布时间】:2012-08-15 17:59:21
【问题描述】:

我使用 JSF 2。我需要编写一个自定义标记来更改其中消息的样式类。 所以如果我有这样的代码:

<my:errorGrid errorClass="#{richBean.erClass}" >
   <my:inputField id="pid1" value="#{richBean.pid}" fsType="pid">
   </my:inputField>

  <h:message for="pid1" ajaxRendered="true"/>

</my:errorGrid>

错误信息应该与额外的 styleClass 一起输出。此 styleClass 的名称取自自定义标签 my:errorGrid

的属性 errorClass

据我所知,我需要在模板中使用 vcp:body 标记并在 encodeChildren() 方法中处理子项。拜托,谁能给我一些提示如何实现它?也许一些建议或链接。

【问题讨论】:

    标签: jsf jsf-2 tags


    【解决方案1】:

    为什么不使用复合组件?

    errorGrid.xhtml

    <composite:interface>
        <composite:attribute name="errorClass"/>
    </composite:interface>
    
    <composite:implementation>
        <my:inputField id="pid1" value="#{richBean.pid}" fsType="pid">
        </my:inputField>
    
        <h:message for="pid1" ajaxRendered="true" errorClass="#{cc.attrs.errorClass}"/>
    </composite:implementation>
    

    如果你有一个经典组件,你只需要在渲染之前用你的错误类调用HtmlMessage.setErrorClass()。例如:

    for (UIComponent child : getChildren()){
        if(child instanceof HtmlMessage) {
            HtmlMessage msgComp = (HtmlMessage) child;
            msgComp.setErrorClass(getErrorClass());
        }
    }
    

    【讨论】:

    • 感谢您的回答。但它应该是一个带有属性的标签。现在我正在尝试用 encodeChildren() 和 vcp:body 来解决这个问题。但我仍然有一些困难,没有信息/文档......
    • 是一个有属性的组件。你可以像 `. 一样使用它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 2013-04-12
    • 2011-11-15
    相关资源
    最近更新 更多