View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2Toolkit library.
13   *
14   *  The Initial Owner of the Original Code is Power Of Two S.R.L.
15   *  Portions created by Power Of Two S.R.L. are Copyright (C) Power Of Two S.R.L.
16   *  All Rights Reserved.
17   *
18   * Contributor(s):
19   */
20  package com.pow2.struts.action;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.struts.action.ActionErrors;
26  import org.apache.struts.action.ActionForm;
27  import org.apache.struts.action.ActionForward;
28  import org.apache.struts.action.ActionMapping;
29  
30  
31  /***
32   *  Generic dispatcher Action class
33   *  <br>
34   *  The target resource can be specified by:
35   *  <br>
36   *
37   *  <ul>
38   *    <li>
39   *      the <code>parameter</code> attribute of the action element
40   *      of the struts configuration file
41   *    </li>
42   *    <li>
43   *      the <code>fwd</code> request attribute; the <code>fwd</code>
44   *      attribute value specifies the forward name of the ActionForward object to
45   *      retrieve.
46   *      <br>
47   *      Action URL Example: <code>myDispatcher.do?fwd=showUserData</code>
48   *    </li>
49   *  </ul>
50   *
51   * @author     Ted Husted
52   * @author     Luca Fossato
53   * @version    $Revision: 1.1.1.1 $ $Date: 2004/08/31 20:22:46 $
54   */
55  public class DispatcherAction extends BaseAction
56  {
57    /***
58     *  Dispatch the client requests.
59     *
60     * @param  mapping    the ActionMapping used to select this instance
61     * @param  request    the HTTP request we are processing
62     * @param  response   the HTTP response we are creating
63     * @param  form       the ActionForm associated with this action (if any)
64     * @return            the ActionForward object
65     * @exception  java.io.IOException
66     * @exception  javax.servlet.ServletException
67     */
68    public ActionForward execute(ActionMapping mapping,
69                                 ActionForm form,
70                                 HttpServletRequest request,
71                                 HttpServletResponse response)
72         throws
73        java.io.IOException,
74        javax.servlet.ServletException
75    {
76      ActionErrors  errors        = new ActionErrors();
77      ActionForward actionForward = null;
78      String        forwardParam  = null;
79  
80      // isCancelled() returns true if the current form's cancel button was pressed.
81      // This method will check if the cancel button generated by CancelTag was pressed
82      // by the user in the current request.
83      // If true, validation performed by an ActionForm validate() method will
84      // have been skipped by the controller servlet.
85      if (isCancelled(request))
86      {
87        // reset the form associated with this action;
88        if (form != null)
89          form.reset(mapping, request);
90  
91        ActionForward cancelFwd = null;
92  
93        // does cancel forward exist ?
94        if ((cancelFwd = mapping.findForward(FWD_CANCEL)) != null)
95          return cancelFwd;
96  
97        cat.warn("::perform - the current form's cancel button was pressed but the system cannot retrieve the " + FWD_CANCEL + " forward");
98      }
99  
100     // this action uses the action parameter value as the forward key
101     // for mapping.findForward() API;
102     // See the action parameter attribute:
103     // <action  parameter="myParam" ... />
104     forwardParam = mapping.getParameter();
105 
106     // the action parameter is not specified... so try to inspect
107     // the ${fwd} request parameter;
108     if (forwardParam == null)
109       forwardParam = request.getParameter("fwd");
110 
111     // get the ActionForward object;
112     if (forwardParam != null)
113       actionForward = mapping.findForward(forwardParam);
114 
115     // debug !
116     if (actionForward == null)
117       cat.warn("::perform - cannot retrieve a valid ActionForward object using the forward parameter [" + forwardParam + "]; returning NULL");
118 
119     return actionForward;
120   }
121 }