Learn struts + Spring + Hibernate notes

xiaoxiao2021-03-06  81

(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 SpringContextServlet

4

Class> ORG.

Springframework.

Web.

CONTEXT.

ContextLoaderServlet

Class>

5

1

6

Seduce

My entire web.xml is like this:

Java code:

1

2

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 Info_Web

6

7 contextconfigLocation

8 / web-inf / ApplicationContext-hibernate.

XML

9

10

11 setcharacterencoding

12

Class> ICLASS.

Util.

SetEncodingFilter

Class>

13

14 Encoding

15 GBK

16

17

18

19 FilterRedirector

20

Class> ORG.

Apache.

CACTUS.

Server.

FiltestRedirector

Class>

21

22

23 setcharacterencoding

24

/ * 25 26 27 filterRedirector 28 / filterredirector 29 < / filter-mapping> 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.xm L 49 50 51 debug 52 2 53 54 55 Application 56 ApplicationResources 57 58 2 59 60 61 <

servlet-name> 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 < Taglib-uri> /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 < / taglib> 91 92 /web-inf/struts-tiles.tld 93 /Web-inf/struts-tiles.tld 94 95 96 /web-inf/struts-nested.tld 97 <

Taglib-location> /web-inf/struts-nested.tld 98 99 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

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

twenty four

25

Class = "ORG.

Springframework.

ORM.

Hibernate.

LocalSessionFactoryBean>>

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.

Diagect.

MySQLDIALECT

38 >

True

39

True

40

41

42

43

44

45

46

47

48

For A Single Hibernate SessionFactory

(Alternative to JTA

) ->

49

Class = "ORG.

Springframework.

ORM.

Hibernate.

HibernateTransactionManager ">

50

51

52

53

54

55

Class = "ORG.

Springframework.

TRANSACTION.

Interceptor.

TransactionProxyFactoryBean>>

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

Class = "infoweb.

SERVICE.

BoardServicesPRINGIMPL ">

70 71

72

73

74

75

Class = "infoweb.

DAO.

BoardtreedaoImpl>

76

77

78

79! - DAO Info: Hibernate Implementation ->

80

Class = "infoweb.

DAO.

InfodaoImpl>

81

82

83

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 20 *

Description: Using tree structure 21 *

Copyright: Copyright (c) 2004 22 *

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

Description: 15 *

Copyright: Copyright (c) 2004 16 *

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

description: 17 *

Copyright: Copyright (c) 2004 18 *

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

Description: 15 *

Copyright: Copyright (c) 2004 16 *

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

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

New Post(0)