我正在开发一个p:menuButton
页面,其中动态填充了MenuItem元素。菜单项已成功填充,但问题是,当我单击它们时,预期的bean方法不会被触发。当我尝试使用<p:menuItem/>
创建静态菜单项时,预期的方法会按预期启动。
在我的HXTML上:
<h:form id="menuButtonForm" binding="#{taskMenuDefinitionRequestBean.menuButtonForm}">
<p:growl id="messages" showDetail="false"/>
<p:menuButton value="Action" binding="#{customTaskMenuDefinitionBean.menuButton}">
<p:menuitem value="Plain Action" action="#{customTaskMenuDefinitionBean.action}"/>
<p:menuitem value="Test Action" action="#{customTaskMenuDefinitionBean.testAction('1', '2')}"/>
</p:menuButton>
</h:form>
在我的支持bean上:
public TaskMenuDefinitionExtensionPageBean() {
super();
//some prior logic here
try {
List<TaskMenuDefinition> menuList =
getTaskMenuDefinitionService().getMenusByTaskStatus(taskCode,
taskStatusCode, Romp2Constants.MENU_BUTTON_ACTN, user);
if(!menuList.isEmpty()) {
Iterator iterator = menuList.iterator();
while(iterator.hasNext()) {
TaskMenuDefinition taskMenu = (TaskMenuDefinition) iterator.next();
String expression = //"#{taskMenuDefinitionRequestBean.actionEvent}";
String.format("#{taskMenuDefinitionRequestBean.testAction('%s','%s')}",
taskMenu.getNextTaskCode(), taskMenu.getNextTaskStatusCode());
/*MethodExpression actionExpression =
createMethodExpression(expression, Void.TYPE, new Class[]{ActionEvent.class});
MethodExpressionActionListener actionListener =
createMethodActionListener(expression, Void.TYPE, new Class[]{ActionEvent.class});*/
MethodExpression actionExpression =
createMethodExpression(expression, Void.TYPE, new Class[]{String.class, String.class});
MethodExpressionActionListener actionListener =
createMethodActionListener(expression, Void.TYPE, new Class[]{String.class, String.class});
UIMenuItem menuItem = //new UIMenuItem();
(UIMenuItem) getApplication().createComponent(UIMenuItem.COMPONENT_TYPE);
menuItem.setId(taskMenu.getTaskCode() + "_" + taskMenu.getTaskStatusCode()
+ "_" + taskMenu.getId().toString());
menuItem.setValue(taskMenu.getMenuDisplayTxt());
menuItem.setActionExpression(actionExpression);
menuItem.addActionListener(actionListener);
menuItem.getAttributes().put("taskCode", taskCode);
log.info("menuItem command: " + menuItem.getCommand());
log.info("menuItem process: " + menuItem.getProcess());
log.info("actionExpression.getExpressionString: " + actionExpression.getExpressionString());
MethodInfo method = actionExpression.getMethodInfo(getELContext());
if(method != null) {
log.info("actionExpression.getExpressionString: " + method.getName());
}
//menuItem.setTransient(true);
}
}
} catch (Exception e) {
log.error("Caught Exception: " + e.getMessage(), e);
}
}
protected MethodExpression createMethodExpression(String expression, Class<?> returnType,
Class<?>[] parameterTypes) {
return getApplication().getExpressionFactory().createMethodExpression(
getELContext(), expression, returnType, parameterTypes);
}
protected MethodExpressionActionListener createMethodActionListener(String valueExpression,
Class<?> expectedReturnType, Class<?>[] expectedParamTypes) {
MethodExpressionActionListener actionListener =
new MethodExpressionActionListener(createMethodExpression(
valueExpression, expectedReturnType, expectedParamTypes));
return actionListener;
}
答案 0 :(得分:0)
不使用menuButton中的绑定属性而是使用model属性并将其指向org.primefaces.model.menu.MenuModel
model="#{bean.model}"
bean声明中的
private MenuModel model;
public MenuModel getModel() {
return model;
}
public void setUserMenuModel(MenuModel model) {
this.model = model;
}
在你的postConstruct中有
model = new DefaultMenuModel();
DefaultMenuItem item = new DefaultMenuItem("Choose my answer");
item.setCommand("#{customTaskMenuDefinitionBean.action}");
item.setParam("Something u want to pass");
item.setAjax(false);
item.setTitle("My title");
//you can either add this directly OPTION 1
model.addElement(item);
// or have submenus OPTION 2
// DefaultSubMenu submenu = new DefaultSubMenu("Upvote me");
// submenu.setExpanded(false);
// submenu.setStyleClass("stylemeplease");
// submenu.addElement(item);
// model.addElement(submenu);
继续添加许多DefaultMenuItem并将其添加到