带参数的编程菜单?

发布于 2024-12-12 01:41:33 字数 920 浏览 0 评论 0原文

使用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}"/>    

我想使用参数调用控制器方法,例如:

 <p:menuitem value="Menu 01" action="#{myController.search()}">
      <f:param value="1" name="mypara"/>
  </p:menuitem> 

我尝试: item.addActionListener("#{myController.search()}");但这行不通。我可以在 MenuBean 类中做什么?

Using primefaces 2.2.1. We have MenuBean class :

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

This is view :

 <h3>Programmatic Menu </h3>
 <p:menu model="#{menuBean.model}"/>    

I want to call Controller method with parameter, something like :

 <p:menuitem value="Menu 01" action="#{myController.search()}">
      <f:param value="1" name="mypara"/>
  </p:menuitem> 

I try : item.addActionListener("#{myController.search()}"); but it's not work. What can i do in MenuBean class ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月流歌 2024-12-19 01:41:33

对于当前的 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...
    }

}

With current PrimeFaces versions (v6.x) a solution could look like this:

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...
    }

}
微凉徒眸意 2024-12-19 01:41:33

你用的是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);
}

...
}

Are you using Spring?
Because the integration of that use JSF 1.2 and if you want to send a paramter to a controller you have to made a kind of workaround.

I had the same problem and I resolve as the following lines:

<p:menuitem value="Menu 01" action="#{myController.search}">
    <f:setPropertyActionListener target="#{managedBean.myparam}" value="1" />
</p:menuitem>

in your case you could try to add a property into your MenuModel. Something like that:

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

...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文