Java Struts2 动作之间的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/374099/
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-08-11 13:59:31  来源:igfitidea点击:

Struts2 parameters between actions

javajspstruts2parametersaction

提问by Giancarlo

I have to pass some parameter from an action to another action,for example to keep trace of an event.

我必须将一些参数从一个动作传递给另一个动作,例如为了跟踪一个事件。

What is the best way to do that?

最好的方法是什么?

I would not use session parameters. Thanks

我不会使用会话参数。谢谢

采纳答案by krosenvold

Assuming you are serverside within one action and wishing to invoke another action with some parameters.

假设您在一个操作中处于服务器端并希望使用某些参数调用另一个操作。

You can use the s:action tag to invoke another action, possibly with additional/other parameters than the original action:

您可以使用 s:action 标签来调用另一个动作,可能使用比原始动作附加/其他参数:

    <s:action name="myAction"  ignoreContextParams="true" executeResult="true">
        <s:param name="foo" value="bar"/>
    </s:action>

You can also use a standard struts-xml result type with a parameter:

您还可以使用带有参数的标准 struts-xml 结果类型:

<result name="success" type="redirect" >
      <param name="location">foo.jsp?foo=${bar}</param>
      <param name="parse">true</param>
      <param name="encode">true</param>
 </result>

If you want a client side redirect you have to send an url back to the client with the proper parameters, and maybe use some javascript to go there.

如果您想要客户端重定向,您必须使用正确的参数将 url 发送回客户端,并且可能使用一些 javascript 去那里。

        <s:url action="myAction" >
            <s:param name="foo" value="bar"/>
        </s:url>

回答by krosenvold

<td>
   <s:url id="url" action="Logging">
      <s:param name="m_userNameInAction"><s:property value="m_userNameInForm"/></s:param>
    </s:url>
    <s:a href="%{url}">English</s:a>
</td>

回答by Shimit

Use url tag in the struts core tags, sample is given below:

在 struts 核心标签中使用 url 标签,示例如下:

                <s:url var="idurl" action="EditEnterprise">
                    <s:param name="enterpriseId">
                        <s:property value="enterpriseId" />
                    </s:param>
                </s:url> 

回答by lwpro2

actually, the scope and servletConfig interceptor can be utilized in struts2, to automatic pop the action context parameters, (request/session, etc)

实际上,struts2 中可以利用 scope 和 servletConfig 拦截器,自动弹出动作上下文参数,(请求/会话等)

回答by Sarathy

Actually you are going to pass your one action parameter value from one action to another action.

实际上,您要将一个动作参数值从一个动作传递到另一个动作。

simply include bean variable with same name. which parameter you are going to receive on action(receiver action).

只需包含具有相同名称的 bean 变量。您将在操作(接收器操作)中接收哪个参数。

<action name="ForwardAction" class="...">
       <result name="success" type="chain">ReceiverAction</result>
</action>

ForwardAction parameter will be forwarded to ReceiverAction. you can use it. but include same bean name in both actions.

ForwardAction 参数将转发给 ReceiverAction。你可以使用它。但在两个操作中都包含相同的 bean 名称。

if you are going to receive userid in receiveaction means.,

如果您要在接收操作中接收用户 ID,

This should be in both actions.,

这应该在两个动作中。,

private int userid;

public void setUserid(int id){
     this.userid = userid;
}

public int getUserid(){
     return userid;
}