一个 .<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">©2002-2003 Geir Landrö</a></p> </body> </html>
(7)文件夹结构截图:
执行效果:
版权声明:本文博客原创文章。博客,未经同意,不得转载。