Richfaces tree 通过读取property文件构建树

view:

 <h:panelGrid columns="1" width="100%">  
   <rich:tree style="width:100px" 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>

 

 

bean:

 

public class RichTreeBean {
 private TreeNode rootNode = null;
 
 private String nodeTitle;
 
 private static final String DATA_PATH = "/WEB-INF/classes/simple-tree-data.properties";
 
 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(), e);
  } finally {
   if (dataStream != null) {
    try {
     dataStream.close();
    } catch (IOException e) {
     externalContext.log(e.getMessage(), e);
    }
   }
  }
 }
 
 public TreeNode getTreeNode() {
  if (rootNode == null) {
   loadTree();
  }
  
  return rootNode;
 }

 public void processSelection(NodeSelectedEvent event) {
  UITree tree = (UITree) event.getComponent();
  nodeTitle = (String) tree.getRowData();
 }
 
 public String getNodeTitle() {
  return nodeTitle;
 }

 public void setNodeTitle(String nodeTitle) {
  this.nodeTitle = nodeTitle;
 }}

 

 

 

property文件:

 

1=Daniel Defo
1.1=Robinson Crusoe
1.1.1=Start In Life
1.1.2=Slavery And Escape
1.1.3=Wrecked On A Desert Island
1.1.4=First Weeks On The Island
1.1.5=Builds A House - The Journal
1.1.6=Ill And Conscience-Stricken
1.1.7=Agricultural Experience
1.1.8=Surveys His Position
1.1.9=A Boat
1.1.10=Tames Goats
1.1.11=Finds Print Of Man's Foot On The Sand
1.1.12=A Cave Retreat
1.1.13=Wreck Of A Spanish Ship
1.1.14=A Dream Realised
1.1.15=Friday's Education
1.1.16=Rescue Of Prisoners From Cannibals
1.1.17=Visit Of Mutineers
1.1.18=The Ship Recovered
1.1.19=Return To England
1.1.20=Fight Between Friday And A Bear
2=Edgar Allan Poe
2.1=Plays
2.1.1=Politian
2.2=Short stories
2.2.1=The Assignation
2.2.2=Berenice
2.2.3=The Black Cat
2.2.4=The Cask of Amontillado
2.2.5=A Descent into the Maelstrom
2.3=Poetry
2.3.1=Alone
2.3.2=An Enigma
2.3.3=Annabel Lee
2.3.4=Bridal Ballad
3=Henry Wadsworth Longfellow
3.1=The Song of Hiawatha
3.1.1=Introduction
3.1.2=I. The Peace-Pipe
3.1.3=II. The Four Winds
3.1.4=III. Hiawatha's Childhood
3.1.5=IV. Hiawatha and Mudjekeewis
3.1.6=V. Hiawatha's Fasting
3.1.7=VI. Hiawatha's Friends
3.1.8=VII. Hiawatha's Sailing
3.1.9=VIII. Hiawatha's Fishing
3.1.10=IX. Hiawatha and the Pearl-Feather
3.1.11=X. Hiawatha's Wooing
3.1.12=XI. Hiawatha's Wedding-Feast
3.1.13=XII. The Son of the Evening Star
3.1.14=XIII. Blessing the Cornfields
3.1.15=XIV. Picture-Writing
3.1.16=XV. Hiawatha's Lamentation
3.1.17=XVI. Pau-Puk-Keewis
3.1.18=XVII. The Hunting of Pau-Puk-Keewis
3.1.19=XVIII. The Death of Kwasind
3.1.20=XIX. The Ghosts
3.1.21=XX. The Famine
3.1.22=XXI. The White Man's Foot
3.1.23=XXII. Hiawatha's Departure
3.2=Poetry
3.2.1=A Psalm Of Life
3.2.2=Birds Of Passage
3.2.3=Hiawatha's Childhood
3.2.4=Hymn To The Night

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值