从属性文件中填充Richfaces Tree组件的代码?
答案 0 :(得分:0)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@ taglib prefix="rich" uri="http://richfaces.org/rich"%>
<%@ taglib prefix="a4j" uri="http://richfaces.org/a4j"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Tree Example</title>
</head>
<body>
<f:view>
<h:form>
<h:panelGrid columns="2" width="100%" columnClasses="col1,col2">
<rich:tree style="width:300px"
nodeSelectListener="#{simpleTreeBean.processSelection}"
reRender="selectedNode" switchType="client"
ajaxSubmitSelection="true" value="#{simpleTreeBean.treeNode}"
var="item">
</rich:tree>
<h:outputText escape="false"
value="Selected Node: #{simpleTreeBean.nodeTitle}" id="selectedNode" />
</h:panelGrid>
</h:form>
</f:view>
</body>
包com.java.beans;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.richfaces.component.html.HtmlTree;
import org.richfaces.event.NodeSelectedEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;
public class SimpleTreeBean {
protected org.richfaces.component.UITree sampleTreeBinding;
public org.richfaces.component.UITree getSampleTreeBinding() {
return sampleTreeBinding;
}
public void setSampleTreeBinding(
org.richfaces.component.UITree sampleTreeBinding) {
this.sampleTreeBinding = sampleTreeBinding;
}
private TreeNode rootNode = null;
private List<String> selectedNodeChildren = new ArrayList<String>();
private String 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() {
Properties props = new Properties();
InputStream stream = this.getClass().getClassLoader()
.getResourceAsStream("com/xyz/resources/richtree.properties");
try {
props.load(stream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rootNode = new TreeNodeImpl();
addNodes(null, rootNode, props);
}
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());
}
}
}
public TreeNode getTreeNode() {
if (rootNode == null) {
loadTree();
}
return rootNode;
}
public String getNodeTitle() {
return nodeTitle;
}
public void setNodeTitle(String nodeTitle) {
this.nodeTitle = nodeTitle;
}
}
1=Root Node 1
2=Root Node 2
3=Root Node 3
1.1=Leaf 1.1
1.1.1=Leaf 1.1.1
1.1.2=Leaf 1.1.2
1.1.3=Leaf 1.1.3
1.1.4=Leaf 1.1.4
1.1.5=Leaf 1.1.5
2.1=Leaf 2.1
2.1.1=Leaf 2.1.1
2.1.2=Leaf 2.1.2
2.1.3=Leaf 2.1.3
2.1.4=Leaf 2.1.4
2.1.5=Leaf 2.1.5
3.1=Leaf 3.1
3.1.1=Leaf 3.1.1
3.1.2=Leaf 3.1.2
3.1.3=Leaf 3.1.3
3.1.4=Leaf 3.1.4
3.1.5=Leaf 3.1.5
<managed-bean>
<managed-bean-name>simpleTreeBean</managed-bean-name>
<managed-bean-class>com.java.beans.SimpleTreeBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>