java 在 Struts2 中重定向时在 url 中传递参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11006341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:23:00  来源:igfitidea点击:

Passing parameters in url while redirecting in Struts2

javastruts2

提问by MistyD

Possible Duplicate:
Strut2 - Get Property value in next Action

可能的重复:
Strut2 - 在下一个操作中获取属性值

I am trying to achieve the following using Struts2

我正在尝试使用 Struts2 实现以下目标

response.sendRedirect("Pay.jsp?msg=transfer");

This is what I am doing:

这就是我正在做的:

 <action name="AddPayAction" class="controller.AddPayAction">
            <param name="paraA"></param>
            <param name="paraB"></param>
            <param name="msg">SomeMessage</param>
            <result name="error">/Error.jsp</result>
            <result name="success" type="redirect">/Pay.jsp</result>
 </action>

Any suggestions why the above doesn't get redirected as:

任何建议为什么上述内容不会被重定向为:

      Pay.jsp?msg=SomeMessage

回答by MistyD

So far the solution that worked for me was

到目前为止,对我有用的解决方案是

 <result name="success" type="redirect">
                <param name="location">/Pay.jsp?msg=${msg}</param>
 </result>

where the setters and getters of msg are defined in the action

其中 msg 的 setter 和 getter 是在 action 中定义的

回答by anupam

<action name="AddPayAction" class="controller.AddPayAction">
      <result name="success" type="redirectAction">
        <param name="actionName">Pay</param>
        <param name="msg">SomeMessage</param>
      </result>
  </action>

  <action name="Pay">
      <result name="success">/Pay.jsp</result>
  </action>      

If you want to pass a variables value e.g. msgwhose getter/setter is declared in AddPayActionthen use this

如果你想传递一个变量值,例如msg它的 getter/setter 声明在AddPayAction然后使用这个

<param name="msg">${msg}</param>

A similar post

一个类似的帖子

回答by jddsantaella

Taking a look to your code, it looks like you just want to end the action in a JSP, so I do not understand why you are trying to do it using redirecitontype. I recommend to use default redirect type: dispatcher

看看你的代码,看起来你只是想在 JSP 中结束这个动作,所以我不明白你为什么要使用redirecitontype来完成它。我建议使用默认重定向类型:dispatcher

<action name="AddPayAction" class="controller.AddPayAction">
    <result name="error">/Error.jsp</result>
    <result name="success">/Pay.jsp</result>
</action>

Notice that as dispatcheris de default type you don't need to write type="dispatcher"in the results. So, now, if you want to have variables available in the JSP, you just need to declare those variables in the action with its get/set methods. For example:

请注意,dispatcher您不需要type="dispatcher"在结果中写入默认类型。所以,现在,如果您想在 JSP 中使用变量,您只需要在操作中使用其 get/set 方法声明这些变量。例如:

private String msg;

public String AddPayAction() {

    // your action code

    this.setMsg("my message");

    return SUCCESS;

}

public String getMsg() {

    return msg;
}

public void setMsg(String msg) {

    this.msg= msg;
}

Take a look to the result typesavailable.

查看可用的结果类型

回答by nmc

I think that should be something like:

我认为应该是这样的:

<action name="AddPayAction" class="controller.AddPayAction">
    <result name="error">/Error.jsp</result>
    <result name="success" type="redirect">
        <param name="location">Pay.jsp</param>
        <param name="paraA"></param>
        <param name="paraB"></param>
        <param name="msg">SomeMessage</param>
    </result>
</action>

See example at http://struts.apache.org/2.1.6/docs/redirect-result.html

请参阅http://struts.apache.org/2.1.6/docs/redirect-result.html 中的示例