4

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 ?

Roman C
  • 49,761
  • 33
  • 66
  • 176
john
  • 859
  • 2
  • 12
  • 25

1 Answers1

5

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.

Marco Borchert
  • 1,038
  • 1
  • 11
  • 17
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • In Struts 1, It won't redirect unless we use redirect param as true [ new ActionForward(path,redirect) ]. But in Struts 2 as you suggested its always redirecting as a new Action. How to avoid this in Struts 2 ? – john Jul 26 '13 at 15:18
  • Redirect is a normal deal with the action. Are you sure you want to forward to the action? – Roman C Jul 26 '13 at 15:47
  • A new Action object is always created upon request, do you want to avoid it? – Roman C Jul 26 '13 at 15:54
  • I don't want to get as a new request from browser, simply I want internal action forward. – john Jul 29 '13 at 07:37
  • 1
    Then you should configure struts filter to accept forward requests from dispatcher. – Roman C Jul 29 '13 at 08:03
  • Do you have any reference for that. – john Jul 29 '13 at 08:19
  • [This example is for sitmesh](http://struts.apache.org/release/2.3.x/docs//sitemesh-plugin.html#SiteMeshPlugin-FullintegrationwithSiteMesh2Freemarker2.4.2Velocity1.3,includingStruts2Tags,ValueStack,andFreemarkerManagerstatics.). Or look at [this](http://stackoverflow.com/questions/16119925/how-to-forward-request-from-servlet-to-action-of-struts2) question and answers. – Roman C Jul 29 '13 at 08:45