struts2.0學(xué)習(xí)筆記(二)--常用UI標(biāo)記
Posted on 2008-08-20 12:33 ∪∩BUG 閱讀(1748) 評(píng)論(1) 編輯 收藏 所屬分類: Struts2學(xué)習(xí)筆記index.jsp
1
<%@page contentType="text/html; charset=GBK"%>
2
<%@taglib prefix="s" uri="/struts-tags"%>
3
4
<!--
5
Struts2UI標(biāo)簽的使用:
6
1.使用s:checkboxlist生成多個(gè)復(fù)選框。
7
2.使用s:combobox生成下拉輸入框
8
3.使用s:datetimepicker生成時(shí)間選擇器
9
4.使用s:doubleselect生成級(jí)聯(lián)下拉列表框
10
5.使用s:radio生成多個(gè)單選框
11
6.使用s:token可以添加隱藏的表單域,以防止頁面的重復(fù)提交操作
12
-->
13
14
<html>
15
<%
16
//用到s:datetimepicker時(shí)必加的標(biāo)簽
17
%>
18
19
<s:head />
20
<body>
21
<h3>
22
Struts2UI標(biāo)簽的使用:使用 s:checkboxlist生成多個(gè)復(fù)選框。
23
24
</h3>
25
<s:form>
26
<%
27
//使用簡(jiǎn)單的集合(數(shù)組)生成復(fù)選框,list是靜態(tài)列表選項(xiàng)
28
%>
29
<s:checkboxlist name="a" label="選擇圖書" labelposition="top"
30
list="{'Struts','J2EE','JSP','Hibernate'}" />
31
32
<%
33
//name="包名.DAO類名" id="產(chǎn)生的對(duì)象"
34
%>
35
<s:bean name="com.DAO.BookDAO" id="bs" />
36
37
<%
38
//1.list="#bs.books" 是產(chǎn)生的對(duì)象bs會(huì)到BookDAO.java調(diào)用getBooks()方法[注意:這里books首字母要小寫,BookDAO.java的getBooks()方法要大寫]
39
//2.listKey="name" 是復(fù)選框的實(shí)際取值,向后臺(tái)發(fā)送的數(shù)據(jù),listValue="author"是復(fù)選框的標(biāo)簽,顯示在前臺(tái)中的信息(label)
40
%>
41
<s:checkboxlist name="b" label="選擇圖書" labelposition="top"
42
list="#bs.books" listKey="name" listValue="author" />
43
44
<%
45
//使用Map集合生成多個(gè)復(fù)選框
46
//1.list的# 與{}之間必須有空格
47
//2.'Struts':'2004' 中'Struts'是key(向后臺(tái)發(fā)送的數(shù)據(jù));'2004'是value(顯示在前臺(tái)中的信息)
48
%>
49
<s:checkboxlist name="c" label="出版日期" labelposition="top"
50
list="# {'Java':'2005','JSP':'2006','Struts':'2007','Hibernate':'2008'}"
51
listKey="key" listValue="value"></s:checkboxlist>
52
<s:submit></s:submit>
53
</s:form>
54
<hr>
55
<h3>
56
使用s:combobox生成下拉輸入框
57
</h3>
58
<s:form>
59
<%
60
//使用簡(jiǎn)單集合生成下拉輸入框
61
%>
62
<s:combobox label="請(qǐng)選擇圖書" labelposition="top" theme="css_xhtml"
63
list="{'Java','JSP','Struts','Hibernate'}" size="20" maxlength="20"
64
name="book"></s:combobox>
65
66
<%
67
//使用后臺(tái)集合獲取相關(guān)下拉輸入框
68
//1.struts中默認(rèn)提供四套主題模板,theme="css_xhtml"是其中之一
69
//2.list="#bs.books" 是產(chǎn)生的對(duì)象bs會(huì)到BookDAO.java調(diào)用getBooks()方法[注意:這里books首字母要小寫,BookDAO.java的getBooks()方法要大寫]
70
//3.listKey="name" 是復(fù)選框的實(shí)際取值,向后臺(tái)發(fā)送的數(shù)據(jù),listValue="author"是復(fù)選框的標(biāo)簽,顯示在前臺(tái)中的信息(label)
71
%>
72
<s:combobox label="請(qǐng)選擇" labelposition="top" theme="css_xhtml"
73
list="#bs.books" listKey="name" listValue="author" size="20"
74
maxlength="20" name="book1"></s:combobox>
75
76
</s:form>
77
<hr>
78
<h3>
79
用s:datetimepicker生成時(shí)間選擇器
80
</h3>
81
<s:form theme="css_xhtml">
82
<%
83
//1.name:時(shí)間組件的id名
84
//2.toggleType:邦定時(shí)間
85
//3.value:初始化時(shí)間
86
//4.displayFormat:時(shí)間顯示的樣式
87
//5.weekStartsOn:以周的形式顯示時(shí)間日期
88
//6.type:修改時(shí)間選擇器的選擇內(nèi)容
89
%>
90
91
(1)日期選擇部件,指定toggleType屬性,且指定value="today"(即默認(rèn)選中的是當(dāng)天的日期.)<br>
92
93
<s:datetimepicker name="order.date" label="購(gòu)買日期" toggleType="explode"
94
value="today" />
95
96
(2)日期選擇部件,指定了format屬性displayFormat="dddd年MM月dd日"<br>
97
98
<s:datetimepicker name="order.date" label="購(gòu)買日期"
99
displayFormat="dddd年MM月dd日" />
100
101
(3)日期選擇部件,指定了weekStartsOn屬性,默認(rèn)以周的形式開始<br>
102
103
<s:datetimepicker name="order.date" label="購(gòu)買日期"
104
displayFormat="dddd年MM月dd日" weekStartsOn="2" />
105
(4)時(shí)間選擇部分
106
<s:datetimepicker label="出發(fā)日期" name="start" type="time" value="10:20" />
107
</s:form>
108
<hr>
109
<h3>
110
使用s:doubleselect生成級(jí)聯(lián)下拉列表框
111
</h3>
112
<s:form action="y">
113
<%
114
//1.list:用于進(jìn)行分類的列表
115
//2.doubleList:用于控件聯(lián)的動(dòng)顯示列表,使用doubleName來記載聯(lián)動(dòng)列表的取值
116
//3.doubleList的取值使用:表達(dá)式進(jìn)行判斷,top表示分類列表的取值(實(shí)際操作中使用動(dòng)態(tài)列表的賦值)
117
%>
118
119
<s:doubleselect label="請(qǐng)選擇圖書" list="{'a','b'}" doubleName="book"
120
doubleList="top=='a'?{'Java','Struts','Hibernate','Jsp'}:{'其他選項(xiàng)1','其他選項(xiàng)2'}" />
121
</s:form>
122
<hr>
123
<h3>使用s:radio生成多個(gè)單選框</h3>
124
<s:form action="z">
125
(1)使用簡(jiǎn)單的集合(數(shù)組)生成多個(gè)單選框<br>
126
<s:radio name="e" label="請(qǐng)選擇圖書" labelposition="top" list="{'Java','JSP','Struts','Hibernate'}"/>
127
128
(2)使用Map集合生成多個(gè)單選框<br>
129
<s:radio name="f" label="出版時(shí)間" labelposition="top" list="#{'Java':'2005','Jsp':'2006','Struts':'2007','Hibernate':'2008'}" listKey="key" listValue="value"/>
130
<s:bean name="lee.BookService" id="bs"/>
131
132
(3)使用集合里放多個(gè)JavaBean實(shí)例來生成多個(gè)單選框<br>
133
<s:radio name="g" label="選擇圖書" labelposition="top" list="#bs.books" listKey="author" listValue="name"/>
134
</s:form>
135
<hr>
136
<h3>提交表單中s:token可以添加隱藏的表單域,以防止頁面的重復(fù)提交操作.</h3>
137
<s:form>
138
<s:token/>
139
</s:form>
140
</body>
141
</html>

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

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

