CSDN博客

img smilelily

struts开发实践—简单树的实现

发表于2004/4/13 14:14:00  926人阅读

 struts开发实践-简单树的实现

本案主要功能是完成用java语言写的一个简单的树,优点在于jsp页面的代码比较简单干净。缺点在于每次操作都要去服务器端再去取一次数据。近来在http://www.aspaid.com/网站找到一个javascript写的树,将js代码与java代码混编,可以得到一个功能比较全面的树,而且数据可一次性从服务器端取出,大家可以试一下。下面介绍的是前一种方法的代码实例。

1.定义MyTreeForm

package test;

import org.apache.struts.action.*;

import javax.servlet.http.*;

public class MyTreeForm

    extends ActionForm {

  /**id号*/

  private int id = 0;

  /**级别*/

  private int level = 0;

  /**名称*/

  private String text = "";

  /**上级节点*/

  private String upIds = "0";

  /**是否有子节点 0:no;1:yes;*/

  private String haveChild = "0";

  /**是否展开 //0:false;1:true;*/

  private String ifExpanded = "0";

  /**是否有下一个节点0:false;1:true;*/

  private String haveNext = "0";

  /**是否显示 0:visible;1:unvisible;*/

  private String isVisible = "0";

  /**节点链接串*/

  private String link = "null";

  public void setHaveChild(String haveChild) {

    this.haveChild = haveChild;

  }

 

  public String getHaveChild() {

    return haveChild;

  }

 

  public void setHaveNext(String haveNext) {

    this.haveNext = haveNext;

  }

 

  public String getHaveNext() {

    return haveNext;

  }

 

  public void setIfExpanded(String ifExpanded) {

    this.ifExpanded = ifExpanded;

  }

 

  public String getIfExpanded() {

    return ifExpanded;

  }

 

  public void setIsVisible(String isVisible) {

    this.isVisible = isVisible;

  }

 

  public String getIsVisible() {

    return isVisible;

  }

 

  public void setLink(String link) {

    this.link = link;

  }

 

  public String getLink() {

    return link;

  }

 

  public void setId(int id) {

    this.id = id;

  }

 

  public int getId() {

    return id;

  }

 

  public void setLevel(int level) {

    this.level = level;

  }

 

  public int getLevel() {

    return level;

  }

 

  public void setState(String state) {

    this.state = state;

  }

 

  public String getState() {

    return state;

  }

 

  public void setText(String text) {

    this.text = text;

  }

 

  public String getText() {

    return text;

  }

 

  public void setUpIds(String upIds) {

    this.upIds = upIds;

  }

 

  public String getUpIds() {

    return upIds;

  }

 

  public ActionErrors validate(ActionMapping actionMapping,

                               HttpServletRequest httpServletRequest) {

    /**@todo: finish this method, this is just the skeleton.*/

    return null;

  }

 

  public void reset(ActionMapping actionMapping,

                    HttpServletRequest httpServletRequest) {

  }

}

2.你就可以将你要显示的树的结果集,按照myTreeForm的样式存放在新的list中。

3.jsp的显示代码节选:

<%@ page contentType="text/html;charset=GBK" %>

<%@ taglib uri="/bean" prefix="bean" %>

<%@ taglib uri="/html" prefix="html" %>

<%@ taglib uri="/logic" prefix="logic" %>

<html:errors/>

<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr>

<%--树形显示区--%>

<td width="200" valign="top">

  <html:form action="/sortAdminAction.do" method="post">

  <table width="300" cellspacing="0" cellpadding="1">

     <tr><td><a href="javascript:doGenerateTree('0')"><img src="images/base.gif" border="0" >root</a></td></tr>

     <logic:iterate name="treeList" id="treeList" type="test.MyTreeForm">

         <logic:equal name="treeList" property="ifExpanded" value="1">

            <tr><td><img src="images/space.gif" width="<bean:write name="treeList" property="level"/>" height="1"><a href="javascript:doGenerateTree('<bean:write name="treeList" property="upIds"/>')"><img src="images/trees/no.gif" border="0"><bean:write name="treeList" property="text"/></a></td></tr>

         </logic:equal>

         <logic:equal name="treeList" property="ifExpanded" value="0">

            <tr><td><img src="images/space.gif" width="<bean:write name="treeList" property="level"/>" height="1"><a href="javascript:doGenerateTree('<bean:write name="treeList" property="upIds"/>')"><img src="images/trees/have.gif" border="0"><bean:write name="treeList" property="text"/></a></td></tr>

         </logic:equal>

     </logic:iterate>

  </table>

  <html:hidden property="upIds"/>

  </html:form>

</td></tr>

</table>

</body>

</html>

<SCRIPT LANGUAGE="JavaScript"> 

function doGenerateTree(upIds){

   myTreeForm.upIds.value=upIds;

   myTreeForm.submit();

}

</SCRIPT>

4。其实掌握了方法,写一简单树不成问题,如何写的树更美观、互动性更好、速度快还得花点功夫,有好的方法别忘了跟大家交流:)。

 

 

 

 

 

 

 

0 0

相关博文

我的热门文章

img
取 消
img