采用Struts2.0.14做的一個登陸驗證示例
步驟:
1.在Web工程中,加入Struts2的相關jar包,主要有如下五個:
xwork-2.0.7.jar struts2-core-2.0.14.jar ognl-2.6.11.jar freemarker-2.3.8.jar commons-logging-1.0.4.jar
2.書寫相應的jsp頁面login.jsp和result.jsp,頁面代碼如下:
login.jsp:
1
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2
pageEncoding="gbk"%>
3
<%@ taglib prefix="s" uri="/struts-tags" %>
4
<%--導入struts2的標簽庫,這個庫的uri可以在struts-tags.tld中找到--%>
5
6
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
7
<html>
8
<head>
9
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
10
<title>Insert title here</title>
11
</head>
12
<body>
13
<!--<form action="/struts2/login.action" method="post">
14
username:<input type="text" name="username"/><br/>
15
password:<input type="password" name="password"/><br/>
16
<input type="submit" value="submit"/>
17
</form>
18
-->
19
<%--采用struts2提供的標簽,就能夠利用struts2的數據校驗功能。 --%>
20
<s:form action="login"><%--這action的值與普通的form相比,只需訪問的關鍵字,".action"也省了 --%>
21
<s:textfield name="username" label="username"></s:textfield>
22
<%--在struts2中用textfield表示普通文本框 --%>
23
<s:password name="password" label="password"></s:password>
24
<s:submit value="submit"></s:submit>
25
</s:form>
26
</body>
27
</html>
result.jsp:
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
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2
pageEncoding="ISO-8859-1"%>
3
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4
<html>
5
<head>
6
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7
<title>Insert title here</title>
8
</head>
9
<body>
10
username:${requestScope.username}<br>
11
password:${requestScope.password}
12
</body>
13
</html>

2

3

4

5

6

7

8

9

10

11

12

13

3.配置web.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
3
<filter>
4
<filter-name>struts2</filter-name>
5
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
6
</filter>
7
<filter-mapping>
8
<filter-name>struts2</filter-name>
9
<url-pattern>/*</url-pattern>
10
</filter-mapping>
11
</web-app>

2

3

4

5

6

7

8

9

10

11

4.書寫相關的Action類,LoginAction.java:
1
package com.web.action;
2
3
import com.opensymphony.xwork2.ActionSupport;
4
//要利用struts2的數據校驗功能,需繼承ActionSupport類,覆蓋其中的validate方法。
5
public class LoginAction extends ActionSupport{
6
private String username;
7
private String password;
8
public String getUsername() {
9
return username;
10
}
11
public void setUsername(String username) {
12
this.username = username;
13
}
14
public String getPassword() {
15
return password;
16
}
17
public void setPassword(String password) {
18
this.password = password;
19
}
20
public String execute()throws Exception{
21
//注意,數據的邏輯性校驗應該放在execute方法里
22
if(!"jone".equals(this.getUsername())){
23
this.addFieldError("username", "username is error.");
24
}
25
if(!"jone".equals(this.getPassword())){
26
this.addFieldError("password", "password is error");
27
return"failer";
28
}
29
return "success";
30
}
31
//進行數據的有效性校驗
32
@Override
33
public void validate() {
34
if(null==this.getUsername()||"".equals(this.getUsername())){
35
this.addFieldError("username", "username is required.");
36
//可以通過這個方法,會將錯誤信息自動進行保存,和傳輸,這些錯誤信息會傳送到input所指定的目標頁
37
}
38
if(null==this.getPassword()||"".equals(this.getPassword())){
39
this.addFieldError("password", "password is required");
40
}
41
}
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

5.為這個Action類進行配置,struts.xml(編碼時放在src目錄下):
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.xml的簡單配置如下 -->
6
<struts>
7
<package name="struts2" extends="struts-default">
8
<action name="login" class="com.web.action.LoginAction">
9
<!-- 這個name是指訪問服務器路徑的關鍵字(login.action)
10
class就是與訪問路徑相對應得處理類的全限定名。
11
-->
12
<result name="input">/login.jsp</result>
13
<!-- 對數據校驗未能通過時跳轉路徑的配置,input是固定寫法 -->
14
<result name="success">/result.jsp</result>
15
<!-- execute方法返回"success"時跳轉路徑的配置,"success"可省略,這是默認的。 -->
16
<result name="failer">/login.jsp</result>
17
<!-- execute方法返回"failer"時跳轉路徑的配置 -->
18
</action>
19
</package>
20
</struts>
21

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

至此,即可進行運行測試。這個程序只有當username,與password都為"jone"時,才會跳轉到result.jsp.