Search code examples
strutsstruts-1

why does this code doesn't go into infinite loop?


1)

I'm looking at some code that is perhaps Struts1 and was wondering if someone can explain why I'm not getting an infinite loop, and instead, I'm being forwarded to a jsp page instead:

struts-config.xml:

<struts-config>
     <global-forwards>
        <forward name="a.t" path="/Search.do"/>
     </global-forwards>
     <action-mappings>
        <action path="/Search"
              type="path.SearchAction"
              scope="request"
              name="searchForm"
              validate="true">
        <forward name="orders" path="a.t"/>
        <forward name="success" path="a.t"/>
        <forward name="cancel" path="/Search.do"/>
        </action>
     </action-mappings>
    ...
 </struts-config>

I searched for a.t and found it's also referenced in this tiles.xml. Don't know what's the purpose of this.

<tiles-definitions>
   <definition name="a.t"  extends="admin.default">
     <put-attribute name="content" value="/mypath/hello.jsp"/>
   </definition>
</tiles-definitions>

Abridged SearchAction.java class:

public class SearchAction extends Action
(
   ....
   public ActionForward execute(ActionMapping mapping, ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response) throws
   Exception 
   {
       ....
       return mapping.findForward("success");
   }
)

My Original thinking is that since it always returns "success", and according to the struts-config.xml

<forward name="success" path="a.t"/>

it would go to find a.t, which is defined in the global-foward

<struts-config>
        <global-forwards>
           <forward name="a.t" path="/Search.do"/>
        </global-forwards>

and path="/Search.do"

would in theory send me back to SearchAction.java

Since

 <action-mappings>
       <action path="/Search"

points to SearchAction.java

2)

I don't know why the original author decided to do:

    <forward name="orders" path="a.t"/>
    <forward name="success" path="a.t"/>
    <forward name="cancel" path="/Search.do"/>

Is there a difference between

  <forward name="success" path="a.t"/>
vs.
  <forward name="success" path="/Search.do"/>

Solution

  • I'm being forwarded to a jsp page instead:

    I hope you are being forwarded to hello.jsp

    My Original thinking is that since it always returns "success", and according to the struts-config.xml

    <forward name="success" path="a.t"/>

    it would go to find a.t, which is defined in the global-foward

    <struts-config> <global-forwards> <forward name="a.t" path="/Search.do"/> </global-forwards>

    and path="/Search.do"

    The path="a.t" will not again search for mapping with same name in <global-forwards>. Instead it searches for the suitable mapping in tiles.xml and finds the following block and redirects to hello.jsp

    <tiles-definitions>
       <definition name="a.t"  extends="admin.default">
         <put-attribute name="content" value="/mypath/hello.jsp"/>
       </definition>
    </tiles-definitions>
    

    Is there a difference between

     <forward name="success" path="a.t"/>
    vs.
      <forward name="success" path="/Search.do"/>
    

    <forward name="success" path="a.t"/> will get mapped to tiles.xml and forward to hello.jsp

    <forward name="success" path="/Search.do"/> will get mapped to SearchAction.java. Since you have only returned success, it will again go to hello.jsp, but the original author has done so, because in future he can add any other mapping also, for different cases say, failure

    Action class:

    return mapping.findForward("failure");

    struts-config.xml:

    <forward name="failure" path="failure.jsp"/>