5

我有一个看起来像这样的复合组件:

<!DOCTYPE html>
<html 
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:dm="http://davemaple.com/dm-taglib"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:cc="http://java.sun.com/jsf/composite"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:a4j="http://richfaces.org/a4j">

<cc:interface>
    <cc:attribute name="styleClass" />
    <cc:attribute name="textBoxStyleClass" />
    <cc:attribute name="inputTextId" />
    <cc:attribute name="labelText" />
    <cc:attribute name="tabindex" />
    <cc:attribute name="required" default="false" />
    <cc:attribute name="requiredMessage" />
    <cc:attribute name="validatorId" />
    <cc:attribute name="converterId" />
    <cc:attribute name="title"/>
    <cc:attribute name="style"/>
    <cc:attribute name="unicodeSupport" default="false"/>
    <cc:attribute name="tooltip" default="false"/>
    <cc:attribute name="tooltipText" default=""/>    
    <cc:attribute name="tooltipText" default=""/>
    <cc:attribute name="onfail" default=""/>
    <cc:attribute name="onpass" default=""/>
</cc:interface>

<cc:implementation>

        <ui:param name="converterId" value="#{! empty cc.attrs.converterId ? cc.attrs.converterId : 'universalConverter'}" />
        <ui:param name="validatorId" value="#{! empty cc.attrs.validatorId ? cc.attrs.validatorId : 'universalValidator'}" />
        <ui:param name="component" value="#{formFieldBean.getComponent(cc.attrs.inputTextId)}" />
        <ui:param name="componentValid" value="#{((facesContext.maximumSeverity == null and empty component.valid) or component.valid) ? true : false}" />
        <ui:param name="requiredMessage" value="#{! empty cc.attrs.requiredMessage ? cc.attrs.requiredMessage : msg['validation.generic.requiredMessage']}" />
        <ui:param name="clientIdEscaped" value="#{fn:replace(cc.clientId, ':', '\\\\\\\\:')}" />

        <h:panelGroup layout="block" id="#{cc.attrs.inputTextId}ValidPanel" style="display:none;">
            <input type="hidden" id="#{cc.attrs.inputTextId}Valid" value="#{componentValid}" />
        </h:panelGroup>

        <dm:outputLabel for="#{cc.clientId}:#{cc.attrs.inputTextId}" id="#{cc.attrs.inputTextId}Label">#{cc.attrs.labelText}</dm:outputLabel>
        <dm:inputText 
            styleClass="#{cc.attrs.textBoxStyleClass}"
               tabindex="#{cc.attrs.tabindex}"
               id="#{cc.attrs.inputTextId}"
               required="#{cc.attrs.required}"
               requiredMessage="#{requiredMessage}"
               title="#{cc.attrs.title}"
               unicodeSupport="#{cc.attrs.unicodeSupport}">
            <f:validator validatorId="#{validatorId}" />
            <f:converter converterId="#{converterId}" />
            <cc:insertChildren />
            <f:ajax 
                event="blur" 
                execute="@this" 
                render="#{cc.attrs.inputTextId}ValidPanel #{cc.attrs.inputTextId}Msg" 
                onevent="on#{cc.attrs.inputTextId}Event" />
        </dm:inputText>
        <rich:message for="#{cc.clientId}:#{cc.attrs.inputTextId}" id="#{cc.attrs.inputTextId}Msg" style="display: none;" />
        <script>
            function on#{cc.attrs.inputTextId}Event(e) {
                if(e.status == 'success') {
                    $('##{clientIdEscaped}\\:#{cc.attrs.inputTextId}').trigger($('##{cc.attrs.inputTextId}Valid').val()=='true'?'pass':'fail');
                }
            }
            $('##{clientIdEscaped}\\:#{cc.attrs.inputTextId}').bind('fail', function() {
                $('##{clientIdEscaped}\\:#{cc.attrs.inputTextId}, ##{clientIdEscaped}\\:#{cc.attrs.inputTextId}Label, ##{cc.attrs.inputTextId}Msg, ##{cc.id}Msg').addClass('error');
                $('##{cc.id}Msg').html($('##{clientIdEscaped}\\:#{cc.attrs.inputTextId}Msg').html());
                #{cc.attrs.onfail}
            }).bind('pass', function() {
                $('##{clientIdEscaped}\\:#{cc.attrs.inputTextId}, ##{clientIdEscaped}\\:#{cc.attrs.inputTextId}Label, ##{cc.attrs.inputTextId}Msg, ##{cc.id}Msg').removeClass('error');
                $('##{cc.id}Msg').html($('##{clientIdEscaped}\\:#{cc.attrs.inputTextId}Msg').html());
                #{cc.attrs.onpass}
            });
        </script>
        <a4j:region rendered="#{facesContext.maximumSeverity != null and !componentValid}">
            <script>
                $(document).ready(function() { 
                    $('##{clientIdEscaped}\\:#{cc.attrs.inputTextId}').trigger('fail');
                });
            </script>
        </a4j:region>
