Search code examples
htmltwitter-bootstrapjspstruts2struts2-bootstrap-plugin

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


When I add struts tags, the formatting disappears. What do I do?

This is my code:

    <div class="panel-body" style="background-color:;">

    <div align="center">
        <font size=3> <s:form id="FormAddUser" name="FormAddUser"
                action="AddUser" method="post" class="form-horizontal">
                <div class="row" align="center">
    <div class="col-sm-6">
        <div class="col-sm-4" align="right">
            <label>User ID</label>
        </div>

        <s:textfield name="UserId" class="form-control"></s:textfield>
    </div>
</div>
<br>
<div class="row" align="center">
    <div class="col-sm-6">
        <div class="col-sm-4" align="right">
            <label>Name</label>
        </div>

        <s:textfield name="Name" class="form-control"></s:textfield>
    </div>
</div>
<br>
<div class="row" align="center">
    <div class="col-sm-6">
        <div class="col-sm-4" align="right">
            <label>Address</label>
        </div>

        <s:textarea name="Address" class="form-control"></s:textarea>
    </div>
</div>
<br>
<div class="row" align="center">
    <div class="col-sm-6">
        <div class="col-sm-4" align="right">
            <label>Birth date</label>
        </div>

        <s:textfield name="DOB" class="form-control" placeholder="dd/mm/yy"></s:textfield>
    </div>
</div>
<br>
<div class="row" align="center">
    <div class="col-sm-6">
        <div class="col-sm-4" align="right">
            <label>Password</label>
        </div>

        <s:password name="Password" class="form-control"></s:password>

    </div>
</div>
<br>
<div class="row" align="center">
    <div class="col-sm-6">
        <div class="col-sm-4" align="right">
            <label>Email Id</label>
        </div>

        <s:textfield name="EmailId" class="form-control"
            placeholder="firstname.lastname@iiitb.org"></s:textfield>
    </div>
</div>
<br>
<div class="row" align="center">
    <div class="col-sm-6">
        <div class="col-sm-4" align="right">
            <label>Specialization</label>
        </div>

        <s:textfield name="Specialization" class="form-control"
            placeholder="CS/DS/NC/SE"></s:textfield>
    </div>
</div>
<br>
<div class="row" align="center">
    <div class="col-sm-6">
        <div class="col-sm-4" align="right">
            <label>Specialization</label>
        </div>

        <s:file name="Image" class="form-control" id="Image" key="Image"
            label="Select a File to change photo" enctype="multipart/form-data"></s:file>

    </div>
</div>
<br>
<div class="row" align="center">
    <div class="col-sm-7">
        <s:submit class="btn btn-primary" label="Add" onclick="check()" />
        <a href="admin.jsp" class="btn btn-primary btn-md"> <span
            class="glyphicon glyphicon-repeat"></span> Back
        </a>
    </div>
</div>

</s:form>


</font>
</div>
</div>
</div>

This is how it renders. The problem is because of the struts tags. I have included the struts-bootstrap jar. What could be the possible error?

enter image description here


Solution

  • 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.