tags:

views:

296

answers:

1

I'm having trouble calling a method in one Struts action from a method in another Struts Action (I've been told that this is possible).

I'm working with two Struts DynaValidatorForms - one is used to create an entry, the other is used purely for viewing the action (plus editing, deleting etc).

I have two seperate Struts Action files: CreateAction.java (which has the create() method) and ViewAndEditAction.java (which contains the view() and edit() methods).

What I would like to do is to invoke the view() method in the latter action after the create() method in the former action has completed.

I've looked into Action Chaining but haven't been able to find a solution to my problem.

A: 

You need to create a forward to the next action in the first action. Then return the appropriate forward. In the config below, if you return "success" in the Create action then it goes to the view method of the next action.

<action
path="/create"
type="package.CreateAction"
name="YourForm"
scope="request"
validate="false">
<forward
    name="failure"
    path="/mainMenu.jsp"/>
<forward
    name="success"
    path="/view.do" redirect="false"/>
</action>

<action
path="/view"
type="package.ViewAndEditAction"
name="YourForm"
scope="request"
validate="false">
<forward
    name="failure"
    path="/mainMenu.jsp"/>
<forward
    name="success"
    path="/view.jsp"/>
</action>
Vincent Ramdhanie
Thanks for getting back to me so soon Vincent.This is what I've been trying to do but it has proven unsuccessful - how does Struts know which method to hit in the ViewAndEditAction after the forward?Also is it okay that I use a different DynaValidatorForm between the two actions?Thanks.
Gearóid
I suspect that using a different form between the actions may be problematic and that is where you are getting yuour problem. You can specify the method name in the action configuration though so that should solve the other problem.
Vincent Ramdhanie
Ah I see, so my forward will be something like "/view.do?method=getRecord".Thanks a lot Vincent.
Gearóid