Including html in Struts2 form tags.

Recently I had an issue using Struts2 tags to create a form that contained a checkbox.  I wanted the label for the checkbox to contain a link (for example –, but the helpful Struts2 tags kept escaping the html.  After several evenings banging my head against this, I eventually I found that the way to fix this was to use themes.  This blog describes a similar issue.

Struts2 uses themes when converting struts tags to html.  Struts2 comes with 3 themes.  They are simple, xhtml and css_xhtml.  If you don’t specify a theme the default is xhtml.  You can set the theme you want to use for a form or field using the attribute ‘theme’


<s:form id=“regform” theme=“simple” action=“register_submit.action” method=“post” validate=“true”/>

xhtml and css_xhtml will both escape the html, and simple won’t even show it.

Luckily you can create your own themes.

The themes are in the jar file struts2-core.jar in the directories template.{themename}.  Each directory contains a number of freemarker script files for the different Struts2 tags.  The script checkbox.ftl is used to create the final html for the Struts2 tag <s:checkbox/>

Create a directory under your src folder called template.{your theme name}.

Extract all files from the jar for the theme you want to extend and copy them to your new directory.

You can now edit the theme file.  To stop escaping html in the label of a checkbox, you need to edit checkbox.ftl.  If you copied the files from the xhtml theme you need to need to change line 120 from




so remove the ‘?html’.

Edit your struts.xml file so struts knows where to find your theme.

<constant name=“struts.ui.templateDir” value=“template” />

You now have a choice.  If you want to use your theme as the default for your whole application then also add the below line to your struts.xml

<constant name=”struts.ui.theme” value=”dbe/>

Or you can just use your theme for your form

<s:form id=“regform” theme=“dbe” action=“register_submit.action” method=“post” validate=“true”/>

Or you can just use your theme for the checkbox

<s:checkbox name=“tAndCAgreed” theme=“dbe” key=’I have read and agree to the <a href=“/disclaimer_public.action” target=“_blank”>Terms and Conditions</a>.‘ />

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s