【问题标题】:struts2 errors loststruts2 错误丢失
【发布时间】:2011-12-02 11:49:59
【问题描述】:

我尝试使用 struts2 验证框架。我有一个 ActionName-validation.xml。 struts.xml中的入口如下

    <action name="registerCandidateStep1" class="candidateAction"
            method="registerCandidateStep1">
            <result name="success" type="tiles">registerCandidate</result>
            <result name="input" type="chain">
                <param name="actionName">loadCandidateRegistrationForm</param>
                <param name="namespace">/.secureActions</param>
            </result>
        </action>
    <action name="loadCandidateRegistrationForm" class="loadCandidateFromAction"
        method="loadCandidateRegistrationForm">
        <result name="success" type="tiles">registerCandidate</result>
    </action>

错误条件请求确实被转发到“loadCandidateRegistrationForm”,但我没有在页面上看到错误。我在jsp中包含了标签

StrutsConfig.xml

            <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
        <constant name="struts.objectFactory" value="spring" />
        <constant name="struts.devMode" value="false" />

        <constant name="struts.action.extension" value="action" />

        <constant name="struts.custom.i18n.resources" value="global" />

        <package name="org" namespace="/"
            extends="struts-default,json-default">

            <result-types>
                <result-type name="tiles"
                    class="org.apache.struts2.views.tiles.TilesResult" />
            </result-types>

            <global-results>
                <result name="welcome" type="tiles">welcome</result>
            </global-results>

        </package>

        <package name="org.unsecureActions" extends="org">
            <!--
                This package contains such a actions which doesn't need user logged
                in.
            -->

            <action name="welcome" method="forwardAction" class="baseAction">
                <result name="success" type="tiles">welcome</result>
            </action>

            <action name="logout" method="logoutCandidate" class="logoutAction">
                <result name="success" type="tiles">welcome</result>
            </action>

            <action name="loadAdvanceSearchForm" method="loadAdvanceSearch"
                class="advanceSearchAction">
                <result name="success" type="tiles">advanceSearch</result>
            </action>

            <!--Candidate workflow  actions -->
            <action name="registerCandidateStep1" class="candidateAction"
                method="registerCandidateStep1">
                <interceptor-ref name="store">
                    <param name="operationMode">STORE</param>
                </interceptor-ref>
                <interceptor-ref name="defaultStack" />

                <result name="success" type="tiles">registerCandidate</result>
                <result name="input" type="redirect">loadCandidateRegistrationForm.action</result>
            </action>

            <action name="registerCandidateStep2" class="candidateAction"
                method="registerCandidateStep2">
                <result name="success" type="tiles">registerCandidate</result>
                <result name="input" type="tiles">loadCandidateRegistrationForm</result>
            </action>
            <action name="registerCandidateStep3" class="candidateAction"
                method="registerCandidateStep3">
                <result type="chain">
                    <param name="actionName">loginCandidate</param>
                    <param name="namespace">/org.unsecureActions</param>
                </result>
                <result name="input" type="tiles">registerCandidate</result>
            </action>

            <action name="loadCandidateRegistrationForm" class="loadCandidateFromAction"
                method="loadCandidateRegistrationForm">
                <interceptor-ref name="store">
                    <param name="operationMode">RETRIEVE</param>
                </interceptor-ref>
                <result name="success" type="tiles">registerCandidate</result>
            </action>
            <!--Candidate workflow  actions -->

            <action name="loginCandidate" class="loginAction" method="loginCandidate">
                <result name="success" type="tiles">home</result>
                <result name="input" type="tiles">welcome</result>
            </action>

        </package>

        <package name="org.secureActions" extends="org">
            <!--
                This package contains such a actions which needs user must logged in
                before executing these.
            -->

            <interceptors>
                <!--
                    Following interceptor checks for is user logged in before executing
                    the action.
                -->
                <interceptor name="contextSecurityInterceptor"
                    class="org.kovid.matrimony.interceptor.ContextSecurityInterceptor">
                </interceptor>

                <!--
                    This stack is as like default stack provided ny Struts Only
                    difference is at the bottom of folowing stack where we included our
                    "contextSecurityInterceptor" interceptor.
                -->
                <interceptor-stack name="applicationStack">
                    <interceptor-ref name="exception" />
                    <interceptor-ref name="alias" />
                    <interceptor-ref name="servletConfig" />
                    <interceptor-ref name="prepare" />
                    <interceptor-ref name="i18n" />
                    <interceptor-ref name="chain" />
                    <interceptor-ref name="debugging" />
                    <interceptor-ref name="profiling" />
                    <interceptor-ref name="scopedModelDriven" />
                    <interceptor-ref name="modelDriven" />
                    <interceptor-ref name="fileUpload" />
                    <interceptor-ref name="checkbox" />
                    <interceptor-ref name="staticParams" />
                    <interceptor-ref name="actionMappingParams" />
                    <interceptor-ref name="params" />
                    <interceptor-ref name="conversionError" />
                    <interceptor-ref name="validation" />
                    <interceptor-ref name="workflow" />
                    <interceptor-ref name="contextSecurityInterceptor" />
                </interceptor-stack>
            </interceptors>

            <!--
                Setting default stack for interceptor taking care of this packageS.
            -->
            <default-interceptor-ref name="applicationStack" />

            <action name="home" method="forwardAction" class="baseAction">
                <result name="success" type="tiles">home</result>
            </action>

            <action name="loadAdvanceSearchForm" method="loadAdvanceSearch"
                class="advanceSearchAction">
                <result name="success" type="tiles">advanceSearch</result>
            </action>

            <action name="simpleSearch" method="simpleSearch" class="simpleSearchAction">
                <result name="success" type="tiles">search</result>
                <result name="input" type="tiles">home</result>
            </action>

            <action name="advanceSearch" method="advanceSearch" class="advanceSearchAction">
                <result name="success" type="tiles">search</result>
            </action>
            <action name="loadImage" method="loadImage" class="imageAction">
                <result name="imageData" type="stream">
                    <param name="contentType">${imageContentType}</param>
                    <param name="inputName">imageStream</param>
                    <param name="contentDisposition">filename="candidate.jpeg"</param>
                    <param name="bufferSize">${myBufferSize}</param>
                </result>
            </action>

        </package>

    </struts>

