具体为一下几个步骤:
1、引入Struts 2工程所需运行库文件。
2、创建并配置web.xml文件
3、创建一个Action类
4、创建并配置struts.xml文件。
目前我用到的Struts版本为2.1.8.1 GA。
把以下几个运行库文件复制到工程的WEB-INF下的lib文件夹中。
配置WEB-INF下的web.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
version
=
"2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
<
filter
>
<
filter-name
>struts2</
filter-name
>
<
filter-class
>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>struts2</
filter-name
>
<
url-pattern
>/*</
url-pattern
>
</
filter-mapping
>
</
web-app
>
|
我们现在要实现的是一个用户登录验证,因此Action中提供用户名和密码两个属性。如果正确返回success否则返回error
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
|
package
wwfy.action.user;
public
class
UserAction {
private
String username;
private
String password;
public
String getUsername() {
return
username;
}
public
void
setUsername(String username) {
this
.username = username;
}
public
String getPassword() {
return
password;
}
public
void
setPassword(String password) {
this
.password = password;
}
public
String execute()
throws
Exception {
// 只有用户名为wwfy,密码为123456方可成功登陆
if
(username.equals(
"wwfy"
) && password.equals(
"123456"
)) {
return
"success"
;
}
else
{
return
"error"
;
}
}
}
|
Action有一下特点:
Struts2框架的核心配置文件就是struts.xml,该文件主要负责Struts2应用中各Action的具体实现逻辑。struts.xml会根据不同请求找到具体的Action,然后根据Action返回的逻辑视图名找到具体的物理视图。
在WEB-INF/classes文件夹下创建struts.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
<
struts
>
<
package
name
=
"wwfy"
extends
=
"struts-default"
>
<
action
name
=
"loginUser"
class
=
"wwfy.action.user.UserAction"
>
<
result
name
=
"success"
>/jsp/error.jsp</
result
>
<
result
name
=
"error"
>/jsp/error.jsp</
result
>
</
action
>
</
package
>
</
struts
>
|
1、登陆页面login.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
html
>
<
head
>
<
title
>登陆页面</
title
>
</
head
>
<
body
>
<
center
>
<
s:form
action
=
"loginUser"
>
<
s:textfield
name
=
"username"
label
=
"用户名"
></
s:textfield
>
<
s:password
name
=
"password"
label
=
"密码"
></
s:password
>
<
s:submit
value
=
"确定"
/>
<
s:reset
value
=
"重置"
/>
</
s:form
>
</
center
>
</
body
>
</
html
>
|
2、登陆成功页面success.jsp
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
html
>
<
head
>
<
title
>成功页面</
title
>
</
head
>
<
body
>
<
p
align
=
"center"
>登陆成功!!!您的用户名是<
s:property
value
=
"username"
></
s:property
></
p
>
</
body
>
</
html
>
|
3、登陆失败页面error.jsp
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
html
>
<
head
>
<
title
>失败页面</
title
>
</
head
>
<
body
>
<
p
align
=
"center"
>登陆失败!!!</
p
>
</
body
>
</
html
>
|
页面中很多以s:开头的标签,这些是Struts2框架独有的标签库.
整个应用的目录结构是