




已阅读5页,还剩16页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Struts1.2+Hibernate3.0+JavaScript+JSTL1.1 实现无限级树形菜单一、说明:1、开发环境:Eclipse3.2.1+MyEclipse5.1+Tomcat5.5+Microsoft SQL Server 20002、主要实现技术:Struts1.2+Hibernate3.0+JavaScript+JSTL1.1+自定义标签3、页面的树形菜单的节点用 JavaScript进行控制4、数据库中的商品类别表productCategory包含一个引用自身主键的外键,从而形成自身一对多关系5、自定义标签实现类Recursion中主要用了递归实现节点的展开二、完成后运行效果如下图:三、具体实现步骤:1、新建一个Web Project工程(随便加载JSTL1.1的支持),创建如下图所示的结构目录;2、创建数据库数据库中就只有两张表,其中商品类别商品为一对多关系,商品类别自身形成一对多关系(引用主键:类别编号做外键:父类编号),下面是由PowerDesigner 12 画的概念图生成的数据库脚本tree.sql如下:/*=*/* DBMS name: Microsoft SQL Server 2000 */* Created on: 2007-07-14 09:10:40 */*=*/*=*/* Table: product */*=*/create database treegouse treegocreate table product ( productId int identity not null, CategoryId int not null, productName varchar(100) not null, productPrice money not null, constraint PK_PRODUCT primary key nonclustered (productId)go/*=*/* Index: ProductCategoryToProduct_FK */*=*/create index ProductCategoryToProduct_FK on product (CategoryId ASC)go/*=*/* Table: productCategory */*=*/create table productCategory ( CategoryId int identity not null, ParentCategoryId int null, CategoryName varchar(50) not null, constraint PK_PRODUCTCATEGORY primary key nonclustered (CategoryId)go/*=*/* Index: 商品类别拥有商品类别_FK */*=*/create index 商品类别拥有商品类别_FK on productCategory (ParentCategoryId ASC)goalter table product add constraint FK_PRODUCT_PRODUCTCA_PRODUCTC foreign key (CategoryId) references productCategory (CategoryId)goalter table productCategory add constraint FK_PRODUCTC_商品类别拥有商品类_PRODUCTC foreign key (ParentCategoryId) references productCategory (CategoryId)用查询分析器运行上述脚本,在数据库中建表product结构如下图:表productCategory结构如下图:3、数据库建好后,通过MyEclipse加载Struts 1.2、Hibernate 3.0的支持4、切换到MyEclipse Database 视图,通过Hibernate Reverse Engineering生成两张表的Pojo类和*.hbm.xml映射文件5、对自动生成的Pojo类和*.hbm.xml映射文件进行适当的修改,修改后的文件如下:1)Product.javapackage com.lideedu.yame.tree.pojos;public class Product implements java.io.Serializable private Integer productId; private ProductCategory productCategory; private String productName; private Double productPrice; public Product() public Integer getProductId() return ductId; public void setProductId(Integer productId) ductId = productId; public ProductCategory getProductCategory() return ductCategory; public void setProductCategory(ProductCategory productCategory) ductCategory = productCategory; public String getProductName() return ductName; public void setProductName(String productName) ductName = productName; public Double getProductPrice() return ductPrice; public void setProductPrice(Double productPrice) ductPrice = productPrice; 2)Product.hbm.xml 3)ProductCategory.javapackage com.lideedu.yame.tree.pojos;import java.util.HashSet;import java.util.Set;SuppressWarnings(serial)public class ProductCategory implements java.io.Serializable private Integer categoryId;private String categoryName;private ProductCategory parentCategory;private Set subCategories = new HashSet(0);private Set products = new HashSet(0);public Set getProducts() return products;public void setProducts(Set products) ducts = products;public ProductCategory() public Integer getCategoryId() return this.categoryId;public void setCategoryId(Integer categoryId) this.categoryId = categoryId;public String getCategoryName() return this.categoryName;public void setCategoryName(String categoryName) this.categoryName = categoryName;public ProductCategory getParentCategory() return parentCategory;public void setParentCategory(ProductCategory parentCategory) this.parentCategory = parentCategory;public Set getSubCategories() return subCategories;public void setSubCategories(Set subCategories) this.subCategories = subCategories;4)ProductCategory.hbm.xml 6、编写DAO1) ProductDAOpackage com.lideedu.yame.tree.dao;import java.util.List;import org.hibernate.Session;import org.hibernate.Transaction;import com.lideedu.yame.tree.db.HibernateSessionFactory;import com.lideedu.yame.tree.pojos.Product;public class ProductDAO private Session session= null;private Transaction tx = null;public void save(Product product)try session = HibernateSessionFactory.getSession();tx = session.beginTransaction();session.save(product);mit(); catch (Exception e) e.printStackTrace();if(tx != null)tx.rollback();finallyHibernateSessionFactory.closeSession();tx = null;public List queryAll()List list = null;try session = HibernateSessionFactory.getSession();list = session.createQuery(from Product p left join fetch ductCategory).list(); catch (Exception e) e.printStackTrace();finallyHibernateSessionFactory.closeSession();return list;public List queryByProductId(int productId)List list = null;try session = HibernateSessionFactory.getSession();list = session.createQuery(from Product p left join fetch ductCategory where ductId=+productId).list(); catch (Exception e) e.printStackTrace();finallyHibernateSessionFactory.closeSession();return list;public List queryByCategoryId(int categoryId)List list = null;try session = HibernateSessionFactory.getSession();list = session.createQuery(from Product p left join fetch ductCategory where ductCategory.categoryId=+categoryId).list(); catch (Exception e) e.printStackTrace();finallyHibernateSessionFactory.closeSession();return list;2) ProductCategoryDAOpackage com.lideedu.yame.tree.dao;import java.util.List;import org.hibernate.Session;import org.hibernate.Transaction;import com.lideedu.yame.tree.db.HibernateSessionFactory;import com.lideedu.yame.tree.pojos.ProductCategory;public class ProductCategoryDAO private Session session= null;private Transaction tx = null;public void save(ProductCategory pc)try session = HibernateSessionFactory.getSession();tx = session.beginTransaction();session.save(pc);mit(); catch (Exception e) e.printStackTrace();if(tx != null)tx.rollback();finallyHibernateSessionFactory.closeSession();tx = null;public List queryAll()List list = null;try session = HibernateSessionFactory.getSession();list = session.createQuery(from ProductCategory p left join fetch p.parentCategory).list(); catch (Exception e) e.printStackTrace();finallyHibernateSessionFactory.closeSession();return list;7、编写Struts 控制类1)ListCategoryAction.java 这个类的作用是从数据库中查出根类,存放到List中,然后把List保存到request范围,在前台通过自定义标签取出后,在自定义标签的实现类RecursionTag中通过递归做成一棵目录树,在页面显示出来,页面的节点的展开和折叠是通过JavaScript控制的。package com.lideedu.yame.tree.struts.action;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import com.lideedu.yame.tree.dao.ProductCategoryDAO;import com.lideedu.yame.tree.pojos.ProductCategory;public class ListCategoryAction extends Action SuppressWarnings(unchecked)public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) ProductCategoryDAO pcDAO = new ProductCategoryDAO();List list = new ArrayList();Iterator it = pcDAO.queryAll().iterator();for (Iterator iter = it; iter.hasNext();) ProductCategory element = (ProductCategory) iter.next();if(element.getParentCategory() = null)list.add(element);request.setAttribute(all, list);return mapping.findForward(list);2)QueryProductAction.java 此类用于显示具体商品的名称时调用,当页面目录树展开到没有子类的节点时,就调用此类从数据库中查询出该类别下所有商品的信息package com.lideedu.yame.tree.struts.action;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import com.lideedu.yame.tree.dao.ProductDAO;public class QueryProductAction extends Action private ProductDAO productDAO = new ProductDAO();public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) int productId = new Integer(request.getParameter(productId);List list = productDAO.queryByProductId(productId);request.setAttribute(products, list);return mapping.findForward(listProducts);8、Struts配置文件struts-config.xml 9、几个JSP页面文件1) main.jsp 此页面非常简单,只是提供个调用ListCategoryAction的入口 Main !- 显示所有商品类别目录 2) ListProducts.jsp此页用来显示某个具体商品的详细信息 My JSP ListProducts.jsp starting page !- 商品详细信息 商品所属类别:$ductCategory.categoryName 商品编号:$ductId 商品名称:$ductName 价格:$ductPrice元 * 3) ListCategory.jsp *核心页面显示商品类别目录树和具体商品信息,无限级树型菜单所有商品没有任何商品!initiate();您的浏览器不支持此功能,请您使用最新的版本。 function iframeResize()var dyniframe = null;var indexwin = null;if (document.getElementById)dyniframe = document.getElementById(mainFrame);indexwin = window;if (dyniframe)if (dyniframe.contentDocument)dyniframe.height = dyniframe.contentDocument.body.scrollHeight + 10;else if (dyniframe.document & dyniframe.document.body.scrollHeight) iframeheight= mainFrame.document.body.scrollHeight + 10;dyniframe.height = iframeheight;10、用到的JavaScript脚本和样式表(控制树节点的关键)1) myjs.js2) mycss.css上述两个文件和用到的图片都打包在上传的文件中,因为这两个文件是网上搜来的,有兴趣的自己去研究!11、自定义标签的编写1) 创建标签处理类(Tag Handler ClassRecursionTag.java)package com.lideedu.yame.utils;import java.io.IOException;import java.util.Iterator;imp
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《调皮的小闹钟》课件
- 血液净化中心护理工作总结
- 公司节后安全培训课件
- 中国城市轨道交通安全现状与安全能力建设
- 尾矿库安全操作规程
- 事记叙文课件
- 耳源性患者的护理
- 物业部门工作汇报
- 社区综治平安建设工作总结
- 护理工作十大核心制度
- 基础计算机知识常识试题及答案
- 2022年7月23日广东省事业单位高校毕业生招聘考试《基本能力测试》试题真题答案解析
- 电缆缚设人工合同协议
- 药房卫生知识培训课件
- 2025年职业指导师专业能力测试卷:职业技能提升与职业素养培养试题
- 剪彩仪式方案超详细流程
- 江苏镇江历年中考作文题与审题指导(2003-2024)
- 四个自信的深刻理解试题及答案
- 精装修施工安全管理
- CNAS-CC175-2017 基于ISOIEC 20000-1的服务管理体系认证机构要求
- 创新创业甜品店计划书
评论
0/150
提交评论