struts2.0學習筆記(六)--Validation(數據效驗)
Posted on 2008-08-26 02:23 ∪∩BUG 閱讀(959) 評論(1) 編輯 收藏 所屬分類: Struts2學習筆記項目樹形圖
src/struts.xml
1
<?xml version="1.0" encoding="UTF-8"?>
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
<!-- 包空間 ConverterDemo 繼承 struts-default -->
9
<package name="ConverterDemo" extends="struts-default">
10
11
<!-- 映射名name="HelloWorld" 與 index.jsp 中的 action="HelloWorld" 對應,使用com.action.LoginAction來實現 -->
12
<action name="HelloWorld" class="com.action.LoginAction">
13
<result>/index.jsp</result>
14
<result name="input">/index.jsp</result>
15
</action>
16
17
<!--
18
1.映射名name="ProductConfirm" 與 submit.jsp 中的 action="ProductConfirm" 對應,使用com.action.ProductConfirm來實現
19
2.成功轉到show.jsp頁面
20
3.失敗轉入submit.jsp頁面
21
-->
22
<action name="ProductConfirm"
23
class="com.action.ProductConfirm">
24
<result>/show.jsp</result>
25
<result name="input">/submit.jsp</result>
26
</action>
27
28
<!--
29
1.映射名name="ValidationAction" 與 int.jsp 中的 action="ValidationAction" 對應,使用com.action.InputAction來實現
30
2.成功轉到output.jsp頁面
31
3.失敗轉入int.jsp頁面
32
-->
33
<action name="ValidationAction"
34
class="com.action.InputAction">
35
<result>/output.jsp</result>
36
<result name="input">/int.jsp</result>
37
</action>
38
</package>
39
</struts>
40
41

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

src/com.action.InputAction.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 25, 2008 5:05:10 PM
8
* @表單校驗Action
9
*/
10
public class InputAction extends ActionSupport {
11
12
private String string;
13
14
public String getString() {
15
return string;
16
}
17
18
public void setString(String string) {
19
this.string = string;
20
}
21
22
@Override
23
public String execute() throws Exception {
24
25
return SUCCESS;
26
}
27
28
// @Override
29
// public void validate() { 非action-validation.xml驗證方式用
30
// if (this.string.length() <= 0) {
31
// this.addFieldError("string", "empty");
32
// }
33
// }
34
35
}
36

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

src/com.action.InputAction-validation.xml
Struts 2.0校驗框架的規范:在相應的Action同級目錄下創建名為"Action名-validation.xml"的配置文件
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE validators PUBLIC
3
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
4
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
5
6
<!--
7
1.InputAction-validation.xml驗證對應InputAction.java里的屬性,表單信息先通過此驗證才過Action
8
2.validators:驗證框架,fied表明針對域的驗證
9
3.fied name="string":name對應的只能是此Action(InputAction.java)里的屬性.
10
4.fied-validator:字段效驗器.(這里有兩個,可設置多個) type="reqiuredString":屬性name="string"的驗證規則是type="reqiuredString"
11
5.message:錯誤提示信息.key="empty"表明從資源文件中讀取(直接可以I18n)
12
注意:dtd的版本是xwork-validator-1.0.2.dtd,此例子其他版本會出錯!
13
-->
14
<validators>
15
<field name="string">
16
<field-validator type="requiredstring">
17
<message key="empty"></message>
18
</field-validator>
19
<field-validator type="email">
20
<message>
21
The email address you entered is not valid.
22
</message>
23
</field-validator>
24
</field>
25
</validators>
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/int.jsp
1
<%@page contentType="text/html; charset=UTF-8"%>
2
<%@taglib prefix="s" uri="/struts-tags"%>
3
<html>
4
<head>
5
<title>int</title>
6
</head>
7
<body>
8
<!--
9
非action-validation.xml驗證方式用
10
<s:fielderror></s:fielderror>
11
-->
12
<s:form action="ValidationAction">
13
<%
14
//name="string"對應InputAction.java中的屬性string
15
%>
16
<s:textfield name="string" label="Reqiured String"></s:textfield>
17
<br>
18
<s:submit></s:submit>
19
</s:form>
20
</body>
21
</html>
22

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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

2

3

4

5

6

7

8

9

10

11

12

src/globalMessages_en_US.properties










src/globalMessages_zh_CN.properties









