View Javadoc

1   /*
2    * $Id: MultiRegistrationAction.java 421488 2006-07-13 03:43:08Z wsmoak $
3    *
4    * Copyright 2000-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.struts.webapp.validator;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  import javax.servlet.http.HttpSession;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.struts.action.Action;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  import org.apache.struts.action.ActionMessages;
32  
33  /***
34   * Implementation of <strong>Action</strong> that validates a multi-page
35   * registration form.
36   *
37   */
38  public final class MultiRegistrationAction extends Action {
39  
40      /***
41       * Commons Logging instance.
42       */
43      private Log log = LogFactory.getFactory().getInstance(this.getClass().getName());
44  
45      /***
46       * Process the specified HTTP request, and create the corresponding HTTP
47       * response (or forward to another web component that will create it).
48       * Return an <code>ActionForward</code> instance describing where and how
49       * control should be forwarded, or <code>null</code> if the response has
50       * already been completed.
51       *
52       * @param mapping The ActionMapping used to select this instance
53       * @param form The optional ActionForm bean for this request (if any)
54       * @param request The HTTP request we are processing
55       * @param response The HTTP response we are creating
56       *
57       * @exception Exception if an input/output error or servlet exception occurs
58       */
59      public ActionForward execute(
60          ActionMapping mapping,
61          ActionForm form,
62          HttpServletRequest request,
63          HttpServletResponse response)
64          throws Exception {
65  
66          // Extract attributes we will need
67          RegistrationForm info = (RegistrationForm) form;
68  
69  
70          // Was this transaction cancelled?
71          if (isCancelled(request)) {
72              if (log.isInfoEnabled()) {
73                  log.info(
74                      " "
75                          + mapping.getAttribute()
76                          + " - Registration transaction was cancelled");
77              }
78  
79              removeFormBean(mapping, request);
80  
81              return mapping.findForward("success");
82          }
83  
84          ActionMessages errors = info.validate(mapping, request);
85  
86          if (errors != null && errors.isEmpty()) {
87              if (info.getPage() == 1)
88                  return mapping.findForward("input2");
89  
90              if (info.getPage() == 2)
91                  return mapping.findForward("success");
92  
93          } else {
94              this.saveErrors(request, errors);
95  
96              if (info.getPage() == 1){
97                  return mapping.findForward("input" + info.getPage());
98              }
99  
100             if (info.getPage() == 2){
101                 return mapping.findForward("input" + info.getPage());
102             }
103         }
104 
105         return mapping.findForward("input1");
106     }
107 
108     /***
109      * Convenience method for removing the obsolete form bean.
110      *
111      * @param mapping The ActionMapping used to select this instance
112      * @param request The HTTP request we are processing
113      */
114     protected void removeFormBean(
115         ActionMapping mapping,
116         HttpServletRequest request) {
117 
118         // Remove the obsolete form bean
119         if (mapping.getAttribute() != null) {
120             if ("request".equals(mapping.getScope())) {
121                 request.removeAttribute(mapping.getAttribute());
122             } else {
123                 HttpSession session = request.getSession();
124                 session.removeAttribute(mapping.getAttribute());
125             }
126         }
127     }
128 }