采用Struts2.0.14做的一個(gè)登陸驗(yàn)證示例
步驟:
1.在Web工程中,加入Struts2的相關(guān)jar包,主要有如下五個(gè):
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.書寫相應(yīng)的jsp頁(yè)面login.jsp和result.jsp,頁(yè)面代碼如下:
login.jsp:
1
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2
pageEncoding="gbk"%>
3
<%@ taglib prefix="s" uri="/struts-tags" %>
4
<%--導(dǎo)入struts2的標(biāo)簽庫(kù),這個(gè)庫(kù)的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提供的標(biāo)簽,就能夠利用struts2的數(shù)據(jù)校驗(yàn)功能。 --%>
20
<s:form action="login"><%--這action的值與普通的form相比,只需訪問(wèn)的關(guān)鍵字,".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.書寫相關(guān)的Action類,LoginAction.java:
1
package com.web.action;
2
3
import com.opensymphony.xwork2.ActionSupport;
4
//要利用struts2的數(shù)據(jù)校驗(yàn)功能,需繼承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
//注意,數(shù)據(jù)的邏輯性校驗(yàn)應(yīng)該放在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
//進(jìn)行數(shù)據(jù)的有效性校驗(yàn)
32
@Override
33
public void validate() {
34
if(null==this.getUsername()||"".equals(this.getUsername())){
35
this.addFieldError("username", "username is required.");
36
//可以通過(guò)這個(gè)方法,會(huì)將錯(cuò)誤信息自動(dòng)進(jìn)行保存,和傳輸,這些錯(cuò)誤信息會(huì)傳送到input所指定的目標(biāo)頁(yè)
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.為這個(gè)Action類進(jìn)行配置,struts.xml(編碼時(shí)放在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的簡(jiǎn)單配置如下 -->
6
<struts>
7
<package name="struts2" extends="struts-default">
8
<action name="login" class="com.web.action.LoginAction">
9
<!-- 這個(gè)name是指訪問(wèn)服務(wù)器路徑的關(guān)鍵字(login.action)
10
class就是與訪問(wèn)路徑相對(duì)應(yīng)得處理類的全限定名。
11
-->
12
<result name="input">/login.jsp</result>
13
<!-- 對(duì)數(shù)據(jù)校驗(yàn)未能通過(guò)時(shí)跳轉(zhuǎn)路徑的配置,input是固定寫法 -->
14
<result name="success">/result.jsp</result>
15
<!-- execute方法返回"success"時(shí)跳轉(zhuǎn)路徑的配置,"success"可省略,這是默認(rèn)的。 -->
16
<result name="failer">/login.jsp</result>
17
<!-- execute方法返回"failer"時(shí)跳轉(zhuǎn)路徑的配置 -->
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

至此,即可進(jìn)行運(yùn)行測(cè)試。這個(gè)程序只有當(dāng)username,與password都為"jone"時(shí),才會(huì)跳轉(zhuǎn)到result.jsp.