SHibernate+Javascript实现无限级树形菜单(3)

2018-11-18 21:08

}

List list = null; try {

session = HibernateSessionFactory.getSession();

list = session.createQuery(\

p.productCategory where p.productId=\+productId).list();

} catch (Exception e) { }

return list;

e.printStackTrace();

HibernateSessionFactory.closeSession(); }finally{

public List queryByCategoryId(int categoryId){

List list = null; try {

session = HibernateSessionFactory.getSession();

list = session.createQuery(\

p.productCategory where

p.productCategory.categoryId=\+categoryId).list(); }

}

} catch (Exception e) { }

return list;

e.printStackTrace();

HibernateSessionFactory.closeSession(); }finally{

2) ProductCategoryDAO

package 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); tx.commit();

e.printStackTrace(); if(tx != null)

tx.rollback();

} catch (Exception e) {

}finally{

HibernateSessionFactory.closeSession(); tx = null;

}

public List queryAll(){ }

List list = null; try {

session = HibernateSessionFactory.getSession();

list = session.createQuery(\ProductCategory p left join

fetch p.parentCategory\).list();

} catch (Exception e) { }

return list;

e.printStackTrace();

HibernateSessionFactory.closeSession(); }finally{

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 { }

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(\)); List list = productDAO.queryByProductId(productId); request.setAttribute(\, list); return mapping.findForward(\); @SuppressWarnings(\)

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();) { }

request.setAttribute(\, list); return mapping.findForward(\);

ProductCategory element = (ProductCategory) iter.next(); if(element.getParentCategory() == null)

list.add(element);

8、Struts配置文件?struts-config.xml

\>

type=\>

type=\>

parameter=\/>

9、几个JSP页面文件

1) main.jsp 此页面非常简单,只是提供个调用ListCategoryAction的入口

<%@ page language=\import=\pageEncoding=\%>

Main

prefix=\%> prefix=\%> prefix=\%>

<%@ taglib uri=\ <%@ taglib uri=\ <%@ taglib uri=\prefix=\%>


SHibernate+Javascript实现无限级树形菜单(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:电子测量实验二晶体管毫伏表的使用

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: