简体   繁体   English

动态设置a4j:ajax的事件

[英]Dynamically set the event of an a4j:ajax

When trying to dynamically set the event of an a4j:ajax as follow: 尝试动态设置a4j:ajax event时,如下所示:

<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 : 它似乎来自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. 我想编写自己的CustomBehaviorHandler使其表现为所需的行为。 The question is: how to register this new CustomBehaviorHandler with JSF? 问题是:如何在JSF中注册这个新的CustomBehaviorHandler

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 . 我找不到任何示例或文档,但是似乎有可能,因为PrimeFaces具有自己的org.primefaces.component.behavior.ajax.AjaxBehaviorHandler

I would like to write my own CustomBehaviorHandler to make it behave as I need. 我想编写自己的CustomBehaviorHandler使其表现为所需的行为。 The question is: how to register this new CustomBehaviorHandler with JSF? 问题是:如何在JSF中注册这个新的CustomBehaviorHandler?

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: 如以下答案所示,准备一个*.taglib.xmlJSF中的Custom Facelet组件 (注意:taghandler类不是必需的)并添加以下条目:

<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> . 然后,您将可以使用<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. 您不能简单地从BehaviorHandler扩展,因为它已经在构造过程中引发了异常。


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. 我找不到任何示例或文档,但是似乎有可能,因为PrimeFaces有自己的org.primefaces.component.behavior.ajax.AjaxBehaviorHandler。

That's the <p:ajax> . 那就是<p:ajax> The <a4j:ajax> which you've initially at your hands is also such one, by the way. 顺便说一句,您最初使用的<a4j:ajax>也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM