在bean jsf中创建的动态组件中添加事件

时间:2014-02-25 19:04:56

标签: jsf primefaces

美好的一天,所以伙计们,我想要做的是以下内容,我在JSF页面中通过bean添加了一个HtmlSelectOneMenu,当它改变了他的值时喜欢它,HtmlSelectOneMenu他添加另一个,添加HtmlSelectOneMenu'我没有遇到任何问题,但是想知道如何通过BEAN课程ehehee

向他们添加一个事件
HtmlSelectOneMenu select = new HtmlSelectOneMenu();
        HtmlOutputLabel lblData = new HtmlOutputLabel();
        lblData.setValue(list.get(contador).getItem().getNome());
        UISelectItem item = new UISelectItem();
        item.setItemLabel("NÃO");
        item.setItemValue(false);
        UISelectItem item2 = new UISelectItem();
        item2.setItemLabel("SIM");
        item2.setItemValue(true);
        select.getChildren().add(item);
        select.getChildren().add(item2);
        FacesContext context = FacesContext.getCurrentInstance();
        ELContext elContext = context.getELContext();
        Application app = context.getApplication();
        ExpressionFactory expressionFactory = app.getExpressionFactory();
        ValueExpression ve = expressionFactory.createValueExpression(elContext, "#{projetoBean.qualRespondeu}", String.class);
        select.setValueExpression("valeu", ve);
        UIPanel container = (UIPanel) FacesContext.getCurrentInstance().getViewRoot().findComponent(":formProjeto:painel");
        container.getChildren().add(lblData);
        container.getChildren().add(select);
        contador++;

2 个答案:

答案 0 :(得分:1)

感谢Kolossus的回答,但决定如下:

   AjaxBehavior ajax = new AjaxBehavior();
ajax.setAsync(true);
ajax.setUpdate(":formProjeto");
MethodExpression ex = expressionFactory.createMethodExpression(elContext, "#{projetoBean.adicionaPergunta()}", null, new Class[]{});
ajax.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(ex, ex));
select.addClientBehavior("change", ajax);

答案 1 :(得分:0)

请尝试以下步骤:

  1. 创建AjaxBehaviorListener(事件的实际处理):

    MyAjaxBehaviorListener implements AjaxBehaviorListener{
    
          @Override
          public void processAjaxBehavior(AjaxBehaviorEvent e) throws AbortProcessingException {
          //perform the event processing here
    
          }
    
    
    }
    
  2. 将听众附加到AjaxBehavior

     AjaxBehavior ajaxBehavior = (AjaxBehavior) FacesContext.getCurrentInstance().getApplication() .createBehavior(AjaxBehavior.BEHAVIOR_ID);
     MyAjaxBehaviorListener listener = new MyAjaxBehaviorListener();
     ajaxBehavior.setExecute(Collections.singletonList("@this")); //sets the execute attribute of the event handler
     ajaxBehavior.addAjaxBehaviorListener(listener);
    
  3. AjaxBehavior附加到组件

    item.addClientBehavior("change",ajaxBehavior); //set the triggering event to "change"