To create a ModelDriven Action our Action class should implement the ModelDriven interface and should include the modelDriven interceptor. The modelDriven interceptor is already included in the default stack.
The next step is to implement the getModel() method in such a way that it returns the application domain object, in our example we return the User object.
When using the ModelDriven method we need to initialize the User object ourselves.
The framework will automatically transfers the form data into the User object.
01.
public
class
UserAction
extends
ActionSupport
implements
ModelDriven {
02.
03.
private
User user =
new
User();
04.
05.
public
UserAction() {
06.
}
07.
08.
public
Object getModel() {
09.
return
user;
10.
}
11.
12.
public
String execute() {
13.
return
SUCCESS;
14.
}
15.
16.
public
User getUser() {
17.
return
user;
18.
}
19.
20.
public
void
setUser(User user) {
21.
this
.user = user;
22.
}
23.
}
You can directly access the user attributes like name, age etc in Action use the following syntax.
1.
user.getXXX();
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 can be accessed directly. To refer the user's age, the value of the name attribute should be
1.
name =
"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
=
"name"
label
=
"User Name"
/>
12.
<
s:textfield
name
=
"age"
label
=
"Age"
/>
13.
<
s:radio
name
=
"sex"
label
=
"Sex"
list
=
"{'M','F'}"
/>
14.
<
s:checkboxlist
name
=
"hobby"
label
=
"Hobby"
15.
list
=
"{'Music','Art','Dance'}"
/>
16.
<
s:select
name
=
"country"
label
=
"Country"
17.
list
=
"{'Select','India','USA','France','Spain'}"
/>
18.
<
s:submit
/>
19.
</
s:form
>
20.
</
body
>
21.
</
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
=
"name"
/>
13.
14.
Age :<
s:property
value
=
"age"
/>
15.
16.
Hobbies :<
s:property
value
=
"hobby"
/>
17.
18.
Country :<
s:property
value
=
"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