简体   繁体   English

Struts2验证重定向操作输入 - FieldErrors已经过去了

[英]Struts2 Validation Redirect Action Input - FieldErrors are Gone

Below is my configuration for interceptors: 以下是拦截器的配置:

    <interceptors>
        <interceptor-stack name="storeStack">
            <interceptor-ref name="defaultStack"/>
            <interceptor-ref name="store">
                <param name="operationMode">STORE</param>
            </interceptor-ref>
        </interceptor-stack>

        <interceptor-stack name="retrieveStack">
            <interceptor-ref name="defaultStack"/>
            <interceptor-ref name="store">
                <param name="operationMode">RETRIEVE</param>
            </interceptor-ref>
        </interceptor-stack>

        <interceptor-stack name="appDefaultStack">
            <interceptor-ref name="defaultStack">
                <param name="exception.logEnabled">true</param>
                <param name="exception.logLevel">ERROR</param>
            </interceptor-ref>
        </interceptor-stack>
    </interceptors>

And my action: 我的行动:

    <action name="update/*" class="comics.comics.ComicsAction" method="view">
        <interceptor-ref name="retrieveStack" />
        <param name="key">{1}</param>
        <result>/comics/comics-chapters.jsp</result>
    </action>


    <action name="add" class="comics.comics.ChapterAction" method="add">
        <interceptor-ref name="storeStack" />
        <result type="redirectAction">
            <param name="parse">true</param>
            <param name="key">${key}</param>
            <param name="actionName">update</param>
            <param name="namespace">/comics</param>    
        </result>

        <result name="input" type="redirectAction">
            <param name="parse">true</param>
            <param name="key">${key}</param>
            <param name="actionName">update</param>
            <param name="namespace">/comics</param>    
        </result>

    </action>

In my comics-chapters.jsp page, field errors are gone. 在我的comics-chapters.jsp页面中,字段错误消失了。 But when i change the input result to: 但是当我将输入结果更改为:

         <result name="input">/comics/comics-chapters.jsp</result>

field errors are displayed. 显示字段错误。

How to display field errors when my input result is an action? 输入结果为动作时如何显示字段错误?

This is because when you are using redirectAction , Struts2 is creating a new request and response object and will clear the value stack and place new request and response in the value stack so it will overwrite the previous data. 这是因为当您使用redirectAction ,Struts2正在创建一个新的请求和响应对象,并将清除值堆栈并在值堆栈中放置新的请求和响应,以便它将覆盖以前的数据。

You have few options here 你在这里几乎没有选择

  1. Store your messages in the action and on the redirectAction you can fetch them from the action and show them to the user. 将您的消息存储在操作和redirectAction您可以从操作中获取它们并将其显示给用户。
  2. use MessageStoreInterceptor 使用MessageStoreInterceptor

There is a long pending request for scope result which will be able to handle such use-cases, but i believe its coming in struts-3.x (aka 2.5) version. 有一个很长的待处理范围结果请求将能够处理这样的用例,但我相信它的struts-3.x(又名2.5)版本。

try put store interceptor on top of defaultStack like this: 尝试将store拦截器置于defaultStack之上,如下所示:

<interceptor-stack name="storeStack">
         <interceptor-ref name="store">
                <param name="operationMode">STORE</param>
         </interceptor-ref>
         <interceptor-ref name="defaultStack"/>

</interceptor-stack>

<interceptor-stack name="retrieveStack">
         <interceptor-ref name="store">
                <param name="operationMode">RETRIEVE</param>
         </interceptor-ref>
         <interceptor-ref name="defaultStack"/>

 </interceptor-stack>

please Always notice the order of interceptors. 始终注意拦截器的顺序。

As Umesh says the redirectAction which you have specified as result causes the error messages to be cleared from the value stack when the new action is called. 正如Umesh所说,您指定的redirectAction导致在调用新操作时从值堆栈中清除错误消息。 Another option you have though in this case is to use the Chain result. 在这种情况下,您有另一个选择是使用Chain结果。 The chain result causes another action to be called keeping all existing values in the value stack except error messages, for which you will have to use the ChainingInterceptor . 链结果导致调用另一个操作,保留值堆栈中除错误消息之外的所有现有值,您必须使用ChainingInterceptor
You can also see this blog post for more details on how to use the chain result and ChainingInterceptor. 您还可以查看此博客文章 ,了解有关如何使用链结果和ChainingInterceptor的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM