Apache Struts 2 Documentation > Home > Guides > Tag Developers Guide > Struts Tags > UI Tags > Themes and Templates > xhtml theme |

Apache Struts 2 Documentation
xhtml theme
Edit Page Browse Space Add Page Add News
Added by Patrick Lightbody, last edited by Ted Husted on Sep 03, 2006 (view change)

The xhtml provides all the basics that the simple theme provides and adds several features.

- Standard two-column table layout for the HTML Struts Tags (form, textfield, select, and so forth)
- Labels for each of the HTML Struts Tags on the left hand side (or top, depending on the `labelposition` attribute)
- Validation and error reporting
- Pure JavaScript Client Side Validation using 100% JavaScript on the browser

## Wrapping the Simple Theme

The xhtml theme uses the "wrapping" technique described by Extending Themes. Let's look at how the HTML tags are wrapped by a standard header and footer. For example, in the `text.ftl` template, the `controlheader.ftl` and `controlfooter.ftl` templates are wrapped around the simple template.

    <#include "/${parameters.templateDir}/${parameters.theme}/controlheader.ftl" />
    <#include "/${parameters.templateDir}/simple/text.ftl" />
    <#include "/${parameters.templateDir}/xhtml/controlfooter.ftl" />

The `controlheader.ftl` is referenced using ${parameters.theme} so that the code can be reused by the ajax theme.

## XHTML Theme Header

Now let's look at the `controlheader.ftl` and `controlheader-core.ftl`. Again, these are split up for easy re-use with the ajax theme) contents:

    <#include "/${parameters.templateDir}/xhtml/controlheader-core.ftl" />
        <td
    <#if parameters.align?exists>
        align="${parameters.align?html}"<#t/>
    </#if>
    ><#t/>

    <#--
    	Only show message if errors are available.
    	This will be done if ActionSupport is used.
    -->
    <#assign hasFieldErrors = parameters.name?exists && fieldErrors?exists && fieldErrors[parameters.name]?exists/>
    <#if hasFieldErrors>
    <#list fieldErrors[parameters.name] as error>
    <tr errorFor="${parameters.id}">
    <#if parameters.labelposition?default("") == 'top'>
        <td align="left" valign="top" colspan="2"><#rt/>
    <#else>
        <td align="center" valign="top" colspan="2"><#rt/>
    </#if>
            <span class="errorMessage">${error?html}</span><#t/>
        </td><#lt/>
    </tr>
    </#list>
    </#if>
    <#--
    	if the label position is top,
    	then give the label it's own row in the table
    -->
    <tr>
    <#if parameters.labelposition?default("") == 'top'>
        <td align="left" valign="top" colspan="2"><#rt/>
    <#else>
        <td class="tdLabel"><#rt/>
    </#if>
    <#if parameters.label?exists>
        <label <#t/>
    <#if parameters.id?exists>
            for="${parameters.id?html}" <#t/>
    </#if>
    <#if hasFieldErrors>
            class="errorLabel"<#t/>
    <#else>
            class="label"<#t/>
    </#if>
        ><#t/>
    <#if parameters.required?default(false) && parameters.requiredposition?default("right") != 'right'>
            <span class="required">*</span><#t/>
    </#if>
    ${parameters.label?html}<#t/>
    <#if parameters.required?default(false) && parameters.requiredposition?default("right") == 'right'>
     <span class="required">*</span><#t/>
    </#if>
    :<#t/>
    <#include "/${parameters.templateDir}/xhtml/tooltip.ftl" />
    </label><#t/>
    </#if>
        </td><#lt/>
    <#-- add the extra row -->
    <#if parameters.labelposition?default("") == 'top'>
    </tr>
    <tr>
    </#if>

The header used by the HTML tags in the xhtml theme is complicated. However, a close look reveals that the logic produces two behaviors: either a two-column format or a two-row format. Generally the two-column approach is what we want, so that is the default option. To use the two-row approach, change the `labelposition` parameter to `top`.

| Moving fieldErrors The `fieldErrors`, usually caused by Validation, are printed out as a row above the HTML form element. Some people prefer that the errors display elsewhere, such as in a third column. If you wish to place these elsehwere, overriding the headers is easy, allowing you to continue to use the other features provided by this theme. See Template Loading for more information on how to do this.

## XHTML Theme Footer

The primary objective of `controlfooter.ftl` is to close the table. But, before the table closes, the template checks for an `after` parameter.

    ${parameters.after?if_exists}<#t/>
        </td><#lt/>
    </tr>

While "after" isn't an attribute supported by any of the Struts Tags, if you are using FreeMarker Tags, Velocity Tags, or the param tag in any template language, you can add an `after` parameter to place any content you like after the simple theme template renders. The `after` attribute makes it easier to fine-tune HTML forms for your specific environment.

## Special Interest

Two xhtml templates of special interest are `head` and `form`.

### head template

The xhtml head template extends the simple head template and provides an additional CSS that helps render the WW:xhtml theme form elements.

    <link rel="stylesheet" href="<@s.url value='/struts/xhtml/styles.css' includeParams='none' encode='false' />" type="text/css"/>
    <#include "/${parameters.templateDir}/simple/head.ftl" />

The head template imports a style sheet.

    .wwFormTable {}
    .label {font-style:italic; }
    .errorLabel {font-style:italic; color:red; }
    .errorMessage {font-weight:bold; text-align: center; color:red; }
    .checkboxLabel {}
    .checkboxErrorLabel {color:red; }
    .required {color:red;}
    .tdLabel {text-align:right; vertical-align:top; }

### form template

The xhtml form template sets up the wrapping table around all the other WW:xhtml theme form elements. In addition to creating this wrapping table, the opening and closing templates also, if the `validate` parameter is set to true, enable Pure JavaScript Client Side Validation.

    <#include "/${parameters.templateDir}/xhtml/form-validate.ftl" />
    <#include "/${parameters.templateDir}/simple/form.ftl" />
    <#include "/${parameters.templateDir}/xhtml/control.ftl" />

The closing template, `form-close.ftl*`:

    <#include "/${parameters.templateDir}/xhtml/control-close.ftl" />
    <#include "/${parameters.templateDir}/simple/form-close.ftl" />
    <#include "/${parameters.templateDir}/xhtml/form-close-validate.ftl" />

Children Show Children Hide Children
xhtml form template (Apache Struts 2 Documentation)
xhtml head template (Apache Struts 2 Documentation)