如何以编程方式或动态地添加AJAX监听到JSF 2的组成部分?组成部分、方式、动态、AJAX

2023-09-10 19:00:02 作者:人间的理想

如何以编程方式或动态地添加AJAX监听器JSF 2成分? 我想:

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!!! ");
    }
  }

但THID不起作用。在页面上,我得到这个JS异常:

But thid doesn't work. In page I've got this JS exception:

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

我发现这个例子RichFaces的: http://community.jboss.org/message/611571

I found this example in RichFaces: http://community.jboss.org/message/611571

但在AjaxBehavior类的普通JSF 2没有

But in plain JSF 2 in AjaxBehavior class there is no

addAjaxBehaviorListener(MethodExpression methodExpression) 

仅存在

addAjaxBehaviorListener(ValueExpression valueExpression) 

我试图来解决这个很多天。照片 请帮助。 非常感谢你。

I'm trying to solve this for many days. Please help. Thank you very much.

推荐答案

好了,所以我做了更多的研究和发现,如果组件中,我wan't到添加AJAX监听器也动态添加(从Java - 就像 parant.getChildren()加(组件))到网页,一切工作正常。

OK, so 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.

但是,如果组件中,我wan't添加AJAX监听器静态加入(JSP中 - 如< H:panelGroup中结合=#{bean.component}> )插入到页面,上述奇怪抛出异常:

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

我会试着问在JSF论坛这个问题。可能是它在Mojara的错误。

I will try to ask this question in JSF forum. May be it's a bug in Mojara.