1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
67 RegistrationForm info = (RegistrationForm) form;
68
69
70
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
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 }