Meyyappan has posted 101 posts at DZone. View Full User Profile

Struts HTML Tag Tutorial

06.14.2012
| 46774 views |
  • submit to reddit

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.

<%@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.

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Feedback Form</title>
</head>
<body>
<b>Feedback Form</b>
<hr>
<html:form action="/feedbackAction">
<table>
    <tr>
        <td>
            Name :
        </td>
        <td>
            <html:text name="feedbackForm" property="name" />
        </td>
    </tr>
    <tr>
        <td>
            Sex :
        </td>
        <td>
            <html:radio name="feedbackForm" property="sex" value="M" >
            M </html:radio>
            <html:radio name="feedbackForm" property="sex" value="F" >
            F </html:radio>
        </td>
    </tr>
    <tr>
        <td>
            Comments :
        </td>
        <td>
            <html:textarea cols="20" rows="5" name="feedbackForm"
            property="comments" />
        </td>
    </tr>
    <tr>
        <td>
            <html:submit />
        </td>
        <td>
            <html:reset />
        </td>
    </tr>
</table>
</html:form>
</body>
</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.

<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

The success.jsp page contains the following code.

<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Feedback Form</title>
</head>
<body>
<b>Feedback Successfully Submitted.</b><br>
You have submitted the following feedback.
<table>
    <tr>
        <td>
            Name :
        </td>
        <td>
            <bean:write name="feedbackForm" property="name" />
        </td>
    </tr>
    <tr>
        <td>
            Sex :
        </td>
        <td>
            <logic:equal name="feedbackForm" property="sex" value="M"> Male </logic:equal>
            <logic:equal name="feedbackForm" property="sex" value="F"> Female </logic:equal>
        </td>
    </tr>
    <tr>
        <td>
            Comments :
        </td>
        <td>
            <bean:write name="feedbackForm" property="comments" />
        </td>
    </tr>
</table>
</body>
</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.

You can download the source code of the Struts HTML Tags example by clicking on the Download link below.

Source :Download
Source + Lib :Download

 

Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Saravanan Sagadevan replied on Mon, 2012/08/06 - 9:13am

  1. If I add <s:textfield name="userId" key="label.userID" size="22"  /><s:textfield name="userNo" key="label.empNo" size="22"  />  will it be displayed in same row? The output will be two text boxes are displaying one by one . But I want to display both input fields should be displayed horizontal way.How can we do it by using strut2 tags?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.