Richfaces tree component usage
Original: Copyright chszs
The richfaces tree component provides a default data model that allows you to display simple tree structures without creating your own tree model class set.
The following example illustrates the data generation tree directory from the property file.
Select the node of the tree component on the left, and the corresponding node name is displayed on the right. :
As shown in, the steps for creating the graph page are as follows:
1. Build a richfaces development environment. This step is omitted;
Ii. Editing page:
<UI: composition xmlns = "http://www.w3.org/1999/xhtml" <br/> xmlns: Ui = "http://java.sun.com/jsf/facelets" <br/> xmlns: H = "http://java.sun.com/jsf/html" <br/> xmlns: F = "http://java.sun.com/jsf/core" <br/> xmlns: Rich = "http://richfaces.org/rich" <br/> xmlns: a4j = "http://richfaces.org/a4j"> <br/> <MCE: style> <! -- <Br/>. col ,. col2 {<br/> width: 50%; <br/> vertical-align: Top; <br/>}< br/> --> </MCE: style> <style mce_bogus = "1">. col ,. col2 {<br/> width: 50%; <br/> vertical-align: Top; <br/>}</style> <br/> <p: form> <br/> <fieldset> <br/> <legend> directory tree </legend> <br/> <p: panelgrid columns = "2" width = "100%" columnclasses = "col1, col2"> <br/> <RICH: tree style = "width: 150px; "<br/> nodeselectlistener =" # {simpletreebean. processselection} "<br/> rerender =" selectednode "ajaxsubmitselection =" true "<br/> switchtype =" client "value =" # {simpletreebean. treenode} "Var =" item "> <br/> </rich: Tree> <br/> <p: outputtext escape = "false" <br/> value = "selected node: # {simpletreebean. nodetitle} "id =" selectednode "/> <br/> </p: panelgrid> <br/> </fieldset> <br/> </p: form> <br/> </UI: composition>
The page file must be described as follows:
1) <RICH: Tree> specifies the ajaxsubmitselection attribute of the component. If it is true, Ajax submission is supported;
2) The Value Attribute of the <RICH: Tree> component. Its value is an instance of the org. richfaces. model. treenode class.
3. Attribute file:
1 =/u7cfb/u7edf/u7ba1/u7406 <br/> 1.1 =/u6743/u9650/u5206/u914d <br/> 1.2 =/u89d2/u8272/u7ba1/u7406 </> 1.3 =/u5bc6/u7801/u4133/u6539 <br/> 1.4 =/u5bc6/u7801/u7b56/u7565 <br/> 1.5 =/u7cfb/u7edf/u65e5/5E <br/> 1.5.1 =/u7528/u6237/u65e5/u5fd7 <br/> 1.5.2 =/u64cd/u4f5c/u8bb0/u5f55 <br/> 2 =/u8bbe/u5907/Examples /u7406 <br/> 2.1 = DVB-2 <br/> 2.2 = DTV-4 <br/> 3 =/u5458/u5de5/u7ba1/u7406 <br/> 3.1 =/u5458/u5de5 /u4fe1/u606f <br/> 3.2 =/u8bf7/u5047/u60c5/u51b5 <br/>
Transcoded text:
4. Hosting bean files
Package chcms. view. mbeans; <br/> Import org.richfaces.component.html. htmltree; <br/> Import Org. richfaces. event. nodeselectedevent; <br/> Import Org. richfaces. model. treenode; <br/> Import Org. richfaces. model. treenodeimpl; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import Java. util. *; <br/> Import javax. faces. facesexception; <br/> Import javax. faces. context. externalcontext; <br/> Import javax. faces. context. facescontext; <br/> public class simpletreebean {<br/> private treenode rootnode = NULL; <br/> private list <string> selectednodechildren = new arraylist <string> (); <br/> private string nodetitle; <br/> Private Static final string data_path = "/test/simple-tree-data.properties"; <br/> Public treenode gettreenode () {<br/> If (rootnode = NULL) {<br/> loadtree (); <br/>}< br/> return Rootnode; <br/>}< br/> Public String getnodetitle () {<br/> return nodetitle; <br/>}< br/> Public void setnodetitle (string nodetitle) {<br/> This. nodetitle = nodetitle; <br/>}< br/> private void addnodes (string path, treenode node, properties Properties) {<br/> Boolean end = false; <br/> int counter = 1; <br/> while (! End) {<br/> string key = path! = NULL? Path + '.' + counter: String. valueof (Counter); <br/> string value = properties. getproperty (key); <br/> If (value! = NULL) {<br/> treenodeimpl nodeimpl = new treenodeimpl (); <br/> nodeimpl. setdata (value); <br/> node. addchild (New INTEGER (Counter), nodeimpl); <br/> addnodes (Key, nodeimpl, properties); <br/> counter ++; <br/>}else {<br/> end = true; <br/>}< br/> private void loadtree () {<br/> facescontext = facescontext. getcurrentinstance (); <br/> externalcontext = facescontext. gete Xternalcontext (); <br/> inputstream datastream = externalcontext. getresourceasstream (data_path); <br/> try {<br/> properties Properties = new properties (); <br/> properties. load (datastream); <br/> rootnode = new treenodeimpl (); <br/> addnodes (null, rootnode, properties); <br/>} catch (ioexception E) {<br/> throw new facesexception (E. getmessage (); <br/>}finally {<br/> If (datastream! = NULL) {<br/> try {<br/> datastream. close (); <br/>}catch (ioexception e) {<br/> externalcontext. log (E. getmessage (), e); <br/>}< br/> Public void processselection (nodeselectedevent event) {<br/> htmltree tree = (htmltree) event. getcomponent (); <br/> nodetitle = (string) tree. getrowdata (); <br/> selectednodechildren. clear (); <br/> treenode currentnode = tree. getmodeltreenode (tree. getrowkey (); <Br/> If (currentnode. isleaf () {<br/> selectednodechildren. add (string) currentnode. getdata (); <br/>}else {<br/> iterator <map. entry <object, treenode> it = currentnode. getchildren (); <br/> while (it! = NULL & it. hasnext () {<br/> map. entry <object, treenode> entry = it. next (); <br/> selectednodechildren. add (entry. getvalue (). getdata (). tostring (); <br/>}< br/>}
Managed beans:
1) data_path indicates the path of the property file;
2) The addnodes () method uses a recursive algorithm;
3) understand the usage of Java. util. properties;
4) understand the default implementation class org. richfaces. model. treenodeimpl of the constructor.
5. richfaces configuration file:
<Managed-bean> <br/> <managed-bean-Name> simpletreebean </managed-bean-Name> <br/> <managed-bean-class> chcms. view. mbeans. simpletreebean </managed-bean-class> <br/> <managed-bean-scope> request </managed-bean-scope> <br/> </managed-bean>
The preceding figure shows an example. Some secondary steps are omitted!