KehuiCMS文档-: 学习Struts+spring+hibernate的笔记 -可慧网络 KehuiCMS 内容管理系统官方网站

来源:百度文库 编辑:神马文学网 时间:2024/04/27 20:16:07
学习Struts+spring+hibernate的笔记
文章发表:ezerg  发表日期:2005-03-24  阅读次数:33
(快刀浪子原创)下面是我学习Struts+spring+hibernate的笔记.
1.WEB.XML的配置:
首先在web.xml中加入:
java代码: 
1
2  
3     SpringContextServlet
4     org.springframework.web.context.ContextLoaderServlet
5     1
6  

7
我的整个web.xml象这样:
java代码: 
1
2   
3   
4   
5     info_web
6    
7       contextConfigLocation
8       /WEB-INF/applicationContext-hibernate.xml
9    

10   
11      SetCharacterEncoding
12      iclass.util.SetEncodingFilter
13     
14        encoding
15        GBK
16     

17   

18   
19      FilterRedirector
20      org.apache.cactus.server.FilterTestRedirector
21   

22   
23      SetCharacterEncoding
24      /*
25   

26   
27      FilterRedirector
28      /FilterRedirector
29   

30   
31      SpringContextServlet
32      org.springframework.web.context.ContextLoaderServlet
33      1
34   

35   
36      ServletRedirector
37      org.apache.cactus.server.ServletTestRedirector
38   

39   
40      JspRedirector
41      /jspRedirector.jsp
42   

43   
44      action
45      org.apache.struts.action.ActionServlet
46     
47        config
48        /WEB-INF/struts-config.xml
49     

50     
51        debug
52        2
53     

54     
55        application
56        ApplicationResources
57     

58      2
59   

60   
61      ServletRedirector
62      /ServletRedirector
63   

64   
65      JspRedirector
66      /JspRedirector
67   

68   
69      action
70      *.do
71   

72   
73      index.jsp
74   

75   
76      /tags/struts-nested
77      /WEB-INF/struts-nested.tld
78   

79   
80      /WEB-INF/struts-bean.tld
81      /WEB-INF/struts-bean.tld
82   

83   
84      /WEB-INF/struts-html.tld
85      /WEB-INF/struts-html.tld
86   

87   
88      /WEB-INF/struts-logic.tld
89      /WEB-INF/struts-logic.tld
90   

91   
92      /WEB-INF/struts-tiles.tld
93      /WEB-INF/struts-tiles.tld
94   

95   
96      /WEB-INF/struts-nested.tld
97      /WEB-INF/struts-nested.tld
98   

99  

100
2.spring配置
配置文件为applicationContext-hibernate.xml,在此配置文件中,配了POJO层,商业逻辑层,DAO层,和事务管理
java代码: 
1
2  
3  
4
5  
6         
7
8         
9
10        
11                
12                         org.gjt.mm.mysql.Driver
13                

14                
15                         jdbc:mysql://localhost:3306/info_web?useUnicode=true&characterEncoding=GBK
16                

17                
18                         root
19                

20                
21                         123456
22                

23        

24
25        
26                
27                        
28                                 infoweb/pojo/Answer.hbm.xml
29                                 infoweb/pojo/Board.hbm.xml
30                                 infoweb/pojo/Image.hbm.xml
31                                 infoweb/pojo/Info.hbm.xml
32                        

33                

34
35                
36                        
37                                 net.sf.hibernate.dialect.MySQLDialect
38                                 true
39                                 true
40                        

41                

42
43                
44        

45
46
47
48        
49        
50                
51        

52
53
54        
55        
56                
57                
58                
59                        
60                                 PROPAGATION_REQUIRED,readOnly,-BoardException
61                                 PROPAGATION_REQUIRED,-BoardException
62                                 PROPAGATION_REQUIRED,-BoardException
63                                 PROPAGATION_REQUIRED,-BoardException
64                        

65                

66        

67
68        
69        
70                
71        

72
73
74        
75        
76                
77        

78
79        
80        
81                
82        

83 

84
85
3.做DAO代码,
java代码: 
1
2   package infoweb.dao;
3
4   import java.util.List;
5   import java.util.Iterator;
6
7   import infoweb.pojo.Board;
8
9
10  import net.sf.hibernate.HibernateException;
11  import net.sf.hibernate.Query;
12  import net.sf.hibernate.Session;
13
14  import org.springframework.orm.hibernate.HibernateCallback;
15  import org.springframework.orm.hibernate.support.HibernateDaoSupport;
16
17
18  /**
19   *

Title: 版块分类DAOImpl


20   *

Description: 用树型结构实现


21   *

Copyright: Copyright (c) 2004


22   *

Company:


23   * @author 段洪杰
24   * @version 1.0
25   */
26
27
28  public class BoardTreeDAOImpl extends HibernateDaoSupport implements
29      IBoardTreeDAO {  30
31
32    /**
33     * 构造函数
34     */
35    public BoardTreeDAOImpl() {  36      super();
37    }
38
39
40    /**
41     * 通过ID取得版块
42     * @param id String
43     * @return Board
44     */
45
46    public Board getBoardById(String id) {  47      Board board = (Board) getHibernateTemplate().load(Board.class, id);
48      return board;
49    }
50
51
52    /**
53     * 取根叶
54     * @return Iterator
55     */
56    public Iterator getRoots() throws HibernateException {  57      String queryString =
58          "select board from Board as board where board.parentId=‘root‘ order by board.id desc";
59      List roots = getHibernateTemplate().find(queryString);
60      return roots.iterator();
61    }
62
63
64    /**
65     * 存根叶
66     * @param board Board
67     */
68    public void setRoot(Board board) {  69      board.setParentId("root");
70      getHibernateTemplate().save(board);
71    }
72
73
74    /**
75     * 取子叶
76     * @param  parentid String
77     * @return List
78     */
79    public Iterator getChildren(String parentid) {  80      /*
81           String queryString =
82       "select board as Board where board.parent_id=‘parentid‘ order by board.id desc";
83           List children = getHibernateTemplate().find(queryString);
84           return children;
85       */
86      Board parent = (Board) getHibernateTemplate().load(Board.class, parentid);
87      return parent.getChildren().iterator();
88    }
89
90
91    /**
92     * 取子叶数
93     * @param parentid String
94     * @return int
95     */
96
97    public int getChildrenCount(String parentid) {  98      /*
99           String queryString =
100      "select count(*) Board where board.parent_id=‘parentid‘ order by board.id desc";
101          List children = getHibernateTemplate().find(queryString);
102          int count = ((Integer) children.iterator().next()).intValue();
103          return count;
104      */
105     Board parent = (Board) getHibernateTemplate().load(Board.class, parentid);
106     int count = parent.getChildren().size();
107     return count;
108   }
109
110
111   /**
112    * 存子叶
113    * @param parentLeaf Leaf
114    */
115   public void setChild(Board board, String parentid) {  116     board.setParentId(parentid);
117     getHibernateTemplate().save(board);
118   }
119
120
121   /**
122    *
123    * 删除该叶和它的子叶
124    * @param board Board
125    */
126   public void deleteBranch(Board board) {  127       getHibernateTemplate().delete(board);
128   }
129
130
131   /**
132    * 根据子叶得到父叶
133    * @param child Board
134    * @return Board
135    */
136   public Board getParentByChild(Board child) {  137     String parentId = child.getParentId();
138     Board parent = (Board) getHibernateTemplate().load(Board.class, parentId);
139     return parent;
140   }
141
142
143   /**
144    * 通过子ID得到父叶
145    * @param id String
146    * @return Board
147    */
148   public Board getParentByChildId(String id) {  149     Board child = (Board) getHibernateTemplate().load(Board.class, id);
150     Board parent = (Board) getHibernateTemplate().load(Board.class,child.getParentId());
151     return parent;
152   }
153 }
154
155
4.做service层代码
java代码: 
1
2  package infoweb.service;
3
4  import java.util.List;
5  import java.util.Iterator;
6  import infoweb.dao.BoardTreeDAOImpl;
7  import infoweb.dao.IBoardTreeDAO;
8  import infoweb.pojo.Board;
9  import infoweb.exception.BoardException;
10 import net.sf.hibernate.HibernateException;
11
12 /**
13  *

Title:


14  *

Description:


15  *

Copyright: Copyright (c) 2004


16  *

Company:


17  * @author 段洪杰
18  * @version 1.0
19  */
20 public class BoardServiceSpringImpl implements IBoardService {  21
22     private IBoardTreeDAO boardTreeDAO;
23
24     public BoardServiceSpringImpl() {  25         super();
26     }
27
28     /**
29      * 取所有roots版块
30      * @return Iterator
31      */
32     public Iterator getRoots() throws BoardException {  33         Iterator roots = null;
34         try {  35             roots = boardTreeDAO.getRoots();
36         } catch (Exception ex) {
37             throw new BoardException("取ROOT版块时出错! " + ex.toString());
38         }
39         return roots;
40     }
41
42     /**
43      * 增加Root新版块
44      * @param board Board
45      */
46     public void setRoot(Board board) throws BoardException {47         try {  48             boardTreeDAO.setRoot(board);
49         } catch (Exception ex) {
50             throw new BoardException("增加ROOT版块时出错! " + ex.toString());
51         }
52     }
53
54     /**
55      * 删除版块 (包含下级版块)
56      * @param board Board
57      */
58     public void removeBoard(Board board) throws BoardException {59         try {  60             boardTreeDAO.deleteBranch(board);
61         } catch (Exception ex) {
62             throw new BoardException("删除版块时出错! " + ex.toString());
63         }
64     }
65
66     /**
67      *
68      * @return IBoardTreeDAO
69      */
70     public IBoardTreeDAO getBoardTreeDAO() {  71         return boardTreeDAO;
72     }
73
74     /**
75      *
76      * @param boardTreeDAO IBoardTreeDAO
77      */
78     public void setBoardTreeDAO(IBoardTreeDAO boardTreeDAO) {  79         this.boardTreeDAO = boardTreeDAO;
80     }
81
82 }
83
84
5.做ACTION的父类
java代码: 
1
2  package infoweb.web;
3
4
5  import javax.servlet.ServletContext;
6  import org.apache.struts.action.Action;
7  import org.apache.struts.action.ActionServlet;
8  import org.springframework.web.context.WebApplicationContext;
9  import org.springframework.web.context.support.WebApplicationContextUtils;
10
11 import infoweb.service.IBoardService;
12
13
14 /**
15  *

Title:


16  *

Description:


17  *

Copyright: Copyright (c) 2004


18  *

Company:


19  * @author 段洪杰
20  * @version 1.0
21  */
22
23 public class BaseAction extends Action {  24
25   private IBoardService boardService;
26
27   public void setServlet(ActionServlet actionServlet) {  28     super.setServlet(actionServlet);
29     ServletContext servletContext = actionServlet.getServletContext();
30     WebApplicationContext wac =
31         WebApplicationContextUtils.getRequiredWebApplicationContext(
32         servletContext);
33     this.boardService = (IBoardService) wac.getBean("boardService");
34   }
35
36   protected IBoardService getBoardService() {  37     return boardService;
38   }
39
40 }
41
42
6.做action类
java代码: 
1
2  package infoweb.web;
3
4  import infoweb.pojo.Board;
5  import org.apache.commons.beanutils.PropertyUtils;
6  import org.apache.struts.action.*;
7  import org.apache.log4j.Logger;
8  import javax.servlet.http.*;
9  import java.util.Iterator;
10 import java.util.Date;
11
12 /**
13  *

Title:


14  *

Description:


15  *

Copyright: Copyright (c) 2004


16  *

Company:


17  * @author 段洪杰
18  * @version 1.0
19  */
20
21
22 public class SetBoardAction extends BaseAction {  23
24     private static Logger log = Logger.getLogger(SetBoardAction.class);
25
26     public ActionForward execute(ActionMapping actionMapping,
27                                  ActionForm actionForm,
28                                  HttpServletRequest httpServletRequest,
29                                  HttpServletResponse httpServletResponse) throws
30             Exception {  31
32         // SessionBean sessionBean = (SessionBean) httpServletRequest.getSession().getAttribute("sessionBean");
33         BoardForm boardForm = (BoardForm) actionForm;
34         //String backURL = httpServletRequest.getHeader("Referer");
35         /*
36         if (sessionBean==null||!sessionBean.getIsLogon()) {
37             httpServletRequest.setAttribute("message", "系统超时,或者没有登录 .返回重新登录!");
38             httpServletRequest.setAttribute("locationFile",
39                                             "location=‘index.jsp‘;");
40             return actionMapping.findForward("message");
41         }
42         */
43         Board board = new Board();
44         boardForm.setCreateDate(new Date());
45         PropertyUtils.copyProperties(board, boardForm);
46         getBoardService().setRoot(board);
47
48         httpServletRequest.setAttribute("message", "版块信息录入完成!");
49         httpServletRequest.setAttribute("locationFile",
50                                         "返回");
51         return (actionMapping.findForward("success"));
52     }
53
54 }
55
56
_xyz