In this example you will learn how to use Struts HTML Tags. In order to use the Struts HTML Tags you need to include the following taglib directive in the jsp page.
1.
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
All the Struts HTML tags should be nested within the body of the <html:form /> tag. We will create a simple feedback form to see how the different Struts HTML Tags can be used. The feedback.jsp contains the following code.
01.
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
02.
<
html
>
03.
<
head
>
04.
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
05.
<
title
>Feedback Form</
title
>
06.
</
head
>
07.
<
body
>
08.
<
b
>Feedback Form</
b
>
09.
<
hr
>
10.
<
html:form
action
=
"/feedbackAction"
>
11.
<
table
>
12.
<
tr
>
13.
<
td
>
14.
Name :
15.
</
td
>
16.
<
td
>
17.
<
html:text
name
=
"feedbackForm"
property
=
"name"
/>
18.
</
td
>
19.
</
tr
>
20.
<
tr
>
21.
<
td
>
22.
Sex :
23.
</
td
>
24.
<
td
>
25.
<
html:radio
name
=
"feedbackForm"
property
=
"sex"
value
=
"M"
>
26.
M </
html:radio
>
27.
<
html:radio
name
=
"feedbackForm"
property
=
"sex"
value
=
"F"
>
28.
F </
html:radio
>
29.
</
td
>
30.
</
tr
>
31.
<
tr
>
32.
<
td
>
33.
Comments :
34.
</
td
>
35.
<
td
>
36.
<
html:textarea
cols
=
"20"
rows
=
"5"
name
=
"feedbackForm"
37.
property
=
"comments"
/>
38.
</
td
>
39.
</
tr
>
40.
<
tr
>
41.
<
td
>
42.
<
html:submit
/>
43.
</
td
>
44.
<
td
>
45.
<
html:reset
/>
46.
</
td
>
47.
</
tr
>
48.
</
table
>
49.
</
html:form
>
50.
</
body
>
51.
</
html
>
The name attribute of the HTML tag contains the Form Bean name. The property attribute of the HTML Tag contains the corresponding Form property.
Once the feedback is submitted by the user, the user will be forwarded to the success page. Here the feedback submitted by the user will be displayed. We use Struts Bean Tag do to this. We use Struts Logic Equal Tag to display Male if the user has selected M for sex and Female if the user has selected F for sex. In order to use the Struts Bean and Logic Tag you need to include the following taglib directives in the jsp page.
1.
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
2.
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
The success.jsp page contains the following code.
01.
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
02.
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
03.
<
html
>
04.
<
head
>
05.
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
06.
<
title
>Feedback Form</
title
>
07.
</
head
>
08.
<
body
>
09.
<
b
>Feedback Successfully Submitted.</
b
>
10.
11.
You have submitted the following feedback.
12.
<
table
>
13.
<
tr
>
14.
<
td
>
15.
Name :
16.
</
td
>
17.
<
td
>
18.
<
bean:write
name
=
"feedbackForm"
property
=
"name"
/>
19.
</
td
>
20.
</
tr
>
21.
<
tr
>
22.
<
td
>
23.
Sex :
24.
</
td
>
25.
<
td
>
26.
<
logic:equal
name
=
"feedbackForm"
property
=
"sex"
value
=
"M"
> Male </
logic:equal
>
27.
<
logic:equal
name
=
"feedbackForm"
property
=
"sex"
value
=
"F"
> Female </
logic:equal
>
28.
</
td
>
29.
</
tr
>
30.
<
tr
>
31.
<
td
>
32.
Comments :
33.
</
td
>
34.
<
td
>
35.
<
bean:write
name
=
"feedbackForm"
property
=
"comments"
/>
36.
</
td
>
37.
</
tr
>
38.
</
table
>
39.
</
body
>
40.
</
html
>
On executing the example the following page will be dispalyed to the user.
On submitting the feedback the following page will be dispalyed to the user.
No comments:
Post a Comment