相關(guān):
1
web.xml
2
<?xml version="1.0" encoding="UTF-8"?>
3
<web-app id="WebApp_ID" version="2.4"
4
xmlns="http://java.sun.com/xml/ns/j2ee"
5
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
7
<display-name>Struts2Hello</display-name>
8
<filter>
9
<filter-name>struts2</filter-name>
10
<filter-class>
11
org.apache.struts2.dispatcher.FilterDispatcher
12
</filter-class><!-- 以過慮器的形式出現(xiàn) -->
13
</filter>
14
<filter-mapping>
15
<filter-name>struts2</filter-name>
16
<url-pattern>/*</url-pattern><!-- 過慮所有內(nèi)容 -->
17
</filter-mapping>
18
<welcome-file-list>
19
<welcome-file>index.html</welcome-file>
20
<welcome-file>index.htm</welcome-file>
21
<welcome-file>index.jsp</welcome-file>
22
<welcome-file>default.html</welcome-file>
23
<welcome-file>default.htm</welcome-file>
24
<welcome-file>default.jsp</welcome-file>
25
</welcome-file-list>
26
</web-app>
27

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

1
struts.xml
2
<!DOCTYPE struts PUBLIC
3
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4
"http://struts.apache.org/dtds/struts-2.0.dtd">
5
<struts>
6
<include file="struts-default.xml" /><!-- 使用缺省的struts的配置文件 -->
7
8
<package name="TagDemo" extends="struts-default"><!-- 包空間 admin 繼承 struts-default -->
9
<action name="*"><!-- 試用通配符action的描述 -->
10
<result>/{1}.jsp</result><!-- {1}與*通配符的內(nèi)容相對(duì)應(yīng),即action名={1}.jsp -->
11
</action>
12
</package>
13
</struts>
14

2

3

4

5

6

7

8

9

10

11

12

13

14

1
Book.java
2
package com.pojo;
3
4
import java.io.Serializable;
5
6
/**
7
* @author Administrator
8
* @version 2008 08 17
9
*/
10
public class Book implements Serializable{
11
/**
12
*
13
*/
14
private static final long serialVersionUID = -4565886033215350973L;
15
private String name;
16
private String author;
17
18
public Book() {
19
20
}
21
22
public Book(final String name, final String author) {
23
super();
24
this.name = name;
25
this.author = author;
26
}
27
28
public String getName() {
29
return name;
30
}
31
32
public void setName(final String name) {
33
this.name = name;
34
}
35
36
public String getAuthor() {
37
return author;
38
}
39
40
public void setAuthor(final String author) {
41
this.author = author;
42
}
43
44
}
45

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

1
BookDAO.java
2
package com.DAO;
3
4
import com.pojo.Book;
5
6
/**
7
* @author ∪∩BUG E-mail: tidelgl@163.com
8
* @version Aug 17, 2008 11:29:49 PM 類說明
9
*/
10
11
public class BookDAO {
12
//業(yè)務(wù)方法
13
public Book[] getBooks() {
14
return new Book[] { new Book("a", "Struts"), new Book("b", "JSP"),
15
new Book("c", "J2EE"), new Book("d", "Hibernate") };
16
}
17
}
18

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
