validationjspstruts2struts-validationinterceptorstack

My form is not repopulated when validation fails


I have a simple page with a firstName field and a lastName field both of which are required. When I only enter one field I get the correct error message but I lose the value I have entered. I do have getters and setters for the fields and the page uses the values correctly if I enter them. Everything seems set up in the same way I had it on my previous project and I did not lose data on those pages. I know this question has been asked before but I can't find anything wrong with my code based on those answers so I am hoping someone can see what I am doing wrong.

My jsp code is:

<s:form action = "sign" validate="true" method="post" id="sign" >
<s:hidden name="dateSigned"></s:hidden>    
<div class="row">
    <div class = "col-md-7 col-xs-12">
        <div class="col-md-3 col-sm-3 col-xs-12"><label for="firstName" class="pull-right"><span class="required">*</span><s:text name="firstName"></s:text>:</label></div>
        <div class="col-md-3 col-sm-3 col-xs-12"><s:textfield name = "firstName" id = "firstName" value="" maxlength="50" class="form-control" onchange="dirtyFlag();" tabindex="4"/></div>
        <div class="col-md-3 col-sm-3 col-xs-12"><label for="lastName" class="pull-right"><span class="required">*</span><s:text name="lastName"></s:text>:</label></div>
        <div class="col-md-3 col-sm-3 col-xs-12"><s:textfield name = "lastName" id = "lastName" value="" maxlength="50" class="form-control" onchange="dirtyFlag();" tabindex="5"/></div>
    </div>
</div>
   <div class="row">
        <s:submit method="save" key="button.save" cssClass="submit" onclick="clearDirtyFlag();" tabindex="1"/>
    </div>

My struts action is:

        <action name="sign" class="gov.mo.dnr.egims.controller.request.RequestorSignatureAction">           
        <result name="success" type="tiles">reqSign</result>
        <result name="input" type="tiles">reqSign</result>
        <result name="error" type="tiles">error</result>
        <result name="ownerSignature" type="redirectAction">
            <param name="actionName">owner</param>
            <param name="namespace">/req</param>
            <param name="requestId">${requestId}</param>
        </result>           
    </action>  

My tiles code is:

    <definition name="reqSign" extends="baseLayout">
    <put-attribute name="navbar"
        value="/WEB-INF/pages/request/nav_secondary.jsp" />
    <put-attribute name="content"
        value="/WEB-INF/pages/request/RequestorSignature.jsp" />
</definition>

My struts2 result-types and interceptor code is:

        <result-types>
        <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>  
    <interceptors>
        <interceptor name="validUser" class="gov.mo.dnr.egims.controller.interceptors.ValidUser" />
        <interceptor-stack name="validUserStack">
            <interceptor-ref name="validUser" />
            <interceptor-ref name="defaultStack" />
        </interceptor-stack>
        <interceptor name="userInRole" class="gov.mo.dnr.egims.controller.interceptors.UserInRole">
            <param name="rolesList">Administrator,Geologist,Requestor,Reviewer, User</param>
        </interceptor>
        <interceptor-stack name="userInRoleStack">
            <interceptor-ref name="userInRole" />
        </interceptor-stack>
    </interceptors>     

My validation xml is:

<validators>

<field name="firstName">
    <field-validator type="requiredstring">
        <param name="trim">true</param>
        <message key="errors.required" />
    </field-validator>      
    <field-validator type="regex">
        <param name="regex">^[ a-zA-Z0-9'\-,.!:#@/]*$</param>
        <message key="errors.invalidCharacter"/>
    </field-validator>
    <field-validator type="stringlength">
        <param name="maxLength">50</param>
        <message key="errors.maxlength"></message>
    </field-validator>
</field>


<field name="lastName"> 
        <field-validator type="requiredstring">
        <param name="trim">true</param>
        <message key="errors.required" />
    </field-validator>      
    <field-validator type="regex">
        <param name="regex">^[ a-zA-Z0-9'\-,.!:#@/]*$</param>
        <message key="errors.invalidCharacter"/>
    </field-validator>
    <field-validator type="stringlength">
        <param name="maxLength">50</param>
        <message key="errors.maxlength"></message>
    </field-validator>
</field>

Any help would be appreciated, thanks!

Here is the requested dirtyFlag() code (and related code):

//needToConfirm used by confirm exit
var needToConfirm = false;

//default string message to display before leaving a page when data on the page has changed
//these changes were made to allow the message displayed before exiting to be customized
var defaultWarningMessage = "There is unsaved data on this screen.  Do you want to exit anyway?";
var dirtyWarningMessage = defaultWarningMessage;

//this line intercepts attempts to leave the page via clicking the X button.
window.onbeforeunload = function ()
{
if (needToConfirm)
{   
    var outMessage = dirtyWarningMessage;
    setDirtyWarningMessage(defaultWarningMessage);
    return outMessage;
}
// no changes - return nothing              
}

//function to set the string message to display before leaving a page when data on the page has changed
function setDirtyWarningMessage(inMessage)
{
dirtyWarningMessage = inMessage;
}

//function to set dirty flag
function dirtyFlag()
{
//alert ("Dirty flag set");
needToConfirm = true;
}

//function to clear dirty flag
function clearDirtyFlag()
{
//alert ("Dirty flag cleared");
needToConfirm = false;
}


//function to confirm close
function confirmClose()
{
if (needToConfirm)
{
    var agree=confirm("Some data on the page is modified. Do you wish to leave the page without saving changes?");
    if (agree == true)
    {
        needToConfirm = false;
        window.close();
    }
    else
    {
        return false;
    }
} 
else 
{
    window.close();
}
}

Solution

  • Guess what ?

    <s:textfield name = "firstName" 
                   id = "firstName"
    
                value = "" 
    
            maxlength = "50" 
                class = "form-control" 
             onchange = "dirtyFlag();" 
             tabindex = "4"/>
    

    value="" forces the value to always be empty.