How to avoid generating more HTML elements in Struts

When Struts(2)’ UI tags are used, more HTML elements are generated automatically.For example, when defining <s:form ... > it includes form within a table(even though such table is not declared by us). So, if any CSS is used(lets say, a div tag is only used and located input elements according to the CSS), all elements are not viewed as expected because of auto generated elements and their postions. Even if the struts is allowed to generate such elements and we add CSS to them, then another weight must be bored to add css to design auto generated html elements because more elements are generated in various positions and sizes. Also as I feel, more html codes may effect the browser page loading time.

So is there a way to avoid generating such elements when struts UI tags are used
is there a convenient way to work with CSS in Struts UI tags ? (I read a question in Stackoverflow and it had said for an answer that, to add css finally which the struts generated. But it is not that much easy when compare to normal jsp html designing level..)


In short Struts2 work on the concept of themes and it provides certain theme out of the box. Based on the theme selected it will generate some HTML code for the used tags when rendering the view.

Themes are really a very nice and beautiful concept of Struts2 and it even provides you the way to create a custom themes are per your requirement.

coming to your Question by default Struts2 use xhtml theme which means it will generate some HTML (which includes tables as name suggested) while rendering the HTML for the used tags.

you can control the HTML generation by specifying simple theme either in file or in struts.xml file

Struts Properties file


Struts.xml file

<constant name="struts.ui.theme" value="simple" />

Other theme being provided by S2 out of the box are

  • css_xhtml: generate div based HTML
  • ajax: for ajax tags

Note that simple theme will not generate any extra HTML but only tags equivalent HTML code and you have full control to customize ur view by providing your own CSS and any other properties.

Above settings in properties file as well in struts.xml will be applicable to entire application which means each and every tag will generate same HTML for whole appllication.beside that if you want to control per tag HTML generation each S2 tag as a property namely theme, provide value to this property like simple,css_xhtml etc to control per tag HTML generation.

There is one more way to control the theme per request basis by using the following code <s:set name="theme" value="'simple'" scope="page" /> or

<s:set name="theme" value="%{myTheme}" scope="page" />

Choice is all yours which way you want to go

Leave a Reply

Your email address will not be published. Required fields are marked *