使用primefaces 2.2.1。我们有MenuBean类:
public class MenuBean {
private MenuModel model;
public MenuBean() {
model = new DefaultMenuModel();
//First submenu
Submenu submenu = new Submenu();
submenu.setLabel("Dynamic Submenu 1");
MenuItem item = new MenuItem();
item.setValue("Dynamic Menuitem 1.1");
item.setUrl("#");
submenu.getChildren().add(item);
model.addSubmenu(submenu);
}
public MenuModel getModel() {
return model;
}
}
这是观点:
<h3>Programmatic Menu </h3>
<p:menu model="#{menuBean.model}"/>
我想用参数调用Controller方法,例如:
<p:menuitem value="Menu 01" action="#{myController.search()}">
<f:param value="1" name="mypara"/>
</p:menuitem>
我尝试:item.addActionListener(“#{myController.search()}”);但它不起作用。我在MenuBean类中可以做什么?
答案 0 :(得分:1)
你在使用Spring吗? 因为使用JSF 1.2的集成,如果你想将一个参数发送到控制器,你必须做出一种解决方法。
我遇到了同样的问题,我解决了以下问题:
<p:menuitem value="Menu 01" action="#{myController.search}">
<f:setPropertyActionListener target="#{managedBean.myparam}" value="1" />
</p:menuitem>
在您的情况下,您可以尝试在MenuModel中添加属性。这样的事情:
public class MenuBean {
...
private int myParam;
public int getMyParam() {
return myParam;
}
public void setMyParam(int myParam) {
this.myParam = myParam;
}
public void search() {
//here you could use the param sending by the view
service.find(myParam);
}
...
}
答案 1 :(得分:1)
使用当前的PrimeFaces版本(v6.x),解决方案可能如下所示:
MenuBean:
@Named
public class MenuBean implements Serializable {
private MenuModel model;
@PostConstruct
public void init() {
model = new DefaultMenuModel();
//First submenu
Submenu submenu = new Submenu();
submenu.setLabel("Dynamic Submenu 1");
MenuItem item = new MenuItem("Dynamic Menuitem 1.1");
item.setCommand("#{myController.search}"); // note missing braces when calling "search" method (action event is added by JSF)!
item.setParam("mypara", 1); // set paramater and its value (e.g. integer value)
item.setProcess("@this"); // process this command and its paramerter only
item.setUpdate("@form"); // update the current form (or maybe different component ID)
submenu.getChildren().add(item);
model.addSubmenu(submenu);
}
public MenuModel getModel() {
return model;
}
}
MyController:
@Named
public class MyController implements Serializable {
public void search(MenuActionEvent menuActionEvent) {
// extract the parameter
int myparaValue = Integer.parseInt(menuActionEvent.getMenuItem()
.getParams()
.get("mypara") // better use constant for parameter name!
.get(0)); // note that parameter contains a list of strings!
// do stuff with parameter value...
}
}