【问题标题】:Why can't i redirect an action to another action in Struts2?为什么我不能将一个动作重定向到 Struts2 中的另一个动作?
【发布时间】:2012-06-08 14:56:19
【问题描述】:

我正在使用 Struts2。我有一个对话框,其中包含通过“search_users”操作更新的人员列表。在这个列表旁边,我有一个表单,您可以在提交表单时通过调用“add_user”操作来添加另一个人。

我想要做的是,一旦执行了 add_user 操作,列表就会使用“search_user”操作进行更新。

我尝试在 struts.xml 中使用结果类型“重定向”,如下所示:

<action name="search_users" class="org.apache.struts.gestion_edt.controller.adm_proyectos.BLSubequipo" method="searchUsers">
            <result name="success">list.jsp</result>
        </action>

        <action name="add_user" class="org.apache.struts.gestion_edt.controller.adm_proyectos.BLTipoEntregable" method="addUser">
            <result name="success" type="redirectAction">search_users</result>
        </action>

但这不起作用。我究竟做错了什么?有什么我不知道的东西我应该添加到 struts.xml 文件中吗?

这是我得到的错误信息:

"Caused by: There is no result type defined for type 'redirect-action' mapped with name 'success'.  Did you mean 'redirectAction'? - result - file:/.../struts.xml:59:44
    at ..."

【问题讨论】:

  • 启动 Struts2.1x 或类似的重定向动作被更改为类似redirectAction 的驼峰式大小写,您使用redirectAction 遇到了什么问题?还有您使用的是哪个版本的S2?
  • 版本为2.3.1.2。问题是由于 struts.xml 文件有缺陷,项目无法构建。错误消息显示了更多细节。
  • 对于 2.3.1.2 redirectAction 是正确的
  • 检查我的回答是否解决了您的问题

标签: java struts2


【解决方案1】:

当前配置:

<action name="add_user" class="org.apache.struts.gestion_edt.controller.adm_proyectos.BLTipoEntregable" method="addUser">
   <result name="success" type="redirectAction">search_users</result>
</action>

根据documentation,正确的格式是:

<action name="add_user" class="org.apache.struts.gestion_edt.controller.adm_proyectos.BLTipoEntregable" method="addUser">
    <result type="redirectAction">
        <param name="actionName">search_users</param>
        <!--<param name="namespace">/secure</param> This is optional if your action where you are redirecting is in the same namespace you can leave this, if your action is in some other name space then provide the namespace--> 
    </result>
</action>

【讨论】:

  • 谢谢!这解决了我的问题。我尝试过这种方式,但后来我确实包含了 package 参数,即使它们都属于同一个包。感谢您的帮助。
  • 你如何告诉他们你去重定向成功?
  • @mprabhat 你在这两个地方都写了 class-name BLTipoEntregable 。第一个定义不应该是BLSubequipo吗???根据问题
【解决方案2】:

目前使用 Struts 2.3.20,这可行:

<result type="redirectAction">myAction</result>

我在之前的版本中没有确认。

【讨论】:

    【解决方案3】:

    我不是 Struts 大佬,但根据文档,您的重定向在语法上似乎不正确:http://struts.apache.org/2.1.6/docs/redirect-action-result.html

    <package name="public" extends="struts-default">
        <action name="login" class="...">
            <!-- Redirect to another namespace -->
            <result type="redirect-action">
                <param name="actionName">dashboard</param>
                <param name="namespace">/secure</param>
            </result>
        </action>
    </package>
    

    【讨论】:

    • 谢谢,我刚试过,但也没有用。我将编辑帖子并添加错误消息。
    • redirect-action 用于旧版本的 Struts2,新版本使用 redirectAction(改变分隔词的约定)。否则,您的示例应该可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 2015-03-03
    • 2011-12-18
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 2012-05-16
    • 1970-01-01
    相关资源
    最近更新 更多