Primefaces 5

系统教程 行业动态 更新时间:2024-06-14 16:57:39
Primefaces 5 - 动态p:菜单问题(Primefaces 5 - Dynamic p:menu issues)

我是Primefaces的新手,我正在遵循版本5.0的用户指南 。 我已经成功构建了一个静态菜单。 现在我正在尝试构建一个非常简单的动态菜单,基本上在用户指南上复制粘贴示例(如果感兴趣,请参阅第294页)。 但它不起作用。

这是我的XHTML页面:

<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:p="http://primefaces.org/ui"> <h:head> <title>test-primefaces</title> </h:head> <h:body> <h:form> <p:menu model="#{menuBean.model}" /> </h:form> </h:body>

这是我的MenuBean.java

import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import org.primefaces.model.menu.DefaultMenuItem; import org.primefaces.model.menu.DefaultMenuModel; import org.primefaces.model.menu.DefaultSubMenu; import org.primefaces.model.menu.MenuModel; @ManagedBean @SessionScoped public class MenuBean { private MenuModel model; public MenuBean() { model = new DefaultMenuModel(); //First submenu DefaultSubMenu firstSubmenu = new DefaultSubMenu("Dynamic Submenu"); DefaultMenuItem item = new DefaultMenuItem("External"); item.setUrl("http://www.primefaces.org"); item.setIcon("ui-icon-home"); firstSubmenu.addElement(item); model.addElement(firstSubmenu); //Second submenu DefaultSubMenu secondSubmenu = new DefaultSubMenu("Dynamic Actions"); item = new DefaultMenuItem("Save"); item.setIcon("ui-icon-disk"); item.setCommand("#{menuBean.save}"); item.setUpdate("messages"); secondSubmenu.addElement(item); item = new DefaultMenuItem("Redirect"); item.setIcon("ui-icon-search"); item.setCommand("#{menuBean.redirect}"); secondSubmenu.addElement(item); model.addElement(secondSubmenu); } public MenuModel getModel() { return model; } }

菜单不起作用,因为我尝试运行该项目(我正在使用Netbeans),出现以下错误

无法找到含有“j_idt5:j_idt6”引用的表达式“消息”的组件。

我无法理解为什么我能得到它,而Google一直无法帮助我。 大部分疑问都是关于我在MenuBean.java中使用的注释和导入,因为Netbeans告诉我,来自“javax.faces.bean”的注释将在下一个JSF版本中被弃用。

另一个奇怪的事实是,在XHTML页面中,如果我改变了

<p:menu model="#{menuBean.model}" /> with <p:megaMenu model="#{menuBean.model}" />

我没有收到错误信息,但菜单并没有显示levle两个菜单项,我只是看到一个带有“Dynamic Submenu”和“Dynamic Actions”的菜单,没有任何子项。

我错过了什么? 如果需要,我愿意提供更多关于我的代码/项目的信息。

I'm new to Primefaces, and I'm following the User Guide of Version 5.0. I've successfully built a static menu. Now I'm trying to build a very simple dynamic menu, basically copy-pasting the example on the user guide (page 294, if you are interested). But it's not working.

This is my XHTML page:

<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:p="http://primefaces.org/ui"> <h:head> <title>test-primefaces</title> </h:head> <h:body> <h:form> <p:menu model="#{menuBean.model}" /> </h:form> </h:body>

This is my MenuBean.java

import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import org.primefaces.model.menu.DefaultMenuItem; import org.primefaces.model.menu.DefaultMenuModel; import org.primefaces.model.menu.DefaultSubMenu; import org.primefaces.model.menu.MenuModel; @ManagedBean @SessionScoped public class MenuBean { private MenuModel model; public MenuBean() { model = new DefaultMenuModel(); //First submenu DefaultSubMenu firstSubmenu = new DefaultSubMenu("Dynamic Submenu"); DefaultMenuItem item = new DefaultMenuItem("External"); item.setUrl("http://www.primefaces.org"); item.setIcon("ui-icon-home"); firstSubmenu.addElement(item); model.addElement(firstSubmenu); //Second submenu DefaultSubMenu secondSubmenu = new DefaultSubMenu("Dynamic Actions"); item = new DefaultMenuItem("Save"); item.setIcon("ui-icon-disk"); item.setCommand("#{menuBean.save}"); item.setUpdate("messages"); secondSubmenu.addElement(item); item = new DefaultMenuItem("Redirect"); item.setIcon("ui-icon-search"); item.setCommand("#{menuBean.redirect}"); secondSubmenu.addElement(item); model.addElement(secondSubmenu); } public MenuModel getModel() { return model; } }

The menu is not working, as I try to run the project (I'm using Netbeans) I get the following error

Cannot find component with expression "messages" referenced from "j_idt5:j_idt6".

I'm not able to undertand why I get it, and Google has not been able to help me. Most doubts are about the annotation and the imports I used in MenuBean.java, since Netbeans tells me that annotations from "javax.faces.bean" will be deprecated in the next JSF version.

Another curious fact, in the XHTML page, if I change

<p:menu model="#{menuBean.model}" /> with <p:megaMenu model="#{menuBean.model}" />

I don't get the error, but the menu does not render the levle two menu items, I just see a menu with "Dynamic Submenu" and "Dynamic Actions", without any children entries.

What am I missing? I am willing to provide more information about my code/project, if necessary.

最满意答案

对我来说,最好首先检查Primefaces展示,然后以后可以参考手册。

根据“编程菜单”示例,您需要该部分

<p:growl id="messages" showDetail="false"/>

我想这也是事实上错误信息是指出,它的缺失。

关于注释参见这里的建议。 由于JSF 2.2建议从JSF bean移动到CDI bean。

For me it works better to check the Primefaces showcase first, then later one can always refer to the manual.

According to the example "Programmatic menu" you need the part

<p:growl id="messages" showDetail="false"/>

I guess thats also in fact what the error message is stating, that its missing.

About the annotations see the recommendation i.e. here. Since JSF 2.2 it is recommended to move from JSF beans to CDI beans.

更多推荐

本文发布于:2023-04-13 11:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/bef1cad2f8aaf0e219668739c3f80d9a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Primefaces

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!