2012-07-11 50 views
1

我創建了以下自定義標籤:命令按鈕動作不使用自定義標籤的工作

<h:form> 
<d:formBuilder/>   
</h:form> 

標籤沒有呈現這樣的問題:

my custom tag

標籤代碼:

public class FormBuilder extends TagHandler { 

    public FormBuilder(TagConfig config) { 
     super(config); 
    } 

    public void apply(FaceletContext context, UIComponent parent) throws IOException { 
     CommandButton command = (CommandButton) context.getFacesContext().getCurrentInstance().getApplication().createComponent(CommandButton.COMPONENT_TYPE); 
     command.setValue("Click"); 
     command.setAjax(false); 
     MethodExpression me = context.getExpressionFactory().createMethodExpression(FacesContext.getCurrentInstance().getELContext(), "#{cli.insert}", null, new Class<?>[0]); 
     command.setActionExpression(me); 

     InputText it = (InputText) context.getFacesContext().getCurrentInstance().getApplication().createComponent(InputText.COMPONENT_TYPE);  
     ValueExpression ve1 = context.getExpressionFactory().createValueExpression(FacesContext.getCurrentInstance().getELContext(), "#{cli.name}", String.class);  
     it.setValueExpression("value", ve1);   

     parent.getChildren().clear();  
     parent.getChildren().add(it); 
     parent.getChildren().add(command); 
    } 

} 

託管豆:

@SessionScoped 
@ManagedBean(name = "cli") 
public class ClienteController { 

    private String name = "aa"; 

    public String insert() { 
     name = "test"; 
     return "clientes"; 
    } 
} 

inputText工作正常,但commandButton不執行managedBean的方法!哪裏不對?

謝謝。

回答

0

按鈕曾與代碼:

ExpressionFactory ef = context.getFacesContext().getCurrentInstance().getApplication().getExpressionFactory(); 
MethodExpression me = ef.createMethodExpression(ec, "#{cli.insert}", null, new Class[]{ActionEvent.class}); 
MethodExpressionActionListener meal = new MethodExpressionActionListener(me); 
command.addActionListener(meal); 
command.setType("submit"); 
0

curiosly,我有同樣的問題,但我是使用EL表達式。我解決了刪除標籤「type = button」的問題。我知道,沒有邏輯,但只能刪除標籤。

P.S:Primefaces 3.4