Wednesday, August 1, 2012

programatically add an ajax button

 This seems trivial. But it turns out it is not. It took me about one day to get this correctly.
I highlight the tricky parts I was not correct in the first place.

HtmlCommandButton b = new HtmlCommandButton();
        AjaxBehavior ajax = new AjaxBehavior(); 
        List<String> exs=new LinkedList<String>();
        b.addClientBehavior(b.getDefaultEventName(), ajax);
        return b;

I highlighted three parts.
  1. First, give the button an ID explicitly. This may be a bug in JSF RI implementation. if you want to know more, go here.
  2. Second, the ajax "execute" attribute should include "@this". "@this" keyword informs the server that it should process the button and issues action event to this button. Otherwise, any event listener including ajax event listener will not be called. 
  3. Third, attach the ajax vehavior to button as b.getDefaultEventName() which is "action". I first attached it as "click". "click" is a client-side event name. The logic event name for action is "action", not "click". This is automatically if xhtml file is used. 

No comments:

Post a Comment