2012-05-03 55 views
2

我正嘗試使用輸入文本和命令按鈕創建動態窗體。一切正常。但是當我點擊命令按鈕時,動作監聽器從不被調用。請建議我做錯了什麼,或者這是PF還是Mojarra的錯誤。代碼如下以編程方式在Primefaces中創建命令按鈕

panel = new Panel(); 
panel.setHeader("Test"); 

InputText text = new InputText(); 

final String binding = "#{roleCreateForm.role.name}"; 

text.setValueExpression("value", 
      createValueExpression(binding, String.class)); 

panel.getChildren().add(text); 

CommandButton button = new CommandButton(); 
button.setValue("Save"); 

MethodExpression me = createMethodExpression("#{roleCreateForm.save}"); 

button.addActionListener(new MethodExpressionActionListener(me)); 

panel.getChildren().add(button); 

另外,createXXXExpression低於

private MethodExpression createMethodExpression(String action) { 
    final Class<?>[] paramTypes = new Class<?>[0]; 

    MethodExpression methodExpression = getExpressionFactory() 
    .createMethodExpression(getELContext(),action, null, paramTypes); 

    return methodExpression; 
} 

private ValueExpression createValueExpression(String binding, 
    Class<String> clazz) { 
    final ValueExpression ve = getExpressionFactory() 
     .createValueExpression(getELContext(), binding, String.class); 
    return ve; 
} 


public static ELContext getELContext() { 
    return FacesContext.getCurrentInstance().getELContext(); 
} 

public static ExpressionFactory getExpressionFactory() { 
    return getApplication().getExpressionFactory(); 
} 

public static Application getApplication() { 
    return FacesContext.getCurrentInstance().getApplication(); 
} 

我的form bean低於

public void save() { 
    logger.info("Saving role - {}" , role); 
} 

我使用 Primefaces 3.2,鑽嘴魚科2.1.7,Tomcat的7, JDK 6,Ubuntu 11

這是我修改後的代碼 是的,我看到你已經指出這是常見的錯誤。但這裏是我修改後的代碼。這也不起作用。

public Panel getPanel() { 
    if (panel == null) { 
    panel = new Panel(); 
    panel.setHeader("Test"); 
    panel.setId("dynapanel"); 

    InputText text = new InputText(); 
    text.setId("dynatext"); 

    final String binding = "#{roleCreateForm.role.name}"; 

    text.setValueExpression("value", createValueExpression(binding, String.class)); 

    panel.getChildren().add(text); 

    CommandButton button = new CommandButton(); 
    button.setValue("Save"); 

    MethodExpression me = getExpressionFactory().createMethodExpression(getELContext(), "#{roleCreateForm.save}", void.class, new Class[0]); 
    AjaxBehavior ajaxBehavior = new AjaxBehavior(); 
    //ajaxBehavior.setListener(me); 
    ajaxBehavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(me)); 
    button.addClientBehavior("submit", ajaxBehavior); 


    panel.getChildren().add(button); 

    } 
    return panel; 
}    
+0

T他的面板在表單標籤內部。表單標籤位於模板xhtml中。這是否會對這種奇怪的行爲做任何事情 – dhrubo

+0

只需使用XHTML而不是Java。 – BalusC

回答

-1

至於我記得,如果你要調用一個方法在支撐bean,使用MethodExpression的爲您AjaxBehaviour的監聽器:

 AjaxBehavior ab1 = new AjaxBehavior(); 
     ExpressionFactory ef = ctx.getApplication().getExpressionFactory(); 
     MethodExpression me1 = ef.createMethodExpression(ctx.getELContext(), 
            expression,//Your ELExpression #{roleCreateForm.save} 
            expectedReturnType, //In your case null 
            expectedParamTypes); //If you receive parameters put new Class[]{Object.class}); 
     ab1.setListener(me1); 
     button.addClientBehavior("submit", ab1); 
+0

不幸的是,這也不起作用。我稍微改變了一下策略。 – dhrubo

+0

你解決了嗎?好吧,我有一個類似的問題,我有更多的步驟,以解決它(我發現這個問題在primefaces論壇的解決方法)。看看這裏:http://stackoverflow.com/questions/ 15808956 /如何以編程方式添加一個ajaxbehavior到一個uicomponent與primefaces – Bardelman

-1
CommandButton btn = ((CommandButton) FacesContext.getCurrentInstance().getViewRoot().findComponent("full id of button")); 

try{ 
    FacesContext context = FacesContextWrapper.getCurrentInstance(); 
    MethodExpressionActionListener methodExpression = new MethodExpressionActionListener(context.getApplication().getExpressionFactory() 
       .createMethodExpression(context.getELContext(),"#{bean.method}", null, new Class[] {ActionEvent.class})); 

    btn.addActionListener(methodExpression); 
}catch(Exception exc){ 
    exc.printStackTrace(); 
} 

和createMethodExpression:

public static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes) { 
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    return facesContext.getApplication().getExpressionFactory().createMethodExpression(
     facesContext.getELContext(), expression, returnType, parameterTypes); 
} 

這適用於我;)

+0

這不是創建一個按鈕以編程方式(如問題),但添加功能,已經可用的按鈕 – Kukeltje

+1

「一切工作正常。但是當我點擊命令按鈕時,動作偵聽器從來沒有被調用。請建議我做錯了什麼「...我認爲添加偵聽器到新按鈕是相同的 –

相關問題