[英]use primefaces tree component as a navigation menu
I am building a store website for my school and I am trying to add a tree as a category menu, but I cannot navigate to another page. 我正在为学校建立一个商店网站,并且试图添加一棵树作为类别菜单,但是我无法导航到另一个页面。 I receive all the necessary data in the controller but I do not know how to navigate to the browseByCategory page.
我在控制器中收到了所有必要的数据,但是我不知道如何导航到browserByCategory页面。
I have to mention I am a noooobie in Java JSF. 我不得不提到我是Java JSF的入门者。 any code will be appreciated.
任何代码将不胜感激。
thank you, Denis 谢谢你,丹尼斯
There are two ways of doing that: 1) You can navigate to another page with: 有两种方法:1)您可以使用以下方法导航到另一个页面:
DefaultTreeNode node0 = new DefaultTreeNode("<a href=\"http://www.google.pl\">http://www.google.pl</a>", null);
2) or in your nodeSelectEvent something like this: 2)或在您的nodeSelectEvent中是这样的:
<p:tree dynamic="true" value="#{TreeBean.root}" var="node" expandAnim="FADE_IN" collapseAnim="FADE_OUT" nodeSelectListener="#{TreeBean.onNodeSelect}">
<p:treeNode>
<h:outputText value="#{node}" />
</p:treeNode>
</p:tree>
public void onNodeSelect(NodeSelectEvent event){
String url = event.getTreeNode().getData().toString();
System.out.println(event.getTreeNode().getData().toString());
try {
//redirection
FacesContext.getCurrentInstance().getExternalContext().redirect(url);
} catch (IOException ex) {
//Logger.getLogger(TreeBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
Problem: The suggestions shared by r.piesnikowski are not correct. 问题:r.piesnikowski共享的建议不正确。 ie following code does not work:
即下面的代码不起作用:
DefaultTreeNode node0 = new DefaultTreeNode("<a href=\"http://www.google.pl\">http://www.google.pl</a>", null);
Also, the "nodeSelectListener" property does not exist in latest version of p:tree. 另外,最新版本的p:tree中不存在“ nodeSelectListener”属性。
Solution: Correct way to use links in Primefaces TreeNode using h:outputLink is as following: 解决方案:通过h:outputLink在Primefaces TreeNode中使用链接的正确方法如下:
<h:form> <p:tree value="#{treeView.root}" var="node"> <p:treeNode> <h:outputLink value="#{node.url}">#{node.label}</h:outputLink> </p:treeNode> </p:tree> </h:form>
Create another class (let's call it LinkNode) with two properties String url and String label. 使用两个属性String url和String label创建另一个类(我们称其为LinkNode)。 When you are creating a new node, pass the new object of LinkNode class with required url & label to new DefaultTreeNode(Object data, TreeNode parent) ie new DefaultTreeNode(new LinkNode(" http://example.com ","Go to example.com"), root).
创建新节点时,将带有所需URL和标签的LinkNode类的新对象传递到新的DefaultTreeNode(对象数据,TreeNode父对象),即新的DefaultTreeNode(新的LinkNode(“ http://example.com ”,“转到example.com“),根)。
@Named
@ViewScoped
public class TreeView implements Serializable{
private TreeNode root;
@PostConstruct
public void init() {
root = new DefaultTreeNode("Root", null);
TreeNode parent1 = new DefaultTreeNode(new LinkNode("http://example.com","Go to example.com"), root);
TreeNode parent2 = new DefaultTreeNode(new LinkNode("http://example.org","Go to example.org"), root);
}
}
and a LinkNode class like this: 和这样的LinkNode类:
public class LinkNode implements Serializable{
private String label; // add setter & getter as needed
private String link; // add setter & getter as needed
LinkNode(String label, String link){
this.label = label;
this.link = link;
}
}
Hope it helps. 希望能帮助到你。 I have tested this method myself.
我自己已经测试了这种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.