Integrated struts Spring Hibernate

xiaoxiao2021-03-06  39

(Quick Knife Original) Below is the note I learned Struts Spring Hibernate. 1.Web.xml configuration: first add: Join in Web.xml:

Java code:

SpringContextServlet

Class> ORG.

Springframework.

Web.

CONTEXT.

ContextLoaderServlet

Class>

1

My entire web.xml is like this:

Java code:

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 ">

Info_Web

contextconfiglocation

/ web-inf / ApplicationContext-hibernate.

XML

setcharacterencoding

Class> ICLASS.

Util.

SetEncodingFilter

Class>

Encoding

GBK

filterRedirector

Class> ORG.

Apache.

CACTUS.

Server.

FiltestRedirector

Class>

setcharacterencoding

/ * filterRedirector / filterredirector SpringContextServlet org.springframework.web.context.ContextLoaderServlet 1 org.apache.cactus.server.ServlettestRedirector jspredirector /jspredirector.jsp Action ORG. Apache.struts.Action.ActionServlet config /web-inf/struts-config.xml debug 2 Application ApplicationResources 2 servletredirector / servletredirector < servlet-mapping> <

Servlet-Name> jspredirector / jspredirector action *. do index.jsp < Taglib-uri> / tags / struts-nested /web-inf/struts-nested.tld / Web-inf / struts-bean.tld /web-inf/struts-bean.tld / web -Inf / struts-html.tld /web-inf/struts-html.tld / web- INF / STRUTS-LOGIC.TLD /web-inf/struts-logic.tld / web-inf /struts-tiles.tld /web-inf/struts-tiles.tld /web-inf/struts-nested.tld /web-inf/struts-nested.tld 2.spring configuration configuration file is ApplicationContext-hibernate.xml, in this configuration file, with POJO layer, commercial logic, DAO layer, and transaction management

Java code:

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 ">

ORG.

GJT.

MM.

Mysql.

Driver

JDBC: MySQL:

// localhost: 3306 / info_web? useunicode = true & characterencoding = GBK

root

123456

Class = "ORG.

Springframework.

ORM.

Hibernate.

LocalSessionFactoryBean>>

Infoweb / Pojo / Answer.

HBM.

XML

Infoweb / Pojo / Board.

HBM.

XML

Infoweb / Pojo /

Image.

HBM.

XML

Infoweb / Pojo / Info.

HBM.

XML

net.sf.

Hibernate.

Diagect.

MySQLDIALECT

>

True

True

For A Single Hibernate SessionFactory

(Alternative to JTA

) ->

Class = "ORG.

Springframework.

ORM.

Hibernate.

HibernateTransactionManager ">

Class = "ORG.

Springframework.

TRANSACTION.

Interceptor.

TransactionProxyFactoryBean>>

Propagation_required, readonly, -boardException

PropAgation_required, -boardException

PropAgation_required, -boardException

PropAgation_required, -boardException

Class = "infoweb.

DAO.

BoardtreedaoImpl>

Class = "infoweb.

DAO.

InfodaoImpl>

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 *

Description: Using tree structure *

Copyright: Copyright (c) 2004 *

Company: * @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: *

Description: *

Copyright: Copyright (c) 2004 *

Company: * @ 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: *

Description: *

Copyright: Copyright (c) 2004 *

Company: * @ 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: *

Description: *

Copyright: Copyright (c) 2004 *

Company: * @ 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",

"

BACK

(

) / "> Back "

);

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:

contextconfiglocation

/Web-inf/daocontext.xml /web-inf/applicationContext.xml

Org.springframework.web.context.contextloaderListener

context

Org.springframework.web.context.contextloaderServlet

1

->

转载请注明原文地址:https://www.9cbs.com/read-54336.html

New Post(0)