如何在Primefaces中为MenuItem调用ActionListener [英] How to invoke ActionListener for MenuItem in primefaces

查看:155
本文介绍了如何在Primefaces中为MenuItem调用ActionListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个动态菜单模型.在actionlistener中,我需要调用backing bean方法.当我将actionListener添加到menuItem时,我得到了java.lang.InstantiationException.

I need to implement a dynamic menumodel. In the actionlistener, i need to call a backing bean method. When I add the actionListener to the menuItem I am getting an java.lang.InstantiationException.

@ManagedBean(name = "sampleBean")  
@ViewScoped  
public class Sample1 implements Serializable {

private MenuModel model;

public Sample1() {
    model = new DefaultMenuModel();

    for(int i = 0; i < 3; i++){
        MenuItem item1 = new MenuItem();
        item1.setValue("test1" + i);
        item1.setAjax(false);
        item1.setId("item1" + i);
        item1.addActionListener(this.new MenuActionListener());
        model.addMenuItem(item1);
    }
}

// inner class action listener
class MenuActionListener implements ActionListener{
    @Override
    public void processAction(ActionEvent arg0) throws AbortProcessingException {
     System.out.println("test... " + arg0.getComponent().getClientId());
     test(arg0.getComponent().getClientId());   
    }
}

public void test(String test){
    System.out.println("tested..." + test); 
}

我也尝试过使用MethodExpressionActionListener.在这种情况下,传递给"item1"的参数为空.请让我知道如何在methodExpression中传递参数.

I have also tried using MethodExpressionActionListener. In this case the parameter passed "item1" is alays null. Please let me know on how I could pass a parameter in a methodExpression.

for(int i = 0; i < 3; i++){
        MenuItem item1 = new MenuItem();
        item1.setValue("test1" + i);
        item1.setAjax(false);
        item1.setId("item1" + i);
        //item1.addActionListener(new MenuActionListener());
        ExpressionFactory factory = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        MethodExpression expression = factory.createMethodExpression(elContext, "#{beanName.method(" + item1 + ")}", null, new Class[] {MenuItem.class});
        item1.addActionListener(new MethodExpressionActionListener(expression));
        model.addMenuItem(item1);
    }  

推荐答案

尝试将MenuActionListener设置为 normal 类,而不是内部类

Try to make the MenuActionListener a normal class instead of inner class

还请看一下使用JSF进行事件驱动的编程

这篇关于如何在Primefaces中为MenuItem调用ActionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