In Struts 2 transferring the data to the domain object is automatically done by the params interceptor.
You just need to create a domain object as a JavaBeans property and the corresponding getter and setter methods.
The framework will automatically initializes the domain object and transfers the form data. The UserAction class contains the following code.
01.
public
class
UserAction
extends
ActionSupport{
02.
03.
private
User user;
04.
05.
public
UserAction() {
06.
}
07.
08.
public
String execute() {
09.
return
SUCCESS;
10.
}
11.
12.
public
User getUser() {
13.
return
user;
14.
}
15.
16.
public
void
setUser(User user) {
17.
this
.user = user;
18.
}
19.
}
To refer the user attributes like name, age etc. we need to first get the user object and then access its properties. For example to access the user's age in the Action you need to use the following syntax.
1.
getUser().getAge();
The User class contains the following attributes and the corresponding getter and setter methods.
01.
public
class
User {
02.
03.
private
String name;
04.
private
int
age;
05.
private
String sex;
06.
private
String[] hobby;
07.
private
String country;
08.
09.
10.
public
String getName() {
11.
return
name;
12.
}
13.
14.
public
void
setName(String name) {
15.
this
.name = name;
16.
}
17.
18.
public
int
getAge() {
19.
return
age;
20.
}
21.
22.
public
void
setAge(
int
age) {
23.
this
.age = age;
24.
}
25.
26.
public
String getSex() {
27.
return
sex;
28.
}
29.
30.
public
void
setSex(String sex) {
31.
this
.sex = sex;
32.
}
33.
34.
public
String[] getHobby() {
35.
return
hobby;
36.
}
37.
38.
public
void
setHobby(String[] hobby) {
39.
this
.hobby = hobby;
40.
}
41.
42.
public
String getCountry() {
43.
return
country;
44.
}
45.
46.
public
void
setCountry(String country) {
47.
this
.country = country;
48.
}
49.
}
In the jsp page the user attributes cannot be directly referenced. Since the attributes we refer in the jsp page belongs to the User object we need to go one level deeper to reference the attributes. To refer the user's age, the value of the name attribute should be
1.
name=
"user.age"
The index.jsp page contains the following code.
01.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
03.
<%@taglib uri="/struts-tags" prefix="s" %>
04.
<
html
>
05.
<
head
>
06.
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
07.
<
title
>User Details</
title
>
08.
</
head
>
09.
<
body
>
10.
<
s:form
action
=
"UserAction"
>
11.
<
s:textfield
name
=
"user.name"
label
=
"User Name"
/>
12.
<
s:textfield
name
=
"user.age"
label
=
"Age"
/>
13.
<
s:radio
name
=
"user.sex"
label
=
"Sex"
list
=
"{'M','F'}"
/>
14.
<
s:checkboxlist
name
=
"user.hobby"
label
=
"Hobby"
list
=
"{'Music','Art','Dance'}"
/>
15.
<
s:select
name
=
"user.country"
label
=
"Country"
list
=
"{'Select','India','USA','France', 'Spain'}"
/>
16.
<
s:submit
/>
17.
</
s:form
>
18.
</
body
>
19.
</
html
>
The result.jsp page contains the follwing code.
01.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
03.
<%@taglib uri="/struts-tags" prefix="s" %>
04.
<
html
>
05.
<
head
>
06.
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
07.
<
title
>User Details</
title
>
08.
</
head
>
09.
<
body
>
10.
<
h2
>User Details</
h2
>
11.
<
hr
>
12.
User Name :<
s:property
value
=
"user.name"
/>
13.
14.
Age :<
s:property
value
=
"user.age"
/>
15.
16.
Hobbies :<
s:property
value
=
"user.hobby"
/>
17.
18.
Country :<
s:property
value
=
"user.country"
/>
19.
20.
</
body
>
21.
</
html
>
On executing the example the following page will be displayed to the user.
On entering the user details and clicking the Submit button the following page will be dispalyed.
No comments:
Post a Comment