验证失败时我的表单没有重新填充

My form is not repopulated when validation fails

我有一个简单的页面,其中包含一个 firstName 字段和一个 lastName 字段,这两个字段都是必需的。当我只输入一个字段时,我得到了正确的错误消息,但我丢失了我输入的值。我确实有字段的 getter 和 setter,如果我输入它们,页面会正确使用这些值。一切似乎都以与我之前项目相同的方式设置,而且我没有丢失这些页面上的数据。我知道之前有人问过这个问题,但根据这些答案我找不到我的代码有任何问题,所以我希望有人能看到我做错了什么。

我的jsp代码是:

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

我的 struts 操作是:

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

我的磁贴代码是:

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

我的 struts2 结果类型和拦截器代码是:

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

我的验证 xml 是:

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

任何帮助将不胜感激,谢谢!

这是请求的 dirtyFlag() 代码(和相关代码):

//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();
}
}

你猜怎么着?

<s:textfield name = "firstName" 
               id = "firstName"

            value = "" 

        maxlength = "50" 
            class = "form-control" 
         onchange = "dirtyFlag();" 
         tabindex = "4"/>

value="" 强制值始终为空。

  • 使用value="%{firstName}",或
  • 删除它并让 name="firstName" 完成工作 (推荐).