PrimeFaces是最早也最全面的支持JSF2.0的开源库,它显然走在了RichFaces,ICEfaces,MyFaces的前面,2月14日后对JSF2.0的支持更加的完善,增加了几个组件并修补了很多bug,受益于JavaEE6.0的新技术,我们现在可以零配置的在Netbeans6.8或新版本中开发PrimeFaces2.0应用了。
在这里只需要到
http://www.primefaces.org网站上下载其jar包并添加到项目classpath上即可,相当简单,我们看一下官方的一个使用Tree组建事件特性的完整例子(5分钟内搞定):
1.在Netbeans下新建一JavaWeb工程,一切皆默认,并将primefaces-2.0.0.jar添加到类路径中,index页面代码如下
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Hello from Facelets
<h:form>
<p:tree value="#{documentsController.root}"
dynamic="true"
var="doc"
cache="false"
nodeSelectListener="#{documentsController.onNodeSelect}"
nodeExpandListener="#{documentsController.onNodeExpand}"
nodeCollapseListener="#{documentsController.onNodeCollapse}"
update="selectedNode"
>
<p:treeNode>
<h:outputText value="#{doc}"/>
</p:treeNode>
<p:treeNode type="document" styleClass="documentStyle">
<h:outputText value="#{doc}" styleClass="nodeContent"/>
</p:treeNode>
<p:treeNode type="picture" styleClass="pictureStyle">
<h:outputText value="#{doc}" styleClass="nodeContent"/>
</p:treeNode>
<p:treeNode type="mp3" styleClass="mp3Style">
<h:outputText value="#{doc}" styleClass="nodeContent"/>
</p:treeNode>
</p:tree>
<hr/>
<h:outputText id="selectedNode" value="You have selected: #{documentsController.selectedDocument.data}" />
</h:form>
</h:body>
</html>
2.其托管bean代码如下
package controller;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.inject.Named;
import org.primefaces.event.NodeCollapseEvent;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.TreeNode;
/**
*
* @author Bluesky
*/
@ManagedBean
@Named
@RequestScoped
public class DocumentsController {
private static final Logger logger = Logger.getLogger(DocumentsController.class.getName());
private TreeNode root;
private TreeNode selectedDocument;
public DocumentsController() {
root = new TreeNode("root", null);
TreeNode documents = new TreeNode("Documents", root);
TreeNode pictures = new TreeNode("Pictures", root);
TreeNode music = new TreeNode("Music", root);
TreeNode work = new TreeNode("Work", documents);
TreeNode primefaces = new TreeNode("PrimeFaces", documents);
//Documents
TreeNode expenses = new TreeNode("document", "Expenses.doc", work);
TreeNode resume = new TreeNode("document", "Resume.doc", work);
TreeNode refdoc = new TreeNode("document", "RefDoc.pages", primefaces);
//Pictures
TreeNode barca = new TreeNode("picture", "barcelona.jpg", pictures);
TreeNode primelogo = new TreeNode("picture", "logo.jpg", pictures);
TreeNode optimus = new TreeNode("picture", "optimus.png", pictures);
//Music
TreeNode turkish = new TreeNode("Turkish", music);
TreeNode cemKaraca = new TreeNode("Cem Karaca", turkish);
TreeNode erkinKoray = new TreeNode("Erkin Koray", turkish);
TreeNode mogollar = new TreeNode("Mogollar", turkish);
TreeNode nemalacak = new TreeNode("mp3", "Nem Alacak Felek Benim", cemKaraca);
TreeNode resimdeki = new TreeNode("mp3", "Resimdeki Goz Yaslari", cemKaraca);
TreeNode copculer = new TreeNode("mp3", "Copculer", erkinKoray);
TreeNode oylebirgecer = new TreeNode("mp3", "Oyle Bir Gecer", erkinKoray);
TreeNode toprakana = new TreeNode("mp3", "Toprak Ana", mogollar);
TreeNode bisiyapmali = new TreeNode("mp3", "Bisi Yapmali", mogollar);
}
public TreeNode getRoot() {
return root;
}
public void setRoot(TreeNode root) {
this.root = root;
}
public TreeNode getSelectedDocument() {
return selectedDocument;
}
public void setSelectedDocument(TreeNode selectedDocument) {
this.selectedDocument = selectedDocument;
}
public void onNodeSelect(NodeSelectEvent event) {
selectedDocument = event.getTreeNode();
logger.info("Selected:" + selectedDocument.getData());
}
public void onNodeExpand(NodeExpandEvent event) {
String node = event.getTreeNode().getData().toString();
logger.info("Expanded:" + node);
}
public void onNodeCollapse(NodeCollapseEvent event) {
String node = event.getTreeNode().getData().toString();
logger.info("Collapsed:" + node);
}
}
3.运行就ok了,可以很容易实现左边树导航,中间内容的经典应用。