Why are Bootstrap’s form elements rendered terribly with Struts2-Boostrap-Plugin?

Struts2 uses Themes to generate HTML from Tags: a different theme chosen, a different HTML in output.

The default theme is XHTML, that generates <td>, <label> and other stuff around your elements.

Usually, I recommend to use the simple theme, that generates almost no additional code, and that would make your code work as-is. Put this constant in struts.xml to check it out:

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

But in your case, since you said you’ve included the Struts2-bootstrap-plugin in your project, then… simply use it ! You are NOT using it in your code… including the JAR is not enough, you need to set the bootstrap theme as the default one:

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

and declare the struts-bootstrap taglib to use the <sb:head/> tag, as described in the official documentation:

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sb" uri="/struts-bootstrap-tags" %>
<!DOCTYPE html>
<html lang="en">
<head>
    ...
    <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
    <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <sb:head/>
</head>
<body>
...
</body>
</html>

Then remove all the HTML that you’ve written manually, and start using the plugin.

Leave a Comment