3.做DAO代碼,
- package infoweb.dao;
- import java.util.List;
- import java.util.Iterator;
- import infoweb.pojo.Board;
- import net.sf.hibernate.HibernateException;
- import net.sf.hibernate.Query;
- import net.sf.hibernate.Session;
- import org.springframework.orm.hibernate.HibernateCallback;
- import org.springframework.orm.hibernate.support.HibernateDaoSupport;
- /**
- * <p>Title: 版塊分類DAOImpl</p>
- * <p>Description: 用樹型結構實現</p>
- * <p>Copyright: Copyright (c); 2004</p>
- * <p>Company: </p>
- * @author 段洪傑
- * @version 1.0
- */
- public class BoardTreeDAOImpl extends HibernateDaoSupport implements
- IBoardTreeDAO {
- /**
- * 構造函數
- */
- public BoardTreeDAOImpl(); {
- super();;
- }
- /**
- * 通過ID取得版塊
- * @param id String
- * @return Board
- */
- public Board getBoardById(String id); {
- Board board = (Board); getHibernateTemplate();.load(Board.class, id);;
- return board;
- }
- /**
- * 取根葉
- * @return Iterator
- */
- public Iterator getRoots(); throws HibernateException {
- String queryString =
- "select board from Board as board where board.parentId='root' order by board.id desc";
- List roots = getHibernateTemplate();.find(queryString);;
- return roots.iterator();;
- }
- /**
- * 存根葉
- * @param board Board
- */
- public void setRoot(Board board); {
- board.setParentId("root");;
- getHibernateTemplate();.save(board);;
- }
- /**
- * 取子葉
- * @param parentid String
- * @return List
- */
- public Iterator getChildren(String parentid); {
- /*
- String queryString =
- "select board as Board where board.parent_id='parentid' order by board.id desc";
- List children = getHibernateTemplate();.find(queryString);;
- return children;
- */
- Board parent = (Board); getHibernateTemplate();.load(Board.class, parentid);;
- return parent.getChildren();.iterator();;
- }
- /**
- * 取子葉數
- * @param parentid String
- * @return int
- */
- public int getChildrenCount(String parentid); {
- /*
- String queryString =
- "select count(*); Board where board.parent_id='parentid' order by board.id desc";
- List children = getHibernateTemplate();.find(queryString);;
- int count = ((Integer); children.iterator();.next(););.intValue();;
- return count;
- */
- Board parent = (Board); getHibernateTemplate();.load(Board.class, parentid);;
- int count = parent.getChildren();.size();;
- return count;
- }
- /**
- * 存子葉
- * @param parentLeaf Leaf
- */
- public void setChild(Board board, String parentid); {
- board.setParentId(parentid);;
- getHibernateTemplate();.save(board);;
- }
- /**
- *
- * 刪除該葉和它的子葉
- * @param board Board
- */
- public void deleteBranch(Board board); {
- getHibernateTemplate();.delete(board);;
- }
- /**
- * 根據子葉得到父葉
- * @param child Board
- * @return Board
- */
- public Board getParentByChild(Board child); {
- String parentId = child.getParentId();;
- Board parent = (Board); getHibernateTemplate();.load(Board.class, parentId);;
- return parent;
- }
- /**
- * 通過子ID得到父葉
- * @param id String
- * @return Board
- */
- public Board getParentByChildId(String id); {
- Board child = (Board); getHibernateTemplate();.load(Board.class, id);;
- Board parent = (Board); getHibernateTemplate();.load(Board.class,child.getParentId(););;
- return parent;
- }
- }
4.做service層代碼
- package infoweb.service;
- import java.util.List;
- import java.util.Iterator;
- import infoweb.dao.BoardTreeDAOImpl;
- import infoweb.dao.IBoardTreeDAO;
- import infoweb.pojo.Board;
- import infoweb.exception.BoardException;
- import net.sf.hibernate.HibernateException;
- /**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c); 2004</p>
- * <p>Company: </p>
- * @author 段洪傑
- * @version 1.0
- */
- public class BoardServiceSpringImpl implements IBoardService {
- private IBoardTreeDAO boardTreeDAO;
- public BoardServiceSpringImpl(); {
- super();;
- }
- /**
- * 取所有roots版塊
- * @return Iterator
- */
- public Iterator getRoots(); throws BoardException {
- Iterator roots = null;
- try {
- roots = boardTreeDAO.getRoots();;
- } catch (Exception ex); {
- throw new BoardException("取ROOT版塊時出錯! " + ex.toString(););;
- }
- return roots;
- }
- /**
- * 增加Root新版塊
- * @param board Board
- */
- public void setRoot(Board board); throws BoardException {
- try {
- boardTreeDAO.setRoot(board);;
- } catch (Exception ex); {
- throw new BoardException("增加ROOT版塊時出錯! " + ex.toString(););;
- }
- }
- /**
- * 刪除版塊 (包含下級版塊);
- * @param board Board
- */
- public void removeBoard(Board board); throws BoardException {
- try {
- boardTreeDAO.deleteBranch(board);;
- } catch (Exception ex); {
- throw new BoardException("刪除版塊時出錯! " + ex.toString(););;
- }
- }
- /**
- *
- * @return IBoardTreeDAO
- */
- public IBoardTreeDAO getBoardTreeDAO(); {
- return boardTreeDAO;
- }
- /**
- *
- * @param boardTreeDAO IBoardTreeDAO
- */
- public void setBoardTreeDAO(IBoardTreeDAO boardTreeDAO); {
- this.boardTreeDAO = boardTreeDAO;
- }
- }
5.做ACTION的父類
- package infoweb.web;
- import javax.servlet.ServletContext;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionServlet;
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
- import infoweb.service.IBoardService;
- /**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c); 2004</p>
- * <p>Company: </p>
- * @author 段洪傑
- * @version 1.0
- */
- public class BaseAction extends Action {
- private IBoardService boardService;
- public void setServlet(ActionServlet actionServlet); {
- super.setServlet(actionServlet);;
- ServletContext servletContext = actionServlet.getServletContext();;
- WebApplicationContext wac =
- WebApplicationContextUtils.getRequiredWebApplicationContext(
- servletContext);;
- this.boardService = (IBoardService); wac.getBean("boardService");;
- }
- protected IBoardService getBoardService(); {
- return boardService;
- }
- }
6.做action類
- package infoweb.web;
- import infoweb.pojo.Board;
- import org.apache.commons.beanutils.PropertyUtils;
- import org.apache.struts.action.*;
- import org.apache.log4j.Logger;
- import javax.servlet.http.*;
- import java.util.Iterator;
- import java.util.Date;
- /**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c); 2004</p>
- * <p>Company: </p>
- * @author 段洪傑
- * @version 1.0
- */
- public class SetBoardAction extends BaseAction {
- private static Logger log = Logger.getLogger(SetBoardAction.class);;
- public ActionForward execute(ActionMapping actionMapping,
- ActionForm actionForm,
- HttpServletRequest httpServletRequest,
- HttpServletResponse httpServletResponse); throws
- Exception {
- // SessionBean sessionBean = (SessionBean); httpServletRequest.getSession();.getAttribute("sessionBean");;
- BoardForm boardForm = (BoardForm); actionForm;
- //String backURL = httpServletRequest.getHeader("Referer");;
- /*
- if (sessionBean==null||!sessionBean.getIsLogon();); {
- httpServletRequest.setAttribute("message", "系統超時,或者沒有登錄 .返回重新登錄!");;
- httpServletRequest.setAttribute("locationFile",
- "location='index.jsp';");;
- return actionMapping.findForward("message");;
- }
- */
- Board board = new Board();;
- boardForm.setCreateDate(new Date(););;
- PropertyUtils.copyProperties(board, boardForm);;
- getBoardService();.setRoot(board);;
- httpServletRequest.setAttribute("message", "版塊信息錄入完成!");;
- httpServletRequest.setAttribute("locationFile",
- "<A HREF=\"javascript:history.back();\">返回</A>");;
- return (actionMapping.findForward("success"););;
- }
- }