javastrutsstruts-1actionform

How to programmatically set the FormBean of the next Action to be executed?


I wish to forward an action to the next configured action but prior to that to create a new ActionForm instance and have it associated with the forwarded action:

Psuedo code follows :

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

    ActionForward forwardPage = mapping.findForward("success");


    // Some Code....
    ....
    ....

    // finally
    MyActionForm form = new MyActionForm();

    form.setSomeProp(...);
    form.setAnotherProp(...);

    forwardPage.doSomeMagicAndAssociateWith(form);

    return forwardPage;

}

Sorry if its a dumb question, I'm new to all this and have not been able to find an answer...

Thanks

EDIT 1:

Just to clarify: ActionForwarded to "success" had existed prior to the new feature that I must now add.

I just wish to, in certain flows, come in before the initial flow (here called "success") and do some computations after which in case of success [ :-) ] to forward the flow to the "success" action.

Hope my point is clearer now.


Solution

  • I have managed on my own. Maybe my solution will explain what I am not sure I had been able to in my question...

    Say this is my struts-config.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
    <struts-config>
        <data-sources>
        </data-sources>
        <!-- ================== Form Beans ================ -->
        <form-beans>
            <form-bean name="FirstForm"
                type="com.blahblah.forms.FirstForm" />  
            <form-bean name="SecondForm"
                type="com.blahblah.forms.SecondForm" />
    
                <form-bean name="NewForm"
                type="com.blahblah.forms.NewForm" />
        </form-beans>   
        <!-- ================== Action Mapping Definitions ================ -->
        <action-mappings>       
            <action path="/pages/First" name="FirstForm"
                type="org.springframework.web.struts.DelegatingActionProxy" scope="request">
                <forward name="success" path="/pages/Second.do" />
            </action>
            <action path="/pages/Second" name="SecondForm"
                type="org.springframework.web.struts.DelegatingActionProxy" scope="request">
                <forward name="doSomthing" path="/pages/DoSomthing.jsp" />
            </action>
    
            <action path="/pages/New" name="NewForm"
                type="org.springframework.web.struts.DelegatingActionProxy" scope="request">
                <forward name="success" path="/pages/First.do" />
            </action>
        </action-mappings>
    </struts-config>
    

    FirstForm, FirstAction, SecondForm and SecondAction had existed previous to my feature and I had NO wish to intervene within their code...

    The NewAction's execute() does the following:

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    
        ActionForward forwardPage = mapping.findForward("success");
    
        // do some coding
    
        FirstForm firstForm = new FirstForm();
        firstForm.setX(...);
        firstForm.setY(...);
        request.setAttribute("FirstForm", firstForm);
    
        return forwardPage;
    }