使用 JSF 2.0 和 Hibernate 的带有上下文菜单的树
我需要在 JSF 2.0 中实现带有上下文菜单(添加、删除、编辑)的内容树。请给我推荐一些组件。树节点应该是一个像(Data,ID)这样的对象。当我单击树节点时,我需要获取后端 bean 的 id。
示例:
index.xhtml
质数树
<p:growl id="messages" showDetail="true" />
<p:tree value="#{treeBean.root}" var="node" id="pTree"
selectionMode="single" selection="#{treeBean.selectedNode}">
<p:treeNode>
<h:outputText value="#{node}" />
</p:treeNode>
</p:tree>
<p:contextMenu for="pTree" id="cmenu">
<p:menuitem value="Add topic as child" update="pTree, cmenu"
actionListener="#{treeBean.addChildNode}" />
<p:menuitem value="Add topic Below" update="pTree, cmenu"
actionListener="#{treeBean.addTopicBelow}" />
<p:menuitem value="Delete Topic" update="pTree, cmenu"
actionListener="#{treeBean.deleteNode}" />
</p:contextMenu>
</h:form>
</h:body>
TreeBean.java
/* * 要更改此模板,请选择“工具”|“模板 * 并在编辑器中打开模板。 */
包 com.prime.tree;
导入 java.io.Serialized; 导入 javax.faces.application.FacesMessage; 导入 javax.faces.bean.ManagedBean; 导入 javax.faces.bean.SessionScoped; 导入 javax.faces.context.FacesContext; 导入 javax.faces.event.ActionEvent; 导入 org.primefaces.model.DefaultTreeNode; 导入 org.primefaces.model.TreeNode;
/** * * @作者维伦德拉 */ @ManagedBean @SessionScoped 公共类 TreeBean 实现可序列化 {
private TreeNode root;
private TreeNode selectedNode;
public TreeBean() {
root = new DefaultTreeNode("Root", null);
List rootNodes<Employee> = SearchDao.getRootNodes();
Iterator it = rootNodes.iterator();
while (it.hasNext()) {
TreeNode node1 = new DefaultTreeNode(**it.next()**, root);
**/* in place of it.next() I need to display empName. When I click on empName, I need to get the Id(Pkey). */**
}
}
public TreeNode getRoot() {
return root;
}
public TreeNode getSelectedNode() {
return selectedNode;
}
public void setSelectedNode(TreeNode selectedNode) {
this.selectedNode = selectedNode;
}
public void addChildNode(ActionEvent actionEvent) {
System.out.println("Selected Node: "+getSelectedNode().toString());
TreeNode newNode = new DefaultTreeNode("Node New", getSelectedNode());
getSelectedNode().setExpanded(true);
}
public void addTopicBelow(ActionEvent actionEvent){
TreeNode newNode = new DefaultTreeNode("Node New", getSelectedNode().getParent());
}
public void deleteNode(ActionEvent actionEvent){
System.out.println("Node to be deleted: "+getSelectedNode().toString());
//getSelectedNode().
}
}
Employee.java
公共类 Employee{
private String empID;
private String empName;
/**
* @return the empName
*/
public String getEmpName() {
return empName;
}
/**
* @param empName the empName to set
*/
public void setEmpName(String empName) {
this.empName = empName;
}
/**
* @return the empID
*/
public String getEmpID() {
return empID;
}
/**
* @param empID the empID to set
*/
public void setEmpID(String empID) {
this.empID = empID;
}
}
I need to implement a content tree with context menu (add, delete, edit) in JSF 2.0. Please suggest me some components. The tree node should be an object like (Data, ID). When I click on tree node I need to get the id to backend bean.
Example:
index.xhtml
Prime Tree
<p:growl id="messages" showDetail="true" />
<p:tree value="#{treeBean.root}" var="node" id="pTree"
selectionMode="single" selection="#{treeBean.selectedNode}">
<p:treeNode>
<h:outputText value="#{node}" />
</p:treeNode>
</p:tree>
<p:contextMenu for="pTree" id="cmenu">
<p:menuitem value="Add topic as child" update="pTree, cmenu"
actionListener="#{treeBean.addChildNode}" />
<p:menuitem value="Add topic Below" update="pTree, cmenu"
actionListener="#{treeBean.addTopicBelow}" />
<p:menuitem value="Delete Topic" update="pTree, cmenu"
actionListener="#{treeBean.deleteNode}" />
</p:contextMenu>
</h:form>
</h:body>
TreeBean.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.prime.tree;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
/**
*
* @author Veerendra
*/
@ManagedBean
@SessionScoped
public class TreeBean implements Serializable {
private TreeNode root;
private TreeNode selectedNode;
public TreeBean() {
root = new DefaultTreeNode("Root", null);
List rootNodes<Employee> = SearchDao.getRootNodes();
Iterator it = rootNodes.iterator();
while (it.hasNext()) {
TreeNode node1 = new DefaultTreeNode(**it.next()**, root);
**/* in place of it.next() I need to display empName. When I click on empName, I need to get the Id(Pkey). */**
}
}
public TreeNode getRoot() {
return root;
}
public TreeNode getSelectedNode() {
return selectedNode;
}
public void setSelectedNode(TreeNode selectedNode) {
this.selectedNode = selectedNode;
}
public void addChildNode(ActionEvent actionEvent) {
System.out.println("Selected Node: "+getSelectedNode().toString());
TreeNode newNode = new DefaultTreeNode("Node New", getSelectedNode());
getSelectedNode().setExpanded(true);
}
public void addTopicBelow(ActionEvent actionEvent){
TreeNode newNode = new DefaultTreeNode("Node New", getSelectedNode().getParent());
}
public void deleteNode(ActionEvent actionEvent){
System.out.println("Node to be deleted: "+getSelectedNode().toString());
//getSelectedNode().
}
}
Employee.java
public class Employee{
private String empID;
private String empName;
/**
* @return the empName
*/
public String getEmpName() {
return empName;
}
/**
* @param empName the empName to set
*/
public void setEmpName(String empName) {
this.empName = empName;
}
/**
* @return the empID
*/
public String getEmpID() {
return empID;
}
/**
* @param empID the empID to set
*/
public void setEmpID(String empID) {
this.empID = empID;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以结合 Primefaces Tree 组件查看="http://www.primefaces.org/showcase/ui/contextMenu.jsf" rel="nofollow">上下文菜单。
您可以在支持 bean 中构建树并获取选定的节点,如 Primefaces 展示示例中所述。
You could take a look at the Primefaces Tree component in combination with the ContextMenu.
You can build your tree in the backing bean and get the selected node as described in the examples of the Primefaces showcase.