CommandButton操作不适用于自定义标记

时间:2012-07-11 03:44:58

标签: java jsf java-ee primefaces custom-tags

我创建了以下自定义标记:

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

}

托管bean:

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

    private String name = "aa";

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

inputText正常工作,但commandButton不执行managedBean的方法!怎么了?

感谢。

3 个答案:

答案 0 :(得分:0)

这个答案可以帮助你...... why command button not getting invoked?

答案 1 :(得分: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" );

答案 2 :(得分:0)

好奇,我有同样的问题,但我正在使用EL表达。我解决了删除标签“type = button”的问题。我知道,没有逻辑,只能删除标签。

P.S:Primefaces 3.4

相关问题