Struts标签下的用户注册
第一步:创建动态项目,加入相关类包
第二步:在web.xml文件中注册Struts2提供的StrutsPrepareAndFilter过滤器
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
|
<
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"
>
<
welcome-file-list
>
<
welcome-file
>index.html</
welcome-file
>
<
welcome-file
>index.htm</
welcome-file
>
<
welcome-file
>index.jsp</
welcome-file
>
<
welcome-file
>default.html</
welcome-file
>
<
welcome-file
>default.htm</
welcome-file
>
<
welcome-file
>default.jsp</
welcome-file
>
</
welcome-file-list
>
<!-- Struts2过滤器 -->
<
filter
>
<!-- 过滤器名称 -->
<
filter-name
>struts2</
filter-name
>
<!-- 过滤器类 -->
<!-- 注册Struts2提供的StrutsPrepareAndExecuteFilter过滤器 -->
<
filter-class
>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</
filter-class
>
</
filter
>
<!-- Struts2过滤器映射 -->
<
filter-mapping
>
<!-- 过滤器名称 -->
<
filter-name
>struts2</
filter-name
>
<!-- 过滤器映射 -->
<
url-pattern
>/*</
url-pattern
>
</
filter-mapping
>
</
web-app
>
|
第三步:创建index.jsp主页(通过struts2提供的表单标签编写用户注册表单)
1
2
3
4
5
6
7
8
9
10
11
|
<
h2
>用户注册</
h2
>
<
s:form
action
=
"userAction"
method
=
"post"
>
<
s:textfield
name
=
"name"
label
=
"用户名"
required
=
"true"
requiredposition
=
"left"
></
s:textfield
>
<
s:password
name
=
"password"
label
=
"密码"
required
=
"true"
requiredposition
=
"left"
></
s:password
>
<
s:radio
name
=
"sex"
list
=
"#{1 : '男', 0 : '女'}"
label
=
"性别"
required
=
"true"
requiredposition
=
"left"
></
s:radio
>
<
s:select
list
=
"{'请选择地区','广州','深圳','佛山','茂名'}"
name
=
"area"
label
=
"地区"
></
s:select
>
<
s:checkboxlist
list
=
"{'足球','羽毛球','乒乓球','篮球'}"
name
=
"hobby"
label
=
"爱好"
></
s:checkboxlist
>
<
s:textarea
name
=
"description"
cols
=
"30"
rows
=
"5"
label
=
"描述"
></
s:textarea
>
<
s:submit
value
=
"注册"
></
s:submit
>
<
s:reset
value
=
"重置"
></
s:reset
>
</
s:form
>
|
第四步:创建注册后的返回页面success.jsp
1
2
3
4
5
6
7
8
9
10
|
<
div
>
<
ul
>
<
li
>用户名:<
s:property
value
=
"name"
/></
li
>
<
li
>密码:<
s:property
value
=
"password"
/></
li
>
<
li
>性别:<
s:if
test
=
"sex==0"
>女</
s:if
><
s:else
>男</
s:else
></
li
>
<
li
>地区:<
s:property
value
=
"area"
/></
li
>
<
li
>爱好:<
s:property
value
=
"hobby"
/></
li
>
<
li
>描述:<
s:property
value
=
"description"
/></
li
>
</
ul
>
</
div
>
|
第五步:创建UserAction类,继承于ActionSupport类,作用是对用户注册请求及用户信息编辑请求处理
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
|
public
class
UserAction
extends
ActionSupport{
private
static
final
long
serialVersionUID = 1L;
// 用户名
private
String name;
// 密码
private
String password;
// 描述
private
String description;
// 性别
private
int
sex =
0
;
// 省份
private
String area;
// 爱好
private
String[] hobby;
// 用户注册
public
String execute()
throws
Exception {
return
SUCCESS;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getPassword() {
return
password;
}
public
void
setPassword(String password) {
this
.password = password;
}
public
String getDescription() {
return
description;
}
public
void
setDescription(String description) {
this
.description = description;
}
public
int
getSex() {
return
sex;
}
public
void
setSex(
int
sex) {
this
.sex = sex;
}
public
String getArea() {
return
area;
}
public
void
setArea(String area) {
this
.area = area;
}
public
String[] getHobby() {
return
hobby;
}
public
void
setHobby(String[] hobby) {
this
.hobby = hobby;
}
}
|
第六步:创建配置文件Struts.xml,在该文件中配置UserAction对象(要放在src文件夹下)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<
struts
>
<!-- 声明变量(开发模式) -->
<
constant
name
=
"struts.devMode"
value
=
"true"
/>
<!-- 声明包 -->
<
package
name
=
"myPackge"
extends
=
"struts-default"
>
<!-- 创建TagAction的映射 -->
<
action
name
=
"userAction"
class
=
"com.lxy.UserAction"
>
<!-- 注册成功的返回页面 -->
<
result
>success.jsp</
result
>
</
action
>
</
package
>
</
struts
>
|
最后运行效果如下:
输入注册信息
注册成功
Struts2标签下使用验证框架对数据校验
第一步:创建动态项目,加入相关类包
第二步:在web.xml文件中注册Struts2提供的StrutsPrepareAndFilter过滤器
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
|
<
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"
>
<
welcome-file-list
>
<
welcome-file
>index.html</
welcome-file
>
<
welcome-file
>index.htm</
welcome-file
>
<
welcome-file
>index.jsp</
welcome-file
>
<
welcome-file
>default.html</
welcome-file
>
<
welcome-file
>default.htm</
welcome-file
>
<
welcome-file
>default.jsp</
welcome-file
>
</
welcome-file-list
>
<!-- Struts2过滤器 -->
<
filter
>
<!-- 过滤器名称 -->
<
filter-name
>struts2</
filter-name
>
<!-- 过滤器类 -->
<!-- 注册Struts2提供的StrutsPrepareAndExecuteFilter过滤器 -->
<
filter-class
>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</
filter-class
>
</
filter
>
<!-- Struts2过滤器映射 -->
<
filter-mapping
>
<!-- 过滤器名称 -->
<
filter-name
>struts2</
filter-name
>
<!-- 过滤器映射 -->
<
url-pattern
>/*</
url-pattern
>
</
filter-mapping
>
</
web-app
>
|
第三步:创建用户登录的Action对象UserAction,并配置到Struts.xml中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public
class
UserAction
extends
ActionSupport{
private
static
final
long
serialVersionUID = 1L;
private
String username;
// 用户名
private
String password;
// 密码
// 用户登录
@Override
public
String execute()
throws
Exception {
return
SUCCESS;
}
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;
}
}
|
Struts.xml
1
2
3
4
5
6
7
8
9
10
11
12
|
<
struts
>
<!-- 声明包 -->
<
package
name
=
"myPackge"
extends
=
"struts-default"
>
<!-- 配置UserAction -->
<
action
name
=
"userAction"
class
=
"com.lxy.UserAction"
>
<!-- 用户登录页面 -->
<
result
name
=
"input"
>/login.jsp</
result
>
<!-- 注册成功页面 -->
<
result
>/success.jsp</
result
>
</
action
>
</
package
>
</
struts
>
|
第四步:创建用户登录页面login.jsp(通过Struts表单标签创建)
1
2
3
4
5
|
<
s:form
action
=
"userAction"
method
=
"post"
>
<
s:textfield
name
=
"username"
label
=
"用户名"
required
=
"true"
requiredposition
=
"left"
></
s:textfield
>
<
s:password
name
=
"password"
label
=
"密码"
required
=
"true"
requiredposition
=
"left"
></
s:password
>
<
s:submit
key
=
"submit"
value
=
"登录"
></
s:submit
>
</
s:form
>
|
第五步:创建success.jsp
1
2
3
|
<
h2
>
<
s:property
value
=
"username"
/>,登录成功
</
h2
>
|
第六步:编写用户验证文件UserAction-validation.xml(必须放在UserAction所在包中)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<
validators
>
<!-- 验证用户名 -->
<
field
name
=
"username"
>
<
field-validator
type
=
"requiredstring"
>
<
message
>请输入用户名</
message
>
</
field-validator
>
</
field
>
<!-- 验证密码 -->
<
field
name
=
"password"
>
<
field-validator
type
=
"requiredstring"
>
<
message
>请输入密码</
message
>
</
field-validator
>
</
field
>
</
validators
>
|
最后运行效果如下:
登录
成功登录
本文转自lixiyu 51CTO博客,原文链接:http://blog.51cto.com/lixiyu/1355303,如需转载请自行联系原作者