【问题标题】:How to invoke ActionListener for MenuItem in primefaces如何在 primefaces 中为 MenuItem 调用 ActionListener
【发布时间】:2012-10-18 02:07:27
【问题描述】:

我需要实现一个动态菜单模型。在 actionlistener 中,我需要调用一个支持 bean 方法。当我将 actionListener 添加到 menuItem 时,我得到一个 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 中传递参数。

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 成为普通类而不是内部类 javabeat.net/2007/11/event-driven-programming-with-jsf
  • 我可以,但我想从内部类执行一个 bean 方法“test”。我不能那样做吗?
  • 使 MenuActionListener 成为一个普通的类是可行的。非常感谢@Daniel

标签: jsf-2 primefaces


【解决方案1】:

尝试让 MenuActionListener 成为一个普通类而不是内部类

也看看这个Event Driven Programming with JSF

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    相关资源
    最近更新 更多