Platinum Partner
struts

Struts HTML Tag Tutorial

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

 

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}