struts2.0學(xué)習(xí)筆記(三)--Action
Posted on 2008-08-22 22:05 ∪∩BUG 閱讀(1278) 評(píng)論(5) 編輯 收藏 所屬分類(lèi): Struts2學(xué)習(xí)筆記




















src/struts.xml
1
<?xml version="1.0" encoding="GBK"?>
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
<!-- 包空間 ActionDemo 繼承 struts-default -->
9
<package name="ActionDemo" extends="struts-default">
10
11
<!-- 映射名name="HelloAction" ,使用com.Action.HelloAction來(lái)實(shí)現(xiàn) -->
12
<action name="HelloAction" class="com.Action.HelloAction">
13
<result>/HelloAction.jsp</result><!-- 轉(zhuǎn)到的頁(yè)面為HelloAction.jsp-->
14
</action>
15
16
<!--
17
1. method="aliasAction"對(duì)應(yīng) HelloAction.java 中的aliasAction()方法
18
2.訪(fǎng)問(wèn)AliasHelloWord Action方法:在地址欄中輸入:
19
(1)http://localhost:8080/Action/aliasAction.action
20
(2)http://localhost:8080/Action/HelloAction!aliasAction.action
21
-->
22
<action name="AliasHelloWord" class="com.Action.HelloAction"
23
method="aliasAction">
24
<result>/HelloAction.jsp</result>
25
</action>
26
27
<!-- 映射名name="Login" 與 Login.jsp 中的 action="Login" 對(duì)應(yīng),使用com.Action.LoginAction來(lái)實(shí)現(xiàn) -->
28
<action name="Login" class="com.Action.LoginAction">
29
<result>/Hello.jsp</result>
30
</action>
31
32
<!-- 映射名name="Loginx" 與 Loginx.jsp 中的 action="Login" 對(duì)應(yīng),使用com.Action.LoginActionx來(lái)實(shí)現(xiàn) -->
33
<action name="Loginx" class="com.Action.LoginActionx">
34
<result>/Hello.jsp</result>
35
</action>
36
</package>
37
</struts>
38
39

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

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

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

WebRoot/Hello.jsp
1
<%@page contentType="text/html;charset=GBK"%>
2
<%@taglib prefix="s" uri="/struts-tags"%>
3
<html>
4
<head>
5
<title>HelloAction</title>
6
</head>
7
<body>
8
9
<h2>
10
<s:property value="msg" />
11
</h2>
12
${test}<br>
13
${nameA}<br>
14
${nameB}<br>
15
</body>
16
</html>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

WebRoot/HelloAction.jsp
1
<%@page contentType="text/html;charset=GBK"%>
2
<%@taglib prefix="s" uri="/struts-tags"%>
3
<html>
4
<head>
5
<title>HelloAction</title>
6
</head>
7
<body>
8
9
<h2>
10
<s:property value="message" />
11
</h2>
12
</body>
13
</html>

2

3

4

5

6

7

8

9

10

11

12

13

WebRoot/Login.jsp
1
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
2
<%
3
String path = request.getContextPath();
4
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5
%>
6
<%@taglib prefix="s" uri="/struts-tags"%>
7
8
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9
<html>
10
<head>
11
12
<title>Login page</title>
13
</head>
14
15
<body>
16
<s:form action="Login" method="POST">
17
<s:textfield name="name" label="User name"/>
18
<s:password name="pass" label="Password"/>
19
<s:submit value="Submit"></s:submit>
20
</s:form>
21
</body>
22
</html>
23

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

WebRoot/Loginx.jsp
1
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
2
<%
3
String path = request.getContextPath();
4
String basePath = request.getScheme() + "://"
5
+ request.getServerName() + ":" + request.getServerPort()
6
+ path + "/";
7
%>
8
<%@taglib prefix="s" uri="/struts-tags"%>
9
10
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
11
<html>
12
<head>
13
14
<title>Login page</title>
15
</head>
16
17
<body>
18
<!-- action="Loginx"中與struts.xml中映射名name="Loginx"相同, name="user.name"表示使用com.Action.LoginActionx類(lèi)的user對(duì)象的name屬性-->
19
<s:form action="Loginx" method="POST">
20
<s:textfield name="user.name" label="User name" />
21
<s:password name="user.pass" label="Password" />
22
<s:submit value="Submit"></s:submit>
23
</s:form>
24
</body>
25
</html>
26

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

src/com.Action/HelloAction.java
1
package com.Action;
2
3
import java.text.DateFormat;
4
import java.util.Date;
5
6
import com.opensymphony.xwork2.ActionSupport;
7
8
/**
9
* @author ∪∩BUG E-mail: tidelgl@163.com
10
* @version Aug 20, 2008 1:45:28 PM 類(lèi)說(shuō)明
11
*/
12
@SuppressWarnings("serial")
13
public class HelloAction extends ActionSupport {
14
15
private String message;
16
17
// getMessage()方法對(duì)應(yīng)的是頁(yè)面的顯示
18
public String getMessage()
19
{
20
return message;
21
}
22
23
//缺省的方法,將被默認(rèn)執(zhí)行
24
public String execute(){
25
26
message = "HelloAction, Now is" + DateFormat.getInstance().format(new Date());
27
28
//SUCCESS返回的是String類(lèi)型,
29
System.out.println(SUCCESS);
30
return SUCCESS;
31
}
32
33
//自定義的業(yè)務(wù)方法
34
public String aliasAction(){
35
message ="自定義的Action方法";
36
return SUCCESS;
37
}
38
}
39

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

