(Quick Knife Original) Below is the note I learned Struts Spring Hibernate. 1.Web.xml configuration: first add: Join in Web.xml:
Java code:
1
2
3
4 Class> ORG. Springframework. Web. CONTEXT. ContextLoaderServlet servlet- Class> 5 1 loading-on-startup> 6 servlet> Seduce My entire web.xml is like this: Java code: 1 2 XML Version = " 1. 0 "encoding =" UTF- 8 "?> 3
PUBLIC "- // sun microsystems, Inc.//dtd Web Application 2.3 // en "" http://java.sun.com/dtd/web-app_2_3.dtd "> 4 5 6 7 8 XML param-value> 9 context-param> 10 11 12 Class> ICLASS. Util. SetEncodingFilter filter- Class> 13 14 15 16 init-param> 17 filter> 18 19 20 Class> ORG. Apache. CACTUS. Server. FiltestRedirector filter- Class> 21 filter> 22 23 24 / * url-pattern> 25 filter-maping> 26 servlet-name> servletredirector servlet-name> 62 Taglib-location> /web-inf/struts-nested.tld taglib-location> 98 taglib> 99 web-app> 1002.spring Configuration Profile is ApplicationContext-Hibernate.xml, in this configuration file With POJO layers, commercial logic, DAO layers, and transaction management Java code: 1 2 XML Version = " 1. 0 "encoding =" UTF- 8 "?> 3
PUBLIC "- // Spring // DTD bean // en "" http://www.springframework.org/dtd/spring-beans.dtd "> 4 5 6 Seduce 8 9 10 Class = "ORG. Apache. COMMONS. DBCP. BasicDataSource "Destroy-method =" close "> 11 12 GJT. MM. Mysql. Driver value> 13 Property> 14 15 // localhost: 3306 / info_web? useunicode = true & characterencoding = GBK value> 16 Property> 17 18 19 Property> 20 21 123456 Value> 22 property> 23 bean> twenty four 25 Class = "ORG. Springframework. ORM. Hibernate. LocalSessionFactoryBean>> 26 27 28 HBM. XML value> 29 HBM. XML value> 30 Image. HBM. XML value> 31 HBM. XML value> 32 List> 33 Property> 34 35 36 37 sf. Hibernate. Diagect. MySQLDIALECT Prop> 38 True prop> 39 True prop> 40 prOPS> 41 Property> 42 44 bean> 45 46 47 48
For A Single Hibernate SessionFactory (Alternative to JTA ) -> 49 Class = "ORG. Springframework. ORM. Hibernate. HibernateTransactionManager "> 51 bean> 52 53 54 55 Class = "ORG. Springframework. TRANSACTION. Interceptor. TransactionProxyFactoryBean>> 58 59 60 61 62 63 64 prOPs> 65 Property> 66 bean> 67 68 69 Class = "infoweb. SERVICE. BoardServicesPRINGIMPL "> 70 72 73 74 75 Class = "infoweb. DAO. BoardtreedaoImpl> 77 bean> 78 79! - DAO Info: Hibernate Implementation -> 80 Class = "infoweb. DAO. InfodaoImpl> 82 bean> 83 beans> 84 85 3. Do DAO code, Java code: 1 2 Package infoweb. DAO; 3 4 Import Java. Util. List; 5 Import Java. Util. Iterator; 6 Seduce 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 In one 18 / ** 19 * Title: section Classification DaoImpl p> 20 * Description: Using tree structure p> 21 * Copyright: Copyright (c) 2004 p> 22 * Company: p> 23 * @author Duan Hongjie 24 * @version 1.0 25 * / 26 27 Twist public Class BoardtreedaoImpl Extends Hibernatedaosupport IMPLEMENTS 29 iBoardTreedao { ...} 30 31 32 / ** 33 * Constructor 34 * / 35 Public BoardTreedaoImpl ) { ...} 36 Super ( ); 37 } 38 39 40 / ** 41 * Take the text through the 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 * Take the root of the leaf 54 * @return itrator 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 * stub 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. Getildren ( ). Iterator ( ); 88 } 89 90 91 / ** 92 * Take a child 93 * @param parentid string 94 * @return int 95 * / 96 97 Publicint 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 = pent. Getildren ( ). Size ( ); 107 Return count; 108 } 109 110 111 / ** 112 * Saved Leaves 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 * Delete the leaves and its sub-leaves 124 * @Param Board Board 125 * / 126 public Void deletebranch (Board Board) ) { ...} 127 GethibernateTemplate ( ). Delete (Board) ); 128 } 129 130 131 / ** 132 * Depending on the sub-leaf to get the father of the leaves 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 * Get the parent Leaf 145 * @Param ID String 146 * @return board 147 * @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. Do Service layer code Java code: 1 2 Package infoweb. Service; 3 4 Import Java. Util. List; 5 Import Java. Util. Iterator; 6 Import Infoweb. DAO. BoardtreedaoImpl; Seduce Import Infoweb. DAO. IBoardTreedao; 8 Import Infoweb. Pojo. Board; 9 Import Infoweb. EXCEPTION. BoardException; 10 Import Net. sf. Hibernate. HibernateException; 11 12 / ** 13 * Title: p> 14 * Description: p> 15 * Copyright: Copyright (c) 2004 p> 16 * Company: P> 17 * @author Duan Hongjie 18 * @version 1.0 19 * / 20 public Class BoardServicesPRINGIMPL Implements iBoardService { ...} twenty one twenty two Private iBoardtreedao Boardtreeda twenty three twenty four Public BoardServicesPRINGIMPL ( ) { ...} 25 Super ( ); 26 } 27 Twist / ** 29 * Take all ROOTS section 30 * @return item 31 * / 32 public Iterator getroots ( ) Throws BoardException { ...} 33 Iterator roots = NULL; 34 Try { ...} 35 roots = boardtreedao. Gtroots ( ); 36 } Catch ( Exception EX ) { 37 throw New boardexception ("An error occurred while taking the root section!" EX. Tostring ( ) ); 38 } 39 Return roots; 40 } 41 42 / ** 43 * Increase root new block 44 * @Param Board Board 45 * / 46 public Void setRoot (Board Board) ) Throws BoardException { ...} 47 Try { ...} 48 Boardtreedao. Stroot (Board) ); 49 } Catch ( Exception EX ) { 50 throw New boardexception ("Error increasing the root section!" EX. Tostring ( ) ); 51 } 52 } 53 54 / ** 55 * Delete section (including sub-section) 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 ("Error when deleting the section!" EX. Tostring ( ) ); 63 } 64 } 65 66 / ** 67 * 68 * @return iBoardTreedao 69 * / 70 Public iBoardtreedao getBoardTreedao ( ) { ...} 71 Return Boardtreeda; 72 } 73 74 / ** 75 * 76 * @Param Boardtreedao iBoardTreedao 77 * / 78 public Void setBoardTreedao (IBoardtreedao Boardtreedao ) { ...} . Boardtreedao = boardtreedao; 80 } 81 82 } 83 84 5. Do the action of the parent class Java code: 1 2 Package infoweb. Web; 3 4 5 Import Javax. servlet. ServletContext; 6 Import org. Apache. Struts. Action. Action; Seduce 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: p> 16 * description: p> 17 * Copyright: Copyright (c) 2004 p> 18 * Company: P> 19 * @author Duan Hongjie 20 * @version 1.0 21 * / twenty two twenty three public Class baseAction Extends Action { ...} twenty four 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. Do Action class Java code: 1 2 Package infoweb. Web; 3 4 Import Infoweb. Pojo. Board; 5 Import org. Apache. COMMONS. Beanutils. ProPertyUtils; 6 Import org. Apache. Struts. Action. *; Seduce Import org. Apache. Log4j. Logger; 8 Import Javax. servlet. Http. *; 9 Import Java. Util. Iterator; 10 Import Java. Util. Date; 11 12 / ** 13 * Title: p> 14 * Description: p> 15 * Copyright: Copyright (c) 2004 p> 16 * Company: P> 17 * @author Duan Hongjie 18 * @version 1.0 19 * / 20 twenty one twenty two public Class setboardaction EXTENDS baseAction { ...} twenty three twenty four 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.getations (). GetAttribute ("sessionbean"); 33 BoardForm BoardForm = (BoardForm) Actionform; 34 // String backurl = httpservletRequest.getHeader ("Referer"); 35 / * 36 IF (sessionbean == null ||! SessionBean.getislogon ()) {37 httpservletRequest.setttribute ("Message", system timeout, or no login. Return to login! "); 38 httpservletRequest.setttribute (" LocationFile " ", 39" location = 'index.jsp'; "); 40 return anctionsMapping.FindForward (" message "); 41} 42 * / 43 board board = NEW BOARD ( ); 44 boardForm. SetCreatedate ( New Date ( ) ); 45 PropertyUtils. CopyProperties (Board, BoardForm ); 46 getBoardService ( ). Stroot (Board) ); 47 48 httpservletRequest. SetaTribute ("Message", "" Trouble "is completed!" ); 49 httpservletRequest. SetaTribute ("LocationFile", 50 "
BACK ( ) / "> Back ); 51 Return ActionMapping. FINDFORWARD ("Success" ) ); 52 } 53 54 } 55 56 Learning in ing Programming