(Quick Knife Original) Below is the note I learned Struts Spring Hibernate. 1.Web.xml configuration: first add: Join in Web.xml:
Java code:
Class> ORG. Springframework. Web. CONTEXT. ContextLoaderServlet servlet- Class> 1 loading-on-startup> servlet> My entire web.xml is like this: Java code: XML Version = " 1. 0 "encoding =" UTF- 8 "?>
PUBLIC "- // sun microsystems, Inc.//dtd Web Application 2.3 // en "" http://java.sun.com/dtd/web-app_2_3.dtd "> XML param-value> context-param> Class> ICLASS. Util. SetEncodingFilter filter- Class> init-param> filter> Class> ORG. Apache. CACTUS. Server. FiltestRedirector filter- Class> filter> / * url-pattern> Servlet-Name> jspredirector servlet-name> Java code: XML Version = " 1. 0 "encoding =" UTF- 8 "?>
PUBLIC "- // Spring // DTD bean // en "" http://www.springframework.org/dtd/spring-beans.dtd "> Class = "ORG. Apache. COMMONS. DBCP. BasicDataSource "Destroy-method =" close "> GJT. MM. Mysql. Driver value> Property> // localhost: 3306 / info_web? useunicode = true & characterencoding = GBK value> Property> Property> 123456 Value> Property> bean> Class = "ORG. Springframework. ORM. Hibernate. LocalSessionFactoryBean>> HBM. XML value> HBM. XML value> Image. HBM. XML value> HBM. XML value> list> Property> Hibernate. Diagect. MySQLDIALECT Prop> True prop> True prop> prOPS> Property> bean>
For A Single Hibernate SessionFactory (Alternative to JTA ) -> Class = "ORG. Springframework. ORM. Hibernate. HibernateTransactionManager "> bean> Class = "ORG. Springframework. TRANSACTION. Interceptor. TransactionProxyFactoryBean>> prOPS> Property> bean> bean> Class = "infoweb. DAO. BoardtreedaoImpl> bean> Class = "infoweb. DAO. InfodaoImpl> bean> beans> 3. Do DAO code, Java code: 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; / ** * Title: Trouk classification DAOIMPL P> * Description: Using tree structure p> * Copyright: Copyright (c) 2004 p> * Company: p> * @Author Duan Hongjie * @version 1.0 * / public Class BoardtreedaoImpl Extends Hibernatedaosupport IMPLEMENTS IBoardtreedao { /** * Constructor */ Public BoardTreedaoImpl ( ) { Super ( ); } / ** * Take the text through the ID * @Param ID String * @return board * / Public Board getBoardByid ( String ID ) { Board board = (Board) GethibernateTemplate ( ). Load (Board. Class, ID ); Return board; } / ** * Take the roots * @return itrator * / 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 ( ); } / ** * Store 叶 * @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. Getildren ( ). 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 = pent. Getildren ( ). Size ( ); Return count; / ** * Saved leaves * @Param Parentleaf Leaf * / public Void setChild (Board Board, String ParentID ) { Board. setParentID (ParentID) ); GethibernateTemplate ( ). Save (Board) ); } / ** * * Remove the leaf and its sub-leaf * @Param board board * / public Void deletebranch (Board Board) ) { GethibernateTemplate ( ). Delete (Board) ); } / ** * Depending on the child, get the father, @Param Child Board * @return board * / Public Board getParentByChild (Board CHild) ) { String ParentId = Child. GetParentID ( ); Board parent = (Board) GethibernateTemplate ( ). Load (Board. Class, ParentID ); Return Parent; } / ** * Get your father's leaf * @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. Do Service layer code Java code: 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; / ** * Title: p> * Description: p> * Copyright: Copyright (c) 2004 p> * Company: p> * @ Author Duan Hongjie * @version 1.0 * / public Class BoardServicesPRINGIMPL Implements iBoardService { Private iBoardtreedao Boardtreeda Public BoardServicesPRINGIMPL ( ) { Super ( ); } / ** * Take all ROOTS section * @return itrator * / PublicIitrator getroots ( ) Throws BoardException { Iterator roots = NULL; Try { Roots = Boardtreedao. Gtroots ( ); } Catch ( Exception EX ) { Throw New boardexception ("An error occurred while taking the root section!" EX. Tostring ( ) ); } Return roots; } / ** * Add root new block * @Param Board Board * / public Void setRoot (Board Board) ) Throws BoardException { Try { Boardtreedao. Stroot (Board) ); } Catch ( Exception EX ) { Throw New boardexception ("Error increasing the root section!" EX. Tostring ( ) ); } } / ** * Remove the section (including sub-section) * @Param Board Board * / public Void Removeboard (Board Board) ) Throws BoardException { Try { Boardtreedao. Deletebranch (Board) ); } Catch ( Exception EX ) { Throw New boardexception ("Error when deleting the section!" EX. Tostring ( ) ); } } / ** * * @return iBoardtreedao * / Public iBoardtreedao getBoardTreedao ( ) { Return Boardtreeda; } / ** * * @Param Boardtreedao iBoardTreedao * / public Void setBoardTreedao (IBoardtreedao Boardtreedao ) { THIS. Boardtreedao = boardtreedao; } } 5. Do the action of the parent class Java code: 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; / ** * Title: p> * Description: p> * Copyright: Copyright (c) 2004 p> * Company: p> * @ Author Duan Hongjie * @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. Do Action class Java code: 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; / ** * Title: p> * Description: p> * Copyright: Copyright (c) 2004 p> * Company: p> * @ Author Duan Hongjie * @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.getations (). GetAttribute ("sessionbean"); Boardform boardForm = (BoardForm) Actionform; // String backurl = httpservletRequest.getHeader ("Referer"); / * If (sessionbean == null ||! SessionBean.getislogon ()) {httpservletRequest.setttribute ("Message", system timeout, or no login. Return to login! "); HttpservletRequest.settribute (" locationfile "," Location = 'index.jsp'; "); return anctionMApping.findforward (" message ");} * / board board = NEW BOARD ( ); BoardForm. SetCreatedate ( New Date ( ) ); PropertyUtils. CopyProperties (Board, BoardForm ); GetBoardService ( ). Stroot (Board) ); HttpservletRequest. SetaTribute ("Message", "" Trouble "is completed!" ); HttpservletRequest. SetaTribute ("LocationFile", ); Return ActionMapping. FINDFORWARD ("Success" ) ); } } 3.15. Creating an ApplicationContext from a Web Application As opposed to the BeanFactory, which will often be created programmatically, ApplicationContexts can be created declaratively using for example a ContextLoader. Of course you can also create ApplicationContexts programmatically using one of the ApplicationContext implementations. First, let's examine the ContextLoader and its implementations. The ContextLoader has two implementations:.. The ContextLoaderListener and the ContextLoaderServlet They both have the same functionality but differ in that the listener can not be used in Servlet 2.2 compatible containers Since the Servlet 2.4 specification, listeners are required to initialize after startup of a web application . A lot of 2.3 compatible containers already implement this feature It is up to you as to which one you use, but all things being equal you should probably prefer ContextLoaderListener;. for more information on compatibility, have a look at the JavaDoc for the ContextLoaderServlet .You can register an ApplicationContext Using The ContextLoaderListener As Follows: context-param> listener>
servlet> ->