- 浏览: 311358 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (243)
- Core Java (13)
- Java (12)
- Android (2)
- Lucene (3)
- J2EE (3)
- Hibernate (2)
- Jsp & Servlet (3)
- Struts2 (3)
- Spring (5)
- JSF (6)
- RichFaces (2)
- HTML & JS & DOM & CSS (87)
- browser (1)
- Ajax & A4J (2)
- Workflow (1)
- Maven (3)
- Linux (2)
- VM & VBOX (1)
- Tomcat (1)
- Cache (3)
- Others (36)
- design (1)
- PHP (1)
- Try.js (1)
- HTML5 && CSS3 && ECMAScript5 (26)
- 疯言疯语 (5)
- mongodb (2)
- Hardware Common Sence (1)
- RESTful (3)
- Nginx (2)
- web安全 (8)
- Page Design (1)
- web performance (1)
- nodejs (4)
- python (1)
最新评论
-
New_Mao_Er:
求最新的版本破解啊!!!
Omondo eclipseUML插件破解 -
konglx:
讲得仔细,谢了
全面分析 Spring 的编程式事务管理及声明式事务管理 -
cilendeng:
对所有post有效只能使用过滤器
说说Tomcat的Server.xml的URIEncoding=‘UTF-8’配置 -
jiulingchen:
mark了,灰常感谢!
JAVA中最方便的Unicode转换方法 -
anlaetion:
这算法可以有
js 字符串搜索算法
前段时间,因为公司分配了这样的任务,开始研究了richfaces3.3.1的demo。其中关于树的拖动部分,通过研究demo的例子,我做了一些修改,实现了下面的功能:
1.树的动态加载;
2.一棵树内的节点的拖动,只能从叶子节点拖动到非叶子节点。
不过,我想实现更复杂的拖动树在这基础上修改也不会太难。
下面贴代码吧:
/pages/tree/下的jsp:
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <%@taglib uri="http://richfaces.org/rich" prefix="rich"%> <%@taglib uri="http://richfaces.org/a4j" prefix="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=UTF-8"> <title>tree demo</title> </head> <style> .col1, .col2 { width:50%; vertical-align:top; } .rich-table-cell, .rich-table{ border:none; } </style> <body> <f:view> <div id="top"> <h3>tree demo about drag and drop</h3> </div> <rich:dragIndicator id="indicator1" /> <h:form> <h:panelGrid columns="1" width="100%" columnClasses="col1"> <rich:tree style="width:300px" switchType="ajax" value="#{treesBean.treeNode}" var="item" id="tree" treeNodeVar="treeNode" nodeFace="#{item.type == 1 ? 'node' : 'leaf'}"> <rich:treeNode type="node" acceptedTypes="all" dropListener="#{evntBean.processDrop}" dropValue="#{treeNode}" icon="#{treeNode.icon}" iconLeaf="#{treeNode.leafIcon}" changeExpandListener="#{treesBean.processExpansion}" data="#{treeNode}" reRender="tree"> <h:outputText value="#{item.nodeName}"/> </rich:treeNode> <rich:treeNode type="leaf" dragType="all" dragValue="#{treeNode}" icon="#{treeNode.icon}" iconLeaf="#{treeNode.leafIcon}" dragIndicator="indicator1" dragListener="#{evntBean.processDrag}"> <rich:dndParam name="label" type="drag" value="#{item.nodeName}"></rich:dndParam> <h:outputText value="#{item.nodeName}"/> </rich:treeNode> </rich:tree> </h:panelGrid> </h:form> </f:view> </body> </html>
package demo下的hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/richfaces_demo</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">mysql51</property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <mapping resource="/demo/grid/Person.hbm.xml"/> <mapping resource="/demo/tree/NodeVo.hbm.xml"/> </session-factory> </hibernate-configuration>
package demo.tree下的class和*.hbm.xml:
package demo.tree; import org.richfaces.model.TreeNodeImpl; public class DemoTreeNodeImpl extends TreeNodeImpl{ private String icon;//节点icon private String leafIcon;//叶子节点icon private int nodeId;//节点id private int type;//节点类型:1-节点 2-叶子节点 private int status;//节点的状态: 1-展开状态 0-折叠状态 public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public String getLeafIcon() { return leafIcon; } public void setLeafIcon(String leafIcon) { this.leafIcon = leafIcon; } public int getNodeId() { return nodeId; } public void setNodeId(int nodeId) { this.nodeId = nodeId; } public int getType() { return type; } public void setType(int type) { this.type = type; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } }
package demo.tree; import org.richfaces.component.Dropzone; import org.richfaces.component.Draggable; import org.richfaces.event.DropEvent; import org.richfaces.event.DragEvent; import org.richfaces.event.DropListener; import org.richfaces.event.DragListener; import org.richfaces.component.UITree; import org.richfaces.component.UITreeNode; import org.ajax4jsf.context.AjaxContext; public class EventBean implements DropListener,DragListener{//,NodeExpandedListener{ private TreesBean treesBean; private Object dragValue; /** * 处理树内子节点拖动(到节点)操作 */ public void processDrop(DropEvent dropEvent) { Dropzone dropzone = (Dropzone) dropEvent.getComponent(); treesBean.move(dragValue, dropzone.getDropValue()); UITree destTree = null; if(dropEvent.getSource() instanceof UITreeNode){ UITreeNode destNode = (UITreeNode)dropEvent.getSource(); destTree = destNode.getUITree(); }else if(dropEvent.getSource() instanceof UITree){ destTree = (UITree)dropEvent.getSource(); } AjaxContext ac = AjaxContext.getCurrentInstance(); // Add destination tree to reRender try { ac.addComponentToAjaxRender(destTree); } catch (Exception e) { System.err.print("ac.addComponentToAjaxRender(destTree):" + e.getMessage()); } } public void processDrag(DragEvent dragEvent){ Draggable draggable = (Draggable)dragEvent.getComponent(); dragValue = draggable.getDragValue(); } public TreesBean getTreesBean() { return treesBean; } public void setTreesBean(TreesBean treesBean) { this.treesBean = treesBean; } }
/* * NodeVo.java * pojo类 hibernate entitybean */ package demo.tree; public class NodeVo{ private int id; private int nodeId;//节点id private String nodeName;//节点名 private int parentNodeId;//父节点id private int type;//节点类型:1-节点 0-子节点 private String icon;//节点icon private String leafIcon;//子节点icon /** * Creates a new instance of NodeVo */ public NodeVo() { } /** * @return the id */ public int getId() { return id; } /** * @param id the id to set */ public void setId(int id) { this.id = id; } /** * @return the nodeId */ public int getNodeId() { return nodeId; } /** * @param nodeId the nodeId to set */ public void setNodeId(int nodeId) { this.nodeId = nodeId; } /** * @return the nodeName */ public String getNodeName() { return nodeName; } /** * @param nodeName the nodeName to set */ public void setNodeName(String nodeName) { this.nodeName = nodeName; } /** * @return the parentNodeId */ public int getParentNodeId() { return parentNodeId; } /** * @param parentNodeId the parentNodeId to set */ public void setParentNodeId(int parentNodeId) { this.parentNodeId = parentNodeId; } /** * @return the type */ public int getType() { return type; } /** * @param type the type to set */ public void setType(int type) { this.type = type; } /** * @return the icon */ public String getIcon() { return icon; } /** * @param icon the icon to set */ public void setIcon(String icon) { this.icon = icon; } /** * @return the leafIcon */ public String getLeafIcon() { return leafIcon; } /** * @param leafIcon the leafIcon to set */ public void setLeafIcon(String leafIcon) { this.leafIcon = leafIcon; } }
package demo.tree; import java.util.List; import org.richfaces.model.TreeNode; import org.richfaces.event.NodeExpandedEvent; import org.richfaces.component.html.HtmlTreeNode; import javax.faces.context.FacesContext; import org.hibernate.Session; import demo.util.DBUtil; public class TreesBean { private DemoTreeNodeImpl rootNode = null; private static final String ICONS_PATH = "/images/tree/dnd/"; public TreesBean(){ loadTree(1000);//load tree which root node Id is 1000 } /** * 为节点node加载第一层子节点 */ private void addNodes(DemoTreeNodeImpl node) { String sql = "from NodeVo where parentNodeId=:nodeId"; Session ss = DBUtil.getSf().openSession(); List list = ss.createQuery(sql).setInteger("nodeId", node.getNodeId()).list(); ss.close(); node.removeChild("fakeNode"); if(null != list && list.size() > 0){ for(int i=0;i<list.size();i++){ NodeVo o = (NodeVo)list.get(i); DemoTreeNodeImpl tmpNode = new DemoTreeNodeImpl(); tmpNode.setNodeId(o.getNodeId()); tmpNode.setType(o.getType()); tmpNode.setData(o); if(o.getType() == 1){//node if("Unsorted".equalsIgnoreCase(o.getNodeName())){ tmpNode.setIcon(ICONS_PATH + "image.gif"); tmpNode.setLeafIcon(ICONS_PATH + "image.gif"); }else if("Favorites".equalsIgnoreCase(o.getNodeName())){ tmpNode.setIcon(ICONS_PATH + "favorites.gif"); tmpNode.setLeafIcon(ICONS_PATH + "favorites.gif"); }else if("Trash".equalsIgnoreCase(o.getNodeName())){ tmpNode.setIcon(ICONS_PATH + "trash.gif"); tmpNode.setLeafIcon(ICONS_PATH + "trash.gif"); }else{ tmpNode.setIcon(ICONS_PATH + "iconFolder.gif"); tmpNode.setLeafIcon(ICONS_PATH + "iconFolder.gif"); } }else if(o.getType() == 0){//leaf tmpNode.setLeafIcon(ICONS_PATH + "disc.gif"); } tmpNode.setParent(node); node.addChild(new Integer(tmpNode.getNodeId()), tmpNode); if(tmpNode.getType() == 1){ //构造一个假的的子结点 DemoTreeNodeImpl fakeNode = new DemoTreeNodeImpl(); NodeVo vo = new NodeVo(); vo.setNodeName("loading..."); vo.setType(1); fakeNode.setData(vo); fakeNode.setIcon(ICONS_PATH + "loading.gif"); fakeNode.setLeafIcon(ICONS_PATH + "loading.gif"); fakeNode.setType(1); tmpNode.addChild("fakeNode",fakeNode); } } } } /** * 加载节点id为nodeId的树 */ private void loadTree(int nodeId) { Session ss = DBUtil.getSf().openSession(); String sql = "from NodeVo where parentNodeId = 0 and nodeId=:nodeId"; Object o = ss.createQuery(sql).setInteger("nodeId", nodeId).uniqueResult(); ss.close(); if (null != o) { NodeVo nodeVo = (NodeVo) o; rootNode = new DemoTreeNodeImpl(); rootNode.setNodeId(nodeVo.getNodeId()); rootNode.setData(nodeVo); rootNode.setParent(null); addNodes(rootNode); } } /** * 拖动树的叶子节点到节点 */ public void move(Object dragObj,Object dropObj){ DemoTreeNodeImpl dragNode = (DemoTreeNodeImpl)dragObj; DemoTreeNodeImpl dropNode = (DemoTreeNodeImpl)dropObj; TreeNode dragParentNode = dragNode.getParent(); if(null != dragParentNode){ if(((DemoTreeNodeImpl)dragParentNode).getNodeId() == dropNode.getNodeId()){ //在父结点内拖动无操作 return; } } String sql = "from NodeVo where nodeId=:nodeId"; Session ss = DBUtil.getSf().openSession(); Object o = ss.createQuery(sql).setInteger("nodeId",dragNode.getNodeId()).uniqueResult(); if(null != o){ NodeVo nodeVo = (NodeVo)o; nodeVo.setParentNodeId(dropNode.getNodeId()); ss.beginTransaction().begin(); ss.update(nodeVo); ss.beginTransaction().commit(); ss.flush(); } ss.close(); NodeVo vo = (NodeVo)dragNode.getData(); vo.setParentNodeId(dropNode.getNodeId()); dragNode.setData(vo); dragParentNode.removeChild(new Integer(dragNode.getNodeId()));//这里必须先做删除操作,之后再做setParent()和addChild()操作,这与richfaces的具体实现有关 dragNode.setParent(dropNode); dropNode.addChild(new Integer(dragNode.getNodeId()),dragNode); FacesContext.getCurrentInstance().renderResponse(); } /** * 处理展开与折叠操作 */ public void processExpansion(NodeExpandedEvent e){ HtmlTreeNode tNode = (HtmlTreeNode)e.getComponent(); DemoTreeNodeImpl o = (DemoTreeNodeImpl)tNode.getData(); if(o.getStatus() == 0){//当该结点处于折叠状态下,则加载该结点的第一次子结点 addNodes(o); o.setStatus(1); } } public DemoTreeNodeImpl getTreeNode() { return rootNode; } public void setTreeNode(DemoTreeNodeImpl rootNode){ this.rootNode = rootNode; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="demo.tree"> <class name="NodeVo" table="tree"> <id column="id" name="id"> <generator class="native"/> </id> <property name="nodeId" not-null="true"/> <property name="nodeName" not-null="true" type="java.lang.String"/> <property name="parentNodeId" not-null="true"/> <property name="type" not-null="true"/> </class> </hibernate-mapping>
package demo.util下的class:
package demo.util; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.hibernate.cfg.Configuration; import org.hibernate.SessionFactory; import javax.servlet.ServletContextListener; /** * * @author SailingLee */ public class DBUtil implements ServletContextListener{ private static SessionFactory sf = null; public DBUtil() { } public void contextInitialized(ServletContextEvent servletContextEvent) { sf = new Configuration().configure("/demo/hibernate.cfg.xml").buildSessionFactory(); } public void contextDestroyed(ServletContextEvent servletContextEvent) { sf.close(); } public static SessionFactory getSf() { return sf; } }
package demo.phase下的class:
package demo.phase; import java.util.Map; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.faces.event.PhaseEvent; import javax.faces.event.PhaseId; import javax.faces.event.PhaseListener; public class PostbackPhaseListener implements PhaseListener { public static final String POSTBACK_ATTRIBUTE_NAME = PostbackPhaseListener.class.getName(); public void afterPhase(PhaseEvent event) { } public void beforePhase(PhaseEvent event) { FacesContext facesContext = event.getFacesContext(); Map requestMap = facesContext.getExternalContext().getRequestMap(); requestMap.put(POSTBACK_ATTRIBUTE_NAME, Boolean.TRUE); } public PhaseId getPhaseId() { return PhaseId.APPLY_REQUEST_VALUES; } public static boolean isPostback() { FacesContext facesContext = FacesContext.getCurrentInstance(); if (facesContext != null) { ExternalContext externalContext = facesContext.getExternalContext(); if (externalContext != null) { return Boolean.TRUE.equals( externalContext.getRequestMap().get(POSTBACK_ATTRIBUTE_NAME)); } } return false; } }
/WEB-INF/下的xml:
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd"> <!-- =========== FULL CONFIGURATION FILE ================================== --> <faces-config> <!--(down) add by SailingLee about tree demo--> <managed-bean> <managed-bean-name>treesBean</managed-bean-name> <managed-bean-class>demo.tree.TreesBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> <managed-bean> <managed-bean-name>evntBean</managed-bean-name> <managed-bean-class>demo.tree.EventBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>treesBean</property-name> <property-class>demo.tree.TreesBean</property-class> <value>#{treesBean}</value> </managed-property> </managed-bean> <!--(up) add by SailingLee about tree demo--> <factory> <application-factory>org.richfaces.ui.application.StateApplicationFactory</application-factory> </factory> <lifecycle> <phase-listener>demo.phase.PostbackPhaseListener</phase-listener> </lifecycle> </faces-config>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- RichFaces Skin --> <context-param> <param-name>org.richfaces.SKIN</param-name> <param-value>blueSky</param-value> </context-param> <!-- Faces Servlet --> <context-param> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>-1</load-on-startup> </servlet> <!-- Faces Servlet Mapping --> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <!-- RichFaces Filter --> <filter> <display-name>RichFaces Filter</display-name> <filter-name>richfaces</filter-name> <filter-class>org.ajax4jsf.Filter</filter-class> </filter> <filter-mapping> <filter-name>richfaces</filter-name> <servlet-name>Faces Servlet</servlet-name> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> </filter-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> <listener> <listener-class>demo.util.DBUtil</listener-class> </listener> </web-app>
用到的类库见图片附件lib.jpg :)
相关推荐
在`richfaces3.3.1`中,`treeGrid`可以通过设置`nodeType`属性来定义节点类型,通过`onNodeExpand`、`onNodeCollapse`等事件来处理用户操作。 在实际应用中,`richfaces3.3.1`提供的这些组件和功能通常需要与后端...
《深入理解RichFaces 3.3.1:官方Demo源码解析》 RichFaces是一个功能强大的AJAX组件库,基于JavaServer Faces (JSF) 技术,为开发者提供了丰富的用户界面组件和交互性功能。在3.3.1版本中,它提供了大量的示例代码...
2. **richfaces-core-3.3.1.jar** 和 **richfaces-components-3.3.1.jar**:RichFaces的核心和组件库,提供丰富的UI组件和Ajax功能。 3. **facelets-1.2.jar**:Facelets的实现,用于构建JSF视图。 4. **jsf-api-1.2...
包括三个jar: richfaces-api-3.3.1.GA-sources.jar richfaces-impl-3.3.1.GA-sources.jar richfaces-ui-3.3.1.GA-sources.jar
本文将详细探讨"richfaces-ui-3.3.1.GA-bin"这个压缩包中的核心知识点,带你领略RichFaces 3.3.1 GA版本的魅力。 一、RichFaces概述 RichFaces是一个开源项目,由Jboss公司(现Red Hat)维护,它扩展了JSF规范,...
Nuxeo 所需补丁的 Richfaces 3.3.1.GA 源的分支。 原始资源可以在这里下载: 如何构建 运行mvn clean install 。 该构建适用于 Maven 2.2.1。 结果罐子: richfaces-impl-3.3.1.GA-NX8-SNAPSHOT.jar richfaces-ui...
2. **创建树节点模型**:定义数据模型,每个节点代表一个权限项,包括其名称、标识符、子节点等信息。这些数据通常存储在数据库或其他持久化存储中。 3. **绑定数据**:使用JSF的EL(Expression Language)将数据...
RichFaces 自动构建树实现 RichFaces 自动构建树实现是基于 Java 语言和 RichFaces 框架的树形结构实现。树形结构在软件系统中非常常见,包括树形菜单的构建、基于二叉树的二分查找法等等。RichFaces 提供了树形...
richfaces Demo 最新版3.3.1.CR3下载,直接放在tomcat上即可运行,访问网址:http://localhost:8080/richfaces-demo-3.3.1.CR3-tomcat6
richfaces-demo-3.3.1.GA-tomcat6 最新发布的一个版本 来源于Jboss:如果大家需要其他的版本可以去下载
此类定义了树节点的一些基本属性: - `private String value;`: 表示树节点的值。 - `private String label;`: 显示在界面上的标签。 - `private boolean leaf;`: 表明该节点是否为叶子节点。 - `private TreeNode ...
2. **学习RichFaces组件**:研究RichFaces提供的组件,如数据表、树形结构、图表和对话框等,以及它们的配置和使用方法。 3. **掌握Ajax使用**:理解Ajax在RichFaces中的工作方式,如何通过Ajax行为事件更新页面部分...
richfaces环境搭建所需要的jar richfaces-api-3.3.1.GA.jar richfaces-impl-3.3.1.GA.jar richfaces-ui-3.3.1.GA.jar 希望对搭建有帮助
**Richfaces構建節點樹——EJB+JSF+SEAM 使用Richfaces中的Tree和TreeNode組件(转).docx** - 这篇文章可能深入介绍了如何结合Enterprise JavaBeans (EJB)、JavaServer Faces (JSF) 和Seam框架,使用RichFaces的...
关于richfaces的一些比较好的资料,希望大家看了能有收获
总之,RichFaces是JSF开发中的有力工具,它通过丰富的组件和Ajax功能,提升了Web应用的交互性和效率。有了中文开发文档,开发者可以更方便地学习和应用这个框架,无论是在新项目开发还是旧项目维护中,都能发挥重要...
在探讨“RichFaces中的ajax组件实现刷新验证码”的技术细节时,我们首先需要理解RichFaces框架以及AJAX在其中的应用。RichFaces是一个基于JavaServer Faces(JSF)的开源UI组件库,它提供了丰富的用户界面组件,包括...