Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

java - How to change ActionForward with dynamic params in Struts 2

While migrating the application from Struts 1 to 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 createType is 1 means need to append one param or if the createType is 2 means need to append some more extra params, like that I need to pass dynamic params to some other action using ActionForward.

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 change ActionForward with dynamic params in Struts 2 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

In the action you should write a getter for the patrameter

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 this answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...