Struts 校验失败 值丢失

struts.xml配置
<package name="default" extends="struts-default">
   xxxxx
</package>
. 其中,struts-default包含了struts2默认的拦截器栈.
<interceptor-stack name="defaultStack">
                <interceptor-ref name="exception"/>
                <interceptor-ref name="alias"/>
                <interceptor-ref name="servletConfig"/>
                <interceptor-ref name="i18n"/>
                <interceptor-ref name="prepare"/>
                <interceptor-ref name="chain"/>
                <interceptor-ref name="scopedModelDriven"/>
                <interceptor-ref name="modelDriven"/>
                <interceptor-ref name="fileUpload"/>
                <interceptor-ref name="checkbox"/>
                <interceptor-ref name="multiselect"/>
                <interceptor-ref name="staticParams"/>
                <interceptor-ref name="actionMappingParams"/>
                <interceptor-ref name="params">
                    <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
                </interceptor-ref>
                <interceptor-ref name="conversionError"/>
                <interceptor-ref name="validation">
                    <param name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>
                <interceptor-ref name="workflow">
                    <param name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>
                <interceptor-ref name="debugging"/>
            </interceptor-stack>


该配置在struts2-core.jar的struts-default.xml文件里.

值丢失原因:
  当到conversionError Interceptor类型转换失败时,会添加校验失败信息。
 
if(action instanceof ValidationAware)
                {
                    ValidationAware va = (ValidationAware)action;
                    va.addFieldError(propertyName, message);
                }

  当conversionError Interceptor执行完,到workflow Interceptor时,如果发现有校验错误信息,直接return "input"给struts,就不会再到你的Action里面,导致值丢失。

解决办法:
   配置自己的拦截器栈,去除workflow Interceptor。
    <interceptors>
            <interceptor-stack name="xxx">
                <interceptor-ref name="exception"/>
                <interceptor-ref name="alias"/>
                <interceptor-ref name="servletConfig"/>
                <interceptor-ref name="i18n"/>
                <interceptor-ref name="prepare"/>
                <interceptor-ref name="chain"/>
                <interceptor-ref name="scopedModelDriven"/>
                <interceptor-ref name="modelDriven"/>
                <interceptor-ref name="fileUpload"/>
                <interceptor-ref name="checkbox"/>
                <interceptor-ref name="multiselect"/>
                <interceptor-ref name="staticParams"/>
                <interceptor-ref name="actionMappingParams"/>
                <interceptor-ref name="params">
                    <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
                </interceptor-ref>
                <interceptor-ref name="conversionError"/>
                <interceptor-ref name="validation">
                    <param name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="xxx" />

   或者想办法在到达workflow Interceptor之前,把你要的值放进struts2的value stack.

struts流程参考
http://hi.baidu.com/hyy_lover/item/4b26c10e3afc0875d45a11f8

你可能感兴趣的:(Struts 校验失败 值丢失)