天天看點

RichFaces樹元件的用法

版權聲明:本文為部落客chszs的原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/chszs/article/details/5719401

RichFaces樹元件的用法

原創:版權所有chszs

RichFaces Tree元件提供了一個預設的資料模型,允許顯示簡單的樹結構,無需建立自己的樹模型類集。

下面的例子說明了從屬性檔案的資料生成樹目錄。

RichFaces樹元件的用法

選擇左邊樹元件的節點,右邊會顯示出相應的節點名。如圖所示:

RichFaces樹元件的用法

如上圖所示,建立圖示頁面的步驟如下:

一、搭建RichFaces開發環境,這一步省略;

二、編輯頁面:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"

xmlns:ui="http://java.sun.com/jsf/facelets"

xmlns:h="http://java.sun.com/jsf/html"

xmlns:f="http://java.sun.com/jsf/core"

xmlns:rich="http://richfaces.org/rich"

xmlns:a4j="http://richfaces.org/a4j">

<mce:style><!--

.col,.col2 {

width: 50%;

vertical-align: top;

}

--></mce:style><style mce_bogus="1">.col,.col2 {

}</style>

<h:form>

<fieldset>

<legend>Directory Tree</legend>

<h:panelGrid columns="2" width="100%" columnClasses="col1,col2">

<rich:tree style="width:150px;"

nodeSelectListener="#{simpleTreeBean.processSelection}"

reRender="selectedNode" ajaxSubmitSelection="true"

switchType="client" value="#{simpleTreeBean.treeNode}" var="item">

</rich:tree>

<h:outputText escape="false"

value="Selected Node:#{simpleTreeBean.nodeTitle}" id="selectedNode" />

</h:panelGrid>

</fieldset>

</h:form>

</ui:composition>

頁面檔案需說明兩點:

1)<rich:tree>元件的ajaxSubmitSelection屬性,為真時表示支援Ajax送出;

2)<rich:tree>元件的value屬性,其值是org.richfaces.model.TreeNode類的執行個體。

三、屬性檔案:

1=/u7CFB/u7EDF/u7BA1/u7406

1.1=/u6743/u9650/u5206/u914D

1.2=/u89D2/u8272/u7BA1/u7406

1.3=/u5BC6/u7801/u4FEE/u6539

1.4=/u5BC6/u7801/u7B56/u7565

1.5=/u7CFB/u7EDF/u65E5/u5FD7

1.5.1=/u7528/u6237/u65E5/u5FD7

1.5.2=/u64CD/u4F5C/u8BB0/u5F55

2=/u8BBE/u5907/u7BA1/u7406

2.1=DVB-2

2.2=DTV-4

3=/u5458/u5DE5/u7BA1/u7406

3.1=/u5458/u5DE5/u4FE1/u606F

3.2=/u8BF7/u5047/u60C5/u51B5

轉碼後的文字如圖所示:

RichFaces樹元件的用法

四、托管Bean檔案

package chcms.view.mbeans;

import org.richfaces.component.html.HtmlTree;

import org.richfaces.event.NodeSelectedEvent;

import org.richfaces.model.TreeNode;

import org.richfaces.model.TreeNodeImpl;

import java.io.IOException;

import java.io.InputStream;

import java.util.*;

import javax.faces.FacesException;

import javax.faces.context.ExternalContext;

import javax.faces.context.FacesContext;

public class SimpleTreeBean {

private TreeNode rootNode = null;

private List<String> selectedNodeChildren = new ArrayList<String>();

private String nodeTitle;

private static final String DATA_PATH="/test/simple-tree-data.properties";

public TreeNode getTreeNode(){

if(rootNode == null){

loadTree();

}

return rootNode;

}

public String getNodeTitle(){

return nodeTitle;

public void setNodeTitle(String nodeTitle){

this.nodeTitle = nodeTitle;

private void addNodes(String path, TreeNode node, Properties properties){

boolean end = false;

int counter = 1;

while(!end){

String key = path != null ? path+'.'+counter:String.valueOf(counter);

String value = properties.getProperty(key);

if(value!=null){

TreeNodeImpl nodeImpl = new TreeNodeImpl();

nodeImpl.setData(value);

node.addChild(new Integer(counter), nodeImpl);

addNodes(key, nodeImpl, properties);

counter++;

}else{

end = true;

}

private void loadTree(){

FacesContext facesContext = FacesContext.getCurrentInstance();

ExternalContext externalContext = facesContext.getExternalContext();

InputStream dataStream = externalContext.getResourceAsStream(DATA_PATH);

try{

Properties properties = new Properties();

properties.load(dataStream);

rootNode = new TreeNodeImpl();

addNodes(null, rootNode, properties);

}catch(IOException e){

throw new FacesException(e.getMessage());

}finally{

if(dataStream!=null){

try{

dataStream.close();

}catch(IOException e){

externalContext.log(e.getMessage(), e);

}

public void processSelection(NodeSelectedEvent event){

HtmlTree tree = (HtmlTree)event.getComponent();

nodeTitle = (String)tree.getRowData();

selectedNodeChildren.clear();

TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());

if(currentNode.isLeaf()){

selectedNodeChildren.add((String)currentNode.getData());

}else{

Iterator<Map.Entry<Object, TreeNode>> it = currentNode.getChildren();

while(it!=null && it.hasNext()){

Map.Entry<Object, TreeNode> entry = it.next();

selectedNodeChildren.add(entry.getValue().getData().toString());

托管Bean需說明幾點:

1)DATA_PATH表示屬性檔案的路徑;

2)addNodes()方法采用了遞歸算法,需注意;

3)需了解java.util.Properties的用法;

4)了解構造樹元件的預設實作類org.richfaces.model.TreeNodeImpl類。

五、RichFaces配置檔案:

<managed-bean>

<managed-bean-name>simpleTreeBean</managed-bean-name>

<managed-bean-class>chcms.view.mbeans.SimpleTreeBean</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

</managed-bean>

以上内容能實作圖示例子。一些次要步驟省略!

繼續閱讀