JSF 2 -- Composite component with optional listener attribute on f:ajax
Asked Answered
S

3

6

I have a composite component that looks something like this:

<!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>

I'd like to be able to add an optional "listener" attribute which if defined would add an event listener to my f:ajax but I'm having trouble figuring out how to accomplish this. Any help would be appreciated.

Shadowy answered 6/7, 2012 at 19:9 Comment(0)
S
17

You need to specify the method-signature attribute of the <cc:attribute> tag in order to treat the attribute value as a method expression. You can use the JSTL view build time tag <c:if> to conditionally add the <f:ajax> tag.

<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>

(the #{not empty cc.attrs.listener} won't work as EL would implicitly evaluate the attribute as a value expression)

Then you can use it as follows:

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

Or when your environment doesn't support EL 2.2, then create a backing component:

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

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

}

which is to be declared and used as

<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>
Sibship answered 6/7, 2012 at 19:23 Comment(6)
ok. i've gotten that far but i want it to be an optional attribute. When it isn't provided by the client of the component i don't want it used by f:ajax. Is there a way to make it optional on f:ajax?Shadowy
Hmm, this won't work that way. The <c:if> attempts to resolve it as a value expression :/ Sorry, I'll have to revert my answer.Sibship
OK. Option 2 doesn't have a beautiful interface but I think I'll opt for that so I can keep the other f:ajax functionality tucked away under the hood.Shadowy
Theoretically, it should be doable when using a "backing component" (<cc:interface componentType>) which has a getter method which checks if the "listener" attribute is been definied so that you can use <c:if test="#{cc.hasListener}"> or so. There is no way to tell EL to not resolve that as a value expression. Even #{not cc.attrs.containsKey('listener')} won't work due to EL resolver of #{cc.attrs}. I have updated the answer.Sibship
Even better, #{not empty cc.getValueExpression('listener')} works for me. Sorry for the many updates, it's also the first time for me :)Sibship
no need to apologize at all. I greatly appreciate your time. The latest solution works great now and the interface is just what I hoped for.Shadowy
T
2

I've the same problems too, and my solution was create default value for the action method. I have only to create a class: MyComponent.java that contains all default methods signature.

<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>
Trifle answered 18/3, 2016 at 8:4 Comment(0)
G
0

All of the above didn't work for me unfortunately, so I fiddled around and came up with the following solution.

<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>

This will always bind the ajax behavior, but only execute it if there actually is a listener.

Golf answered 5/12, 2017 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.