1

When trying to dynamically set the event of an a4j:ajax as follow:

<a4j:ajax event="#{myBean.event}" listener="#{myBean.listener}" />

I get the following error:

<a4j:ajax> The 'event' attribute for behavior tag must be a literal

It seems to come from javax.faces.view.facelets.BehaviorHandler:

public BehaviorHandler(BehaviorConfig config) {
    super(config);
    this.behaviorId = config.getBehaviorId();
    this.event = this.getAttribute("event");
    if (null != event && !event.isLiteral()) {
        throw new TagException(this.tag, "The 'event' attribute for behavior tag must be a literal");
    }
}

I would like to write my own CustomBehaviorHandler to make it behave as I need. The question is: how to register this new CustomBehaviorHandler with JSF?

Or is there any other way to suit my needs?

I can't find any example nor documentation about it, but it seems to be possible, since PrimeFaces has its own org.primefaces.component.behavior.ajax.AjaxBehaviorHandler.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sp00m
  • 47,968
  • 31
  • 142
  • 252

1 Answers1

2

I would like to write my own CustomBehaviorHandler to make it behave as I need. The question is: how to register this new CustomBehaviorHandler with JSF?

You'd need to create a custom tag for that. Prepare a *.taglib.xml as demonstrated in this answer: Custom Facelet component in JSF (note: taghandler class is not necessary) and add the following entry:

<tag>
    <tag-name>ajax</tag-name>
    <behavior>
        <behavior-id>com.example.MyAjaxBehavior</behavior-id>
        <handler-class>com.example.MyAjaxBehaviorHandler</handler-class>
    </behavior>
    <!-- Define attributes here. -->
</tag>

Then you'll be able to use <my:ajax>. Note: you've got to reimplement a lot in the behavior handler class; you can't simply extend from BehaviorHandler as it throws the exception during its construction already.


Or is there any other way to suit my needs?

Depends on concrete functional requirement for which you thought that this would be the solution. This is unfortunately not stated anywhere in the question, so I can't answer that part.


I can't find any example nor documentation about it, but it seems to be possible, since PrimeFaces has its own org.primefaces.component.behavior.ajax.AjaxBehaviorHandler.

That's the <p:ajax>. The <a4j:ajax> which you've initially at your hands is also such one, by the way.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Wow, you're right, it's harder than I thought... Actually, I find that .xhtml files become quickly overloaded. I wanted to try a [Java-managed View](http://pastebin.com/VqWHFSxi). Then, within the JSF beans, simply [instantiate a new View](http://pastebin.com/GgnmRMwp), and using [custom tags](http://pastebin.com/L8b2bBRc), write [only few lines](http://pastebin.com/7PKHku1M) in your .xhtml files. But it seems to be a bit utopian, and I'm not sure about the benefits of such a system (it was more about the technical challenge actually ;) So if you have any trail... Thanks anyway! – sp00m Aug 30 '13 at 07:53
  • I see. If you go that path, you could perhaps add them programmatically via `ClientBehaviorHolder#addClientBehavior(String, AjaxBehavior)`. – BalusC Aug 30 '13 at 11:08