2013-08-29 24 views
1

當試圖動態設置a4j:ajaxevent如下:動態設置的A4J的事件:AJAX

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

我收到以下錯誤:

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

它似乎來自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"); 
    } 
} 

我想寫我自己的CustomBehaviorHandler以使其表現符合我的需要。問題是:如何使用JSF註冊這個新的CustomBehaviorHandler

或者還有其他方法可以滿足我的需求嗎?

我找不到任何示例或文檔,但它似乎是可能的,因爲PrimeFaces有它自己的org.primefaces.component.behavior.ajax.AjaxBehaviorHandler

回答

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?

您需要爲此創建自定義標籤。準備一個*.taglib.xml在這個答案證明:Custom Facelet component in JSF(注: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> 

然後你就可以使用<my:ajax>。注意:你必須在行爲處理器類中重新實現;你不能簡單地從BehaviorHandler延伸,因爲它已經在建造期間拋出異常。


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.

這就是<p:ajax>。順便說一下,你最初掌握的<a4j:ajax>也是這樣。

+0

哇,你是對的,比我想象的要難...其實,我發現.xhtml文件變得很快被超載。我想嘗試[Java管理的視圖](http://pastebin.com/VqWHFSxi)。然後,在JSF bean中,只需[實例化一個新的視圖](http://pastebin.com/GgnmRMwp),並使用[自定義標籤](http://pastebin.com/L8b2bBRc),寫[只有幾行] (http://pastebin.com/7PKHku1M).xhtml文件中。但它似乎有點烏托邦,我不確定這樣一個系統的好處(它實際上更多的是關於技術挑戰;)所以,如果你有任何線索......無論如何,謝謝! – sp00m

+0

我明白了。如果你走這條路,你可以通過'ClientBehaviorHolder#addClientBehavior(String,AjaxBehavior)'以編程方式添加它們。 – BalusC