Struts Actions

Is there a way to call an Action from another Action class?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

sbockelmanConnect With a Mentor Commented:
What IS it forwarding to, then?

Have you mapped the struts controller servlet to the *.do url pattern?

You may need to put a / in front of the path value, that is, try:

<forward name="TargetSelectAction" path="/"/>

I'm using Tiles, so my forwards are symbolic names, not real paths.  
My example was hastily copied/pasted, so I think maybe that would be the most likely issue.

well, you could just forward to it

FlavaflavAuthor Commented:
How can I forward it to another Action?
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

The execute method of the Action class in Struts returns an ActionForward where in the control can be forwarded (e.g. a JSP, Tile definition, Velocity template, or another Action). If the logical
mapping has been provided in the struts-config.xml file,you can use mapping.findForward(logicalname) or else you can also create a new ActionForward....
in struts-config, where you might normally have somethng like:

<action path="/EditProduct" type="com.cobite.exprts.chargeback.webapp.product.EditProductAction" name="ProductForm" scope="request" input="EditProduct">
      <forward name="view" path="EditProduct.jsp"/>
      <forward name="confirmation" path="ProductConfirmation.jsp"/>

you would just define the forward for another action using the url necessary:

<action path="/EditProduct" type="com.cobite.exprts.chargeback.webapp.product.EditProductAction" name="ProductForm" scope="request" input="EditProduct" >
      <forward name="view" path="" />
      <forward name="confirmation" path="" />

Later versions of struts will even have a formal mechanism for implementing the "chain of responsibility" pattern, though I haven;t looked into it yet.

Usually, when someone has this question, the BETTER solution is to refactor some common code into a method that both actions can call (i.e., maybe put it into a base Action class from which you then subclass other Actions).  What exactly are you trying to do?

FlavaflavAuthor Commented:
I have TargetSelect.jsp which displays a summary of all exisiing Target objects and its properties in a table. This JSP also has a dropdown containing Target obj's that the user can select to add, modify or delete. TargetSelect.jsp needs to be accessed from 2 actions: DisplayTargetSelectAction which basically gets the Target[] from the db, puts it in the request obj, and forwards to TargetSelect.jsp. The other action is AddModifyTargetAction (called from AddModifyTarget.jsp), which of course adds a new Target or modifies the selected Target in the db and then (knowing that I can do this now) forwards to the DisplayTargetSelectAction. Do you think there's a better way to do this? Would it make more sense to combine all functionality into one Action class (DisplayTargetSelectAction)?

Btw, I need to display a confirmation message in TargetSelect.jsp if a Target has been successfully added or modified. Currently I attach a "message" String to the request obj in AddModifyTargetAction and use <bean:write> to display it. Can and should I use ActionMessages instead?
FlavaflavAuthor Commented:
The forwarding to another Action isn't working. This is what I have in struts-config.xml:

<action path="/DeleteTarget" type="com.ds.udecide.server.reporting.web.DeleteTargetAction" name="TargetForm" scope="session" validate="false"> <forward name="TargetSelectAction" path=""/></action>

In DeleteTargetAction I have:

return mapping.findForward("TargetSelectAction");

but it's not calling DisplayTargetSelectPageAction.

What am I doing wrong?
All Courses

From novice to tech pro — start learning today.