You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

The form tags are designed to generate standard markup around control tags. What markup the tags generate is determined by a "theme". A theme is a set of templates and stylesheets that work together to generate the Struts Tags.

The simple theme does not inject any default markup, and lets us add whatever special markup we want around the tags. To use the simple theme throughout an application, we can add this line to the struts.properties file, and place the file under WEB-INF/classes, next to the struts.xml.

struts.ui.theme=simple

The theme can also be specified as part of the form tag, if we just want to handcode a particular form.

The general idea is that we can create a form just by specifying the fields, and let the theme generate the HTML markup for us. (Like using an advanced stylesheet.) The default theme has handy features such as automatically displaying validation errors above each field, with any extra work on our part.

For example, a form like this

<s:form method="post">
    <s:textfield label="Name" name="name"/>
    <s:textfield label="Age" name="age"/>
    <s:submit/>
</s:form>

using the default XHTML team, renders like this

Generated HTML fragment
<form id="quizBasic" name="quizBasic" onsubmit="return true;"
    action="/struts2-showcase/validation/quizBasic.action" method="post">
    <table class="wwFormTable">
        <tr>
            <td class="tdLabel">
                <label for="quizBasic_name" class="label">Name:</label>
            </td>
            <td>
                <input type="text" name="name" value="" id="quizBasic_name" />
            </td>
        </tr>
    
        <tr>
            <td class="tdLabel">
                <label for="quizBasic_age" class="label">Age:</label>
            </td>
            <td>
                <input type="text" name="age" value="0" id="quizBasic_age" />
            </td>
        </tr>
    
        <tr>
            <td colspan="2">
               <div align="right">
                   <input type="submit" id="quizBasic_0" value="Submit" />
               </div>
            </td>
        </tr>
    </table>
</form>

Note that the Struts 2 tags include new attributes, like "label". The XHTML (and AJAX) themes will automatically inject a label tag into the form.

Of course, as mentioned, we can always use the simple theme and inject all themarkup by hand, just like Struts 1.

The themes and individual tags can also be extended, if need be, to meet any specific needs of an application.

For more, see Themes and Templates.

  • No labels