webtier@glassfish.java.net

JSF sdding ajax listener (f:ajax ) to JSF 2 component dynamically (from java)

From: <forums_at_java.net>
Date: Tue, 23 Aug 2011 14:48:00 -0500 (CDT)

 How to programmatically or dynamically add ajax listener to JSF 2
component?

 

I tried:

  ...
  FacesContext facesContext = FacesContext.getCurrentInstance();
  AjaxBehavior dragStart = (AjaxBehavior)facesContext.getApplication().
createBehavior(AjaxBehavior.BEHAVIOR_ID);
  dragStart.addAjaxBehaviorListener(new DragEnterListener());
  dragStart.setTransient(true);
  component.addClientBehavior("dragstart", dragStart);
  ...
  public class DragEnterListener implements AjaxBehaviorListener {
    @Override
    public void processAjaxBehavior(AjaxBehaviorEvent event) throws
AbortProcessingException {
      System.out.println("AjaxListener CALLED!!! ");
    }
  }
But thid doesn't work. In page I've got this JS exception when I try to
interact with page:

serverError: class java.lang.IllegalStateException  
cz.boza.formcreator.FormModel$DragEnterListener
I found this example in RichFaces: http://community.jboss.org/message/611571
[1]

But in plain JSF 2 in AjaxBehavior class there is no

addAjaxBehaviorListener(MethodExpression methodExpression)
there is only

addAjaxBehaviorListener(ValueExpression valueExpression)
 

I made more research and found that if the /component/ in which I wan't to
add ajax listener is also added dynamically (from java - like
/parant.getChildren().add(component)/) into the page, everything works fine.

 

But if /component/ in which I wan't to add ajax listener is is added
statically (in jsp - like/ <h:panelGroup binding="#{bean.component}">/) into
the page, aforementioned weird exception is thrown:

serverError: class java.lang.IllegalStateException
cz.boza.formcreator.FormModel$DragEnterListener
 

If I look in to source code of resulting page, generated code (JS call) looks
same for both cases:

<span id="j_idt7:pokusny" name="j_idt7:pokusny" class="pokus" style="border:
1px solid black;" *draggable="true"*
*ondragstart="mojarra.ab('j_idt7:pokusny',event,'dragstart',0,'j_idt7:pokuss')"*>
<span id="j_idt7:j_id5" name="j_idt7:j_id5" style="border: 1px solid black;"
*draggable="true"
ondragstart="mojarra.ab('j_idt7:j_id5',event,'dragstart',0,'j_idt7:pokuss')"*>
So, am I doing something wrong? Or is it impossible to add ajax listener to
component in jsf page that is not added dynamically in to the page? May be
it's a bug in Mojara.

I'm working on the project which highly depends on dynamic page construction
(from java). User can construct pages how he want's it. I must say that JSF 2
are not very friendly to this kind of usage. There are also some problems
with dynamic nesting of composite components. I had to turn back to old
component construction :(.

Thank you very much.

 

 

 


[1] http://community.jboss.org/message/611571

--
[Message sent by forum member 'Infragile']
View Post: http://forums.java.net/node/836648