java 在 Struts 2 中使用带有动态参数的 ActionForward

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

Using ActionForward with dynamic params in Struts 2

javastruts2strutsactionresult

提问by john

While migrating the application from Struts 1 to Struts 2

将应用程序从 Struts 1 迁移到 Struts 2 时

In some of the places, the same action class has been used for different type of views, based on the request params.

在某些地方,基于请求参数,相同的动作类已用于不同类型的视图。

For Example: if the createTypeis 1 means need to append one param or if the createTypeis 2 means need to append some more extra params, like that I need to pass dynamic params to some other action using ActionForward.

例如:如果createTypeis 1 意味着需要附加一个参数,或者如果createTypeis 2 意味着需要附加一些额外的参数,就像我需要使用ActionForward.

struts-config.xml

struts-config.xml

<action path="/CommonAction" type="com.example.CommonAction" scope="request">
    <forward name="viewAction" path = "/ViewAction.do"/>
</action>

Action Class

动作类

public class CreateAction extends Action
{
    public ActionForward execute(ActionMapping m, ActionForm f, HttpServletRequest req, HttpServletResponse res) throws ServletException, Exception
    {
            String actionPath = m.findForward("viewAction").getPath();
            String createType = req.getParameter("createType");
            String params = "&action=view";
            if("1".equals(createType)){
               params = params + "&from=list";
            }else if("2".equals(createType)){
               params = params + "&from=detail&someParam=someValue";
            }//,etc..
            String actionUrl = actionPath+"?"+params;
            return new ActionForward(actionUrl);
    }
}

But, I not able to do the same thing in Struts 2. Is there any possibilities to use ActionForwardwith dynamic params in Struts 2 ?

但是,我无法在 Struts 2 中做同样的事情。是否有可能ActionForward在 Struts 2 中使用动态参数?

回答by Roman C

You could use a dynamic parameters with a result, see the dynamic result configuration.

您可以使用带有 的动态参数result,请参阅动态结果配置

In the action you should write a getter for the patrameter

在操作中,您应该为参数编写一个 getter

private String actionUrl;

public String getActionUrl() {
    return actionUrl;
}

and configure result

并配置结果

<action name="create" class="CreateAction">
    <result type="redirect">${actionUrl}</result>
</action>

So, the common sense would be rewrite the code like

所以,常识是重写代码,如

public class CreateAction extends ActionSupport
{

    private String actionUrl;

    public String getActionUrl() {
        return actionUrl;
    }

    @Override
    public String execute() throws Exception
    {
            String actionPath = "/view";
            String createType = req.getParameter("createType");
            String params = "&action=view";
            if("1".equals(createType)){
               params = params + "&from=list";
            }else if("2".equals(createType)){
               params = params + "&from=detail&someParam=someValue";
            }//,etc..
            actionUrl = actionPath+"?"+params;
            return SUCCESS;
    }
}

If you need a better way to create the urls from action mapping, you could look at thisanswer.

如果您需要更好的方法来从动作映射创建 url,您可以查看答案。