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