src/com.Action/LoginAction.java
1
package com.Action;
2
3
import com.opensymphony.xwork2.ActionSupport;
4
5
/**
6
* @author ∪∩BUG E-mail: tidelgl@163.com
7
* @version Aug 21, 2008 11:31:00 PM 類(lèi)說(shuō)明
8
*/
9
public class LoginAction extends ActionSupport {
10
@Override
11
public String execute() throws Exception {
12
if("admin".equals(this.name)&"123".equals(this.pass)){
13
this.msg="歡迎" + this.name;
14
15
}
16
else {
17
this.msg="非法輸入!";
18
}
19
return SUCCESS;
20
}
21
22
private String name;
23
private String pass;
24
private String msg;
25
26
// 默認(rèn)構(gòu)造方法是必須的.否則無(wú)法生成action實(shí)例
27
public LoginAction() {
28
29
}
30
31
public LoginAction(String name, String pass, String msg) {
32
super();
33
this.name = name;
34
this.pass = pass;
35
this.msg = msg;
36
}
37
38
public String getName() {
39
return name;
40
}
41
42
public void setName(String name) {
43
this.name = name;
44
}
45
46
public String getPass() {
47
return pass;
48
}
49
50
public void setPass(String pass) {
51
this.pass = pass;
52
}
53
54
public String getMsg() {
55
return msg;
56
}
57
58
public void setMsg(String msg) {
59
this.msg = msg;
60
}
61
62
}
63

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

src/com.Action/LoginActionx.java
1
package com.Action;
2
3
import java.util.Map;
4
5
import javax.servlet.http.HttpServletRequest;
6
import javax.servlet.http.HttpServletResponse;
7
8
import org.apache.struts2.interceptor.ServletRequestAware;
9
import org.apache.struts2.interceptor.ServletResponseAware;
10
import org.apache.struts2.interceptor.SessionAware;
11
12
import com.opensymphony.xwork2.ActionContext;
13
import com.opensymphony.xwork2.ActionSupport;
14
import com.pojo.User;
15
16
/**
17
* @author ∪∩BUG E-mail: tidelgl@163.com
18
* @version Aug 21, 2008 11:31:00 PM 類(lèi)說(shuō)明
19
*/
20
21
//IoC方法獲取Servlet API(implements是一個(gè)類(lèi)實(shí)現(xiàn)一個(gè)接口用的關(guān)鍵字, 他是用來(lái)實(shí)現(xiàn)接口中定義的抽象方法)
22
public class LoginActionx extends ActionSupport implements ServletRequestAware,ServletResponseAware,SessionAware{
23
private String msg;
24
private User user;
25
private HttpServletRequest request;
26
private HttpServletResponse response;
27
private Map session;
28
29
@Override
30
public String execute() throws Exception {
31
if("admin".equals(user.getName())&"123".equals(user.getPass())){
32
this.msg="歡迎" + user.getName()+"對(duì)象化的提交";
33
34
}
35
else {
36
this.msg="非法輸入!";
37
}
38
//ServletActionContext.getRequest().setAttribute("test","Just Test!" );//非IoC方法獲取Servlet API
39
40
//獲取session
41
Map map = ActionContext.getContext().getSession();
42
map.put("nameA", "name1");
43
session.put("nameB", "name2");
44
45
46
return SUCCESS;
47
}
48
49
// 默認(rèn)構(gòu)造方法是必須的.否則無(wú)法生成action實(shí)例
50
public LoginActionx() {
51
52
}
53
54
public String getMsg() {
55
return msg;
56
}
57
58
public void setMsg(String msg) {
59
this.msg = msg;
60
}
61
62
public User getUser() {
63
return user;
64
}
65
66
public void setUser(User user) {
67
this.user = user;
68
}
69
70
//IoC方法獲取Servlet API
71
//實(shí)現(xiàn)接口中定義的抽象方法setServletRequest(HttpServletRequest arg0)
72
public void setServletRequest(HttpServletRequest request) {
73
this.request=request;
74
request.setAttribute("test","Just Test!" );
75
76
}
77
78
//實(shí)現(xiàn)接口中定義的抽象方法setServletResponse(HttpServletResponse arg0)
79
public void setServletResponse(HttpServletResponse response) {
80
this.response=response;
81
82
}
83
84
//實(shí)現(xiàn)接口中定義的抽象方法setSession(Map arg0)
85
public void setSession(Map e) {
86
this.session=e;
87
88
}
89
90
91
}
92

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

src/com.pojo/User.java
1
package com.pojo;
2
3
import java.io.Serializable;
4
5
/**
6
* @author ∪∩BUG E-mail: tidelgl@163.com
7
* @version Aug 22, 2008 11:01:56 AM 類(lèi)說(shuō)明
8
*/
9
public class User implements Serializable {
10
private String name;
11
private String pass;
12
13
public User(String name, String pass) {
14
super();
15
this.name = name;
16
this.pass = pass;
17
}
18
19
public User() {
20
21
}
22
23
public String getName() {
24
return name;
25
}
26
27
public void setName(String name) {
28
this.name = name;
29
}
30
31
public String getPass() {
32
return pass;
33
}
34
35
public void setPass(String pass) {
36
this.pass = pass;
37
}
38
}
39

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
