验证失败后,我的表单不会重新填充

时间:2016-02-17 17:29:34

标签: validation jsp struts2 struts-validation interceptorstack

我有一个简单的页面,其中包含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();
}
}

1 个答案:

答案 0 :(得分:1)

猜猜是什么?

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

            value = "" 

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

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

  • 使用value="%{firstName}"
  • 将其删除并让name="firstName"完成工作 (推荐)