</cc:implementation>

</html>

我希望能够添加一个可选的“listener”属性,如果定义了该属性,它将向我的 f:ajax 添加一个事件侦听器,但我无法弄清楚如何实现这一点。任何帮助,将不胜感激。

4

3 回答 3

16

您需要指定标签的method-signature属性,<cc:attribute>以便将属性值视为方法表达式。您可以使用 JSTL 视图构建时间标记<c:if>来有条件地添加<f:ajax>标记。

<cc:interface>
    <cc:attribute name="listener" method-signature="void listener()" />
</cc:interface>
<cc:implementation>
    <h:someComponent>
        <c:if test="#{cc.getValueExpression('listener') != null}">
            <f:ajax listener="#{cc.attrs.listener}" />
        </c:if>
    </h:someComponent>
</cc:implementation>

(这#{not empty cc.attrs.listener}不起作用,因为 EL 会隐式地将属性评估为值表达式)

然后你可以按如下方式使用它:

<my:someComposite listener="#{bean.listener}" />

或者当您的环境不支持 EL 2.2 时,请创建一个支持组件:

@FacesComponent("someComponent")
public class SomeComponent extends UINamingContainer {

    public boolean isHasListener() {
        return getValueExpression("listener") != null;
    }

}

这将被声明并用作

<cc:interface type="someComponent">
    <cc:attribute name="listener" method-signature="void listener()" />
</cc:interface>
<cc:implementation>
    <h:someComponent>
        <c:if test="#{cc.hasListener}">
            <f:ajax listener="#{cc.attrs.listener}" />
        </c:if>
    </h:someComponent>
</cc:implementation>
于 2012-07-06T19:23:17.810 回答
2

我也有同样的问题,我的解决方案是为 action 方法创建默认值。我只需要创建一个类:MyComponent.java包含所有默认方法签名。

<cc:interface>
    <cc:attribute name="listener" method-signature="void listener()"
                  default="#{myComponent.doNothing()}" />
</cc:interface>
<cc:implementation>
    <h:someComponent>
        <f:ajax listener="#{cc.attrs.listener}" />
    </h:someComponent>
</cc:implementation>
于 2016-03-18T08:04:42.587 回答
0

不幸的是,以上所有方法都对我不起作用,所以我摆弄了一下,想出了以下解决方案。

<cc:interface type="someComponent">
    <cc:attribute name="listener" method-signature="void listener()" />
</cc:interface>
<cc:implementation>
    <h:someComponent>
        <p:ajax listener="#{cc.attrs.listener}" onstart="#{cc.attrs.containsKey('listener') ? '' : 'return false'}" />
    </h:someComponent>
</cc:implementation>

这将始终绑定 ajax 行为,但只有在实际存在侦听器时才执行它。

于 2017-12-05T20:46:19.183 回答