【问题讨论】:

    标签: validation struts2


    【解决方案1】:

    不鼓励使用动作链,原因之一是与验证拦截器的交互。您可以通过将一些值(如验证消息)从一个操作显式映射到下一个操作来绕过它,但是……不要链接操作——几乎没有理由这样做。

    【讨论】:

      【解决方案2】:

      就像Dave 所说,Action Chaining 不鼓励。

      Action Chaining

      不要在家里尝试这个

      一般来说,不推荐使用动作链。 首先探索其他选项,例如 Redirect After Post 技术。

      如果您有充分的理由使用Redirect After Post,请使用

      【讨论】:

      • 我根据消息存储拦截器更新了 strutsConfig。请在原始帖子中找到更新的 strutsConfig。完成这些更改后,错误消息只会显示在服务器控制台上。在提交表单时,即使出现错误,它也会转向“成功”操作。感谢您的帮助。
      • error messages are getting displayed on the server console only,动作类是否扩展了ActionSupport
      • 谢谢,它与 ActionSupport 合作。非常感谢您的帮助:)
      • 因为它是一个重定向操作,所以我的值正在丢失。有没有一种简单的方法可以重新填充这些值?我确实找到了关于同一 stackoverflow.com/questions/5287160/… 的帖子,因为它使用链接,我无法继续这样做。
      • 建议使用 @kunal Parameters in configuration results(URL 中的参数)。但是再一次,你不应该使用Action Chaining。另请参阅 Redirect ResultRedirect Action Result
      【解决方案3】:

      错误的生命周期持续到当前操作。如果您转发/重定向到另一个操作,那么错误将会丢失。

      改用session 范围。将 ArrayList 放入 session 并附加您的错误/消息。

      动作/拦截器:

      Map session         = ActionContext.getContext().getSession();
      List<String> errors = session.get("errorMessages");
      
      if(errors==null)
         session.put("errorMessages", errors=new ArrayList<String>() );
      
      errors.add("Invalid Username/Password. Kindly retry");
      

      JSP:

      <s:iterator var="msg" value="#session.errorMessages" >
           <p> ${msg} </p>
      </s:iterator>
      <s:set scope="session" var="errorMessages" value="%{null}" /><%--flush errors--%>
      

      【讨论】:

        猜你喜欢
        • 2016-08-28
        • 2020-02-19
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        • 1970-01-01
        • 2011-09-23
        • 2013-04-23
        • 2017-10-12
        相关资源
        最近更新 更多