[导入][AppFuse] AppFuse使用手记--DAO(七) [原]
AppFuse生成的Action的提供了几个基本的CRUD方法。先看一下这几个方法的实现。
Actioni通过Spring注入GenericManager,而GenericManager的实现GenericManagerImpl则调用GenericDao完成DAO操作。Appfuse默认为Hibernate进行DAO操作,GenericManagerImpl的实现类为GenericDaoHibernate,而GenericDaoHibernate里面还是调用的Spring的HibernateTemplate实现DAO操作。
UML图如下(点击显示大图):
通常AppFuse提供的几个操作是远远不够的,如果我们需要完成自定义的功能则需要自己实现相应的DAO操作。可以参考Appfuse的实现(参考 Using Hibernate)。增加一个DAO类继承GenericDao,增加一个DAO实现类继承GenericDaoHibernate实现DAO类。
UML图如下(点击显示大图):
在applicationContext.xml里声明DAO。然后在Action通过注入DAO,完成相应的Action操作。
相关试例代码的主要部分如下:
CompanyDao :
CompanyDaoHibernate:
applicationContext.xml:
struts.xml:
CompanyAction:
companyList.jsp
文章来源: http://heyday.blogcn.com/diary,15384409.shtml
Actioni通过Spring注入GenericManager,而GenericManager的实现GenericManagerImpl则调用GenericDao完成DAO操作。Appfuse默认为Hibernate进行DAO操作,GenericManagerImpl的实现类为GenericDaoHibernate,而GenericDaoHibernate里面还是调用的Spring的HibernateTemplate实现DAO操作。
UML图如下(点击显示大图):
![[导入][AppFuse] AppFuse使用手记--DAO(七) [原]_第1张图片](http://img.e-com-net.com/image/product/7ad0790c58a24963a7abd3c90f179b99.jpg)
通常AppFuse提供的几个操作是远远不够的,如果我们需要完成自定义的功能则需要自己实现相应的DAO操作。可以参考Appfuse的实现(参考 Using Hibernate)。增加一个DAO类继承GenericDao,增加一个DAO实现类继承GenericDaoHibernate实现DAO类。
UML图如下(点击显示大图):
![[导入][AppFuse] AppFuse使用手记--DAO(七) [原]_第2张图片](http://img.e-com-net.com/image/product/9697222b83704f4dbef0a2138ce64e39.jpg)
在applicationContext.xml里声明DAO。然后在Action通过注入DAO,完成相应的Action操作。
相关试例代码的主要部分如下:
CompanyDao :
1
package
com.reda.app.dao;
2
3
import
com.reda.app.model.Company;
4
import
org.appfuse.dao.GenericDao;
5
6
import
java.util.List;
7
8
public
interface
CompanyDao
extends
GenericDao
<
Company, Long
>
{
9
public List find(Company company);
10
}
11

2

3

4

5

6

7

8



9

10

11

CompanyDaoHibernate:
1
package
com.reda.app.dao.hibernate;
2
3
import
com.reda.app.model.Company;
4
import
com.reda.app.dao.CompanyDao;
5
import
org.appfuse.dao.hibernate.GenericDaoHibernate;
6
7
import
java.util.List;
8
9
public
class
CompanyDaoHibernate
extends
GenericDaoHibernate
<
Company, Long
>
implements
CompanyDao
{
10
11
public CompanyDaoHibernate()
{
12
super(Company.class);
13
}
14
15
public List find(Company company)
{
16
return super.getHibernateTemplate().find("from Company where companyType=?", company.getCompanyType());
17
}
18
19
}

2

3

4

5

6

7

8

9



10

11



12

13

14

15



16

17

18

19

applicationContext.xml:
1
<!--
Add new DAOs here
-->
2
<
bean
id
="companyDao"
class
="com.reda.app.dao.hibernate.CompanyDaoHibernate"
>
3
<
property
name
="sessionFactory"
ref
="sessionFactory"
/>
4
</
bean
>

2

3

4

struts.xml:
1
<!--
CompanyAction-START
-->
2
<
action
name
="companies"
class
="com.reda.app.webapp.action.CompanyAction"
method
="list"
>
3
<
result
>
/WEB-INF/pages/companyList.jsp
</
result
>
4
</
action
>
5
6
<
action
name
="editCompany"
class
="com.reda.app.webapp.action.CompanyAction"
method
="edit"
>
7
<
result
>
/WEB-INF/pages/companyForm.jsp
</
result
>
8
<
result
name
="error"
>
/WEB-INF/pages/companyList.jsp
</
result
>
9
</
action
>
10
11
<
action
name
="saveCompany"
class
="com.reda.app.webapp.action.CompanyAction"
method
="save"
>
12
<
result
name
="input"
>
/WEB-INF/pages/companyForm.jsp
</
result
>
13
<
result
name
="cancel"
type
="redirect"
>
companies.html
</
result
>
14
<
result
name
="delete"
type
="redirect"
>
companies.html
</
result
>
15
<
result
name
="success"
type
="redirect"
>
companies.html
</
result
>
16
</
action
>
17
18
19
<
action
name
="searchCompany"
class
="com.reda.app.webapp.action.CompanyAction"
method
="search"
>
20
<
result
name
="input"
>
/WEB-INF/pages/companyList.jsp
</
result
>
21
<
result
>
/WEB-INF/pages/companyList.jsp
</
result
>
22
</
action
>
23
24
<!--
CompanyAction-END
-->

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

CompanyAction:
1
package
com.reda.app.webapp.action;
2
3
import
com.opensymphony.xwork2.Preparable;
4
import
com.reda.app.model.Company;
5
import
com.reda.app.model.CompanyType;
6
import
com.reda.app.dao.hibernate.CompanyDaoHibernate;
7
import
org.appfuse.service.GenericManager;
8
import
org.appfuse.webapp.action.BaseAction;
9
10
import
java.util.List;
11
12
public
class
CompanyAction
extends
BaseAction
implements
Preparable
{
13
private GenericManager<Company, Long> companyManager;
14
private GenericManager<CompanyType, Long> companyTypeManager;
15
private List companies;
16
private Company company;
17
private Long companyId;
18
19
private CompanyDaoHibernate companyDao;
20
21
public void setCompanyManager(GenericManager<Company, Long> companyManager)
{
22
this.companyManager = companyManager;
23
}
24
25
public void setCompanyTypeManager(GenericManager<CompanyType, Long> companyTypeManager)
{
26
this.companyTypeManager = companyTypeManager;
27
}
28
29
public List getCompanies()
{
30
return companies;
31
}
32
33
public void setCompanyDao(CompanyDaoHibernate companyDao)
{
34
this.companyDao = companyDao;
35
}
36
37
public String search()
{
38
companies = companyDao.find(company);
39
return SUCCESS;
40
}
41
42
public List getCompanyTypeList()
{
43
return companyTypeManager.getAll();
44
}
45
46
/** *//**
47
* Grab the entity from the database before populating with request parameters
48
*/
49
public void prepare()
{
50
if (getRequest().getMethod().equalsIgnoreCase("post"))
{
51
// prevent failures on new
52
String companyId = getRequest().getParameter("company.companyId");
53
// if (companyId != null && !companyId.equals("")) {
54
// company = companyManager.get(new Long(companyId));
55
// }
56
}
57
}
58
59
public String list()
{
60
companies = companyManager.getAll();
61
return SUCCESS;
62
}
63
64
public void setCompanyId(Long companyId)
{
65
this.companyId = companyId;
66
}
67
68
public Company getCompany()
{
69
return company;
70
}
71
72
public void setCompany(Company company)
{
73
this.company = company;
74
}
75
76
public String delete()
{
77
companyManager.remove(company.getCompanyId());
78
saveMessage(getText("company.deleted"));
79
80
return SUCCESS;
81
}
82
83
public String edit()
{
84
if (companyId != null)
{
85
company = companyManager.get(companyId);
86
} else
{
87
company = new Company();
88
}
89
90
return SUCCESS;
91
}
92
93
public String save() throws Exception
{
94
if (cancel != null)
{
95
return "cancel";
96
}
97
98
if (delete != null)
{
99
return delete();
100
}
101
102
boolean isNew = (company.getCompanyId() == null);
103
104
companyManager.save(company);
105
106
String key = (isNew) ? "company.added" : "company.updated";
107
saveMessage(getText(key));
108
109
if (!isNew)
{
110
return INPUT;
111
} else
{
112
return SUCCESS;
113
}
114
}
115
}

2

3

4

5

6

7

8

9

10

11

12



13

14

15

16

17

18

19

20

21



22

23

24

25



26

27

28

29



30

31

32

33



34

35

36

37



38

39

40

41

42



43

44

45

46


47

48

49



50



51

52

53

54

55

56

57

58

59



60

61

62

63

64



65

66

67

68



69

70

71

72



73

74

75

76



77

78

79

80

81

82

83



84



85

86



87

88

89

90

91

92

93



94



95

96

97

98



99

100

101

102

103

104

105

106

107

108

109



110

111



112

113

114

115

companyList.jsp
1
<%
@ include file="/common/taglibs.jsp"
%>
2
3
<
head
>
4
<
title
><
fmt:message
key
="companyList.title"
/></
title
>
5
<
meta
name
="heading"
content
="<fmt:message key='companyList.heading'/>"
/>
6
<
meta
name
="menu"
content
="CompanyMenu"
/>
7
</
head
>
8
9
<
s:form
id
="companyForm"
action
="searchCompany"
method
="post"
validate
="true"
>
10
11
<
li
>
12
<
s:checkbox
key
="company.status"
cssClass
="checkbox"
theme
="simple"
/>
13
<
s:label
for
="companyForm_company_status"
value
="%{getText('company.status')}"
cssClass
="choice desc"
theme
="simple"
/>
14
<
s:label
for
="companyForm_company_companyType"
value
="%{getText('company.companyType.typeName')}"
cssClass
="desc"
/>
15
<
s:select
name
="company.companyType.typeId"
list
="companyTypeList"
listKey
="typeId"
listValue
="typeName"
></
s:select
>
16
<
s:textfield
key
="company.companyName"
required
="true"
maxlength
="200"
cssClass
="text medium"
/>
17
</
li
>
18
<
li
class
="buttonBar bottom"
>
19
<
s:submit
cssClass
="button"
method
="search"
key
="button.search"
theme
="simple"
/>
20
</
li
>
21
</
s:form
>
22
23
24
<
c:set
var
="buttons"
>
25
<
input
type
="button"
style
="margin-right: 5px"
class
="button"
26
onclick
="location.href='<c:url value="
/editCompany.html"
/>
'"
27
value="
<
fmt:message
key
="button.add"
/>
"/>
28
29
<
input
type
="button"
class
="button"
onclick
="location.href='<c:url value="
/mainMenu.html"
/>
'"
30
value="
<
fmt:message
key
="button.done"
/>
"/>
31
</
c:set
>
32
33
<
c:out
value
="${buttons}"
escapeXml
="false"
/>
34
35
36
<
display:table
name
="companies"
class
="table"
requestURI
=""
id
="companyList"
export
="true"
pagesize
="25"
>
37
<
display:column
property
="companyId"
sortable
="true"
href
="editCompany.html"
media
="html"
38
paramId
="companyId"
paramProperty
="companyId"
titleKey
="company.companyId"
/>
39
<
display:column
property
="companyId"
media
="csv excel xml pdf"
titleKey
="company.companyId"
/>
40
<
display:column
sortProperty
="status"
sortable
="true"
titleKey
="company.status"
>
41
<
input
type
="checkbox"
disabled
="disabled"
<c:if test
="${companyList.status}"
>
checked="checked"
</
c:if
>
/>
42
</
display:column
>
43
44
<
display:column
property
="companyType.typeName"
sortable
="true"
titleKey
="companyType.typeName"
/>
45
<
display:column
property
="companyName"
sortable
="true"
titleKey
="company.companyName"
/>
46
47
<
display:setProperty
name
="paging.banner.item_name"
><
fmt:message
key
="companyList.company"
/></
display:setProperty
>
48
<
display:setProperty
name
="paging.banner.items_name"
><
fmt:message
49
key
="companyList.companies"
/></
display:setProperty
>
50
51
<
display:setProperty
name
="export.excel.filename"
><
fmt:message
key
="companyList.title"
/>
.xls
</
display:setProperty
>
52
<
display:setProperty
name
="export.csv.filename"
><
fmt:message
key
="companyList.title"
/>
.csv
</
display:setProperty
>
53
<
display:setProperty
name
="export.pdf.filename"
><
fmt:message
key
="companyList.title"
/>
.pdf
</
display:setProperty
>
54
</
display:table
>
55
56
<
c:out
value
="${buttons}"
escapeXml
="false"
/>
57
58
<
script
type
="text/javascript"
>
59
highlightTableRows("companyList");
60
</
script
>
61



2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58



59

60

61

文章来源: http://heyday.blogcn.com/diary,15384409.shtml