demo名称:SampleProject
请各位在群共享中下载。

/WEB-INF/src/web.xml有一个<welcome-file-list>标签,用于指出入口页面,这里是index.html。
然后我们进入WEB-INF看看index.html里的内容。

index.html
1
2
3
4
5
6
7
8
9
10
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=Login">
</head>
<body>
<p>Loading ...</p>
</body>
</html>

可以看到第4行有一个到Login的action的重定向。
关于这个重定向的说明,可以参考这里
通过查找struts.xml文件,可以找到被include的SampleProject.xml,
里面可以找到重定向的目标,也就是action:Login

SampleProject.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="SampleProject" namespace="/" extends="default">
<action name="Login_*" method="{1}" class="SampleProject.Login">
<result name="input">pages/Login.jsp</result>
<result name="Student" type="redirectAction">
student/StuInfo.action?id=${username}</result>
<result name="Teacher" type="redirectAction">
teacher/TeacherInfo.action?id=${username}</result>
<result name="Admin" type="redirectAction">
admin/AdminInfo.action?id=${username}</result>
</action>
<action name="*" class="SampleProject.ExampleSupport">
<result>/pages/error.jsp</result>
</action>
<!-- Add actions here -->
</package>
</struts>

在index.html中,我们并没有给Login传递任何参数,所以这里会调用对应class,
也就是SampleProject.Login的默认方法execute()

Login.java
1
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
package SampleProject;
public class Login extends ExampleSupport {
public String execute() throws Exception {
if (isInvalid(getUsername())) return INPUT;
if (isInvalid(getPassword())) return INPUT;
/**TO DO*/
String type = "";
if(getIdentity().equalsIgnoreCase("0")) type = "Student";
else if(getIdentity().equalsIgnoreCase("1")) type = "Teacher";
else if(getIdentity().equalsIgnoreCase("2")) type = "Admin";
if(getUsername().equalsIgnoreCase("admin"))
{
getSession().put("password", getPassword());
return type;
}
else addActionError("Login Error!用户名是admin啊");
return INPUT;
}
private boolean isInvalid(String value) {
return (value == null || value.length() == 0);
}
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
System.out.println(username);
}
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String identity;
public String getIdentity() {
return identity;
}
public void setIdentity(String identity) {
this.identity = identity;
}
}

可以看到验证了姓名之后就会return INPUT,
而在SampleProject.xml中,我们定义了收到input返回值之后会重定向到pages/Login.jsp

Login.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>研究生学籍管理系统</h1>
<s:form action="Login" validate="true">
<s:actionerror />
<s:textfield key="username"/>
<s:password key="password" />
<s:radio key="identity" list="%{#{'0':'Student','1':'Teacher','2':'Admin'}}" value="0"/>
<s:submit value="login"/>
</s:form>
</body>
</html>

在这个页面的第9行,我们可以看到有action="Login",这就表示该页面的参数全部由class:Login接收,该类会返回用户类型,由SampleProject.xml(之前用的默认文件名,应该改名叫Login.xml)接收,返回不同结果(由<result>标签注明)。

至此就实现了所有的登陆功能。