struts2由<s:tree>要麼dtree小工具 建立樹

weixin_34067049發表於2015-08-13

一個 .<s:tree>方法:

1.引入新的標籤:

<%@ taglib prefix="sd" uri="/struts-dojo-tags" %>


2.引入jar包:

struts2-dojo-plugin-2.2.3.jar

3.在<head>中增加

<sd:head/>


4.前臺程式碼:

  <sd:tree id="a" label="總公司">
    <sd:treenode id="aa" label="北京分公司" />
    <sd:treenode id="bb" label="上海分公司">
        <sd:treenode id="bbb1" label="綜合管理部" />
        <sd:treenode id="bbb2" label="研發部" />
    </sd:treenode>
    <sd:treenode id="cc" label="深圳分公司" />
  </sd:tree>

二.dtree方法(通過讀取資料庫中表顯示到dtree):

1.下載外掛dtree。地址:http://download.csdn.net/detail/ahhmdwg/8132227

2.匯入到你的web專案中:css檔案和js檔案放到你的專案中,將example01.html檔案裡的程式碼copy到你須要顯示樹形結構的jsp頁面。

   (1)建立資料庫表:

 

CREATE TABLE `tree` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `parentId` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    (2)bean包下類:

package com.dwg.bean;

public class Tree {
	private int id;
	
	private String name;
	
	private int parentId;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getParentId() {
		return parentId;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

}
   (3)dao層執行的自己定義sql:

     

public List<?

> getTreeList(final String sql) { // TODO Auto-generated method stub return (List<?>) this.getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(final Session session) throws HibernateException, SQLException { final SQLQuery query = session.createSQLQuery(sql); return query.list(); } }); }


   (4)action類:

    public String execute() throws Exception{
    String sql = "select * from tree";
    list = this.treeService.getTreeList(sql);
    Map request = (Map) ActionContext.getContext().get("request"); 
    System.out.println("----list1----");
    request.put("list", list);
    System.out.println("----list2----"); 
    
    	return SUCCESS;
    }

  (5)struts.xml檔案配置

    <package name="dtree"  namespace="/dtree" extends="struts-default">
      <action name="dtree" class="TreeAction">
      <result name="success">/dtree.jsp</result>
      </action>
    </package>

   (6)index.jsp頁面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%> 
<!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">
<base href="<%=basePath%>">
<title>Insert title here</title>
 <sd:head/>
 	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
</head>
<body>

<a href="<%=basePath%>dtree/dtree.action">DtreeDemo</a>
</body>
</html>
     dtree.jsp頁面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>
    <base href="<%=basePath%>">
	<title>Destroydrop » Javascripts » Tree</title>

	<link rel="StyleSheet" href="<%=basePath%>css/dtree.css" type="text/css" />
	<script type="text/javascript" src="<%=basePath%>script/dtree.js"></script>

</head>

<body>

<h1><a href="/">Destroydrop</a> » <a href="/javascripts/">Javascripts</a> » <a href="/javascripts/tree/">Tree</a></h1>

<h2>Example</h2>

<div class="dtree">

	<p><a href="javascript: d.openAll();">open all</a> | <a href="javascript: d.closeAll();">close all</a></p>
	<script type="text/javascript">
	
	d = new dTree('d');
	//構造根節點
	d.add(0,-1,'idada');
    <s:iterator value="list" id="us">
    d.add(<s:property value="#us[0]"/>,<s:property value="#us[2]"/>,'<s:property value="#us[1]"/>','dtree.jsp');
    </s:iterator>
    document.write(d);
    
	</script>

</div>

<p><a href="mailto:drop@destroydrop.com">&copy;2002-2003 Geir Landrö</a></p>

</body>

</html>

(7)資料夾結構截圖:

 執行效果:



















 

 

 

 

 

 

版權宣告:本文部落格原創文章。部落格,未經同意,不得轉載。

相關文章