Search code examples
javajqueryjspspring-mvcspring-form

Do not clear form content upon POST


I am using a Spring form with tabs, each tab again has a form. The problems are

1)After the form submission, the error messages are displayed but the form content is cleared which should not happen
2)After the form submission the control immediately goes to the default tab which is the first tab in my case. I want the control to stay on current tab after transaction and not go to default tab.

This is the first time I am working using Spring MVC, JSP, jQuery. Please let me know if I have to make any changes.

administrator.jsp

Below is the code for Manage Users tab functionality.

jQuery:

$(document).ready(function() {
$('#btnManage').click(function() {
            if($.trim($('#userId').val()) == ''){
                $('#area5').html("User Id is a required field.");
             }else if($.trim($('#region').val()) == 'NONE'){
                $('#area5').html("Region is a required field.");
             }else{
            $('#manageUserForm').submit();
             }
        });
$('#btnCancel5').click(function(e) {
            $('#manageUserForm').trigger("reset");
            $('#area5').html("");
        });
});

administrator.jsp:

<div id="tab5" class="tab">
        <div class="devices" >
        <form:form method="post" id="manageUserForm" modelAttribute="adminUser" action="/DeviceManager/admin/manageUser">
        <div align=center class="message" id="area5">${serverError5}</div> 
        <div>
            <div class="plLabelSearch">User Id:</div> 
            <div class="plinput"><form:input path="userId" type="text" size="29"/></div>
        </div>
        <div>
            <div class="plLabelSearch">Region:</div> 
            <div class="plselect">
            <form:select path="region">
            <form:option value="NONE" label="------- Select One -------" />
            <form:option value="A" label="A"/>
            <form:options items="${regionList}"/>
            </form:select>
        </div>
        </div>
        <br>
        <br>
        <div>
            <div class="plLabelSearch">&nbsp;</div>
            <div class="plinput"><a id="btnManage" class="abutton">OK</a></div>
            <div class="plinput"><a id="btnCancel5" class="abutton">Cancel</a></div>
        </div>
    </form:form>
        </div>
    </div>        

Solution

  • You need to re-render the page with the data from the original form submission.

    In your case this probably means your form should contain a few hidden fields to allow you to determine which tab you are on, your JSP will need to be able to understand this data and use it to select the appropriate tab.