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