001: /*
002: * $Id: MultiRegistrationAction.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.webapp.validator;
023:
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026: import javax.servlet.http.HttpSession;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.struts.action.Action;
031: import org.apache.struts.action.ActionForm;
032: import org.apache.struts.action.ActionForward;
033: import org.apache.struts.action.ActionMapping;
034: import org.apache.struts.action.ActionMessages;
035:
036: /**
037: * Implementation of <strong>Action</strong> that validates a multi-page
038: * registration form.
039: *
040: */
041: public final class MultiRegistrationAction extends Action {
042:
043: /**
044: * Commons Logging instance.
045: */
046: private Log log = LogFactory.getFactory().getInstance(
047: this .getClass().getName());
048:
049: /**
050: * Process the specified HTTP request, and create the corresponding HTTP
051: * response (or forward to another web component that will create it).
052: * Return an <code>ActionForward</code> instance describing where and how
053: * control should be forwarded, or <code>null</code> if the response has
054: * already been completed.
055: *
056: * @param mapping The ActionMapping used to select this instance
057: * @param form The optional ActionForm bean for this request (if any)
058: * @param request The HTTP request we are processing
059: * @param response The HTTP response we are creating
060: *
061: * @exception Exception if an input/output error or servlet exception occurs
062: */
063: public ActionForward execute(ActionMapping mapping,
064: ActionForm form, HttpServletRequest request,
065: HttpServletResponse response) throws Exception {
066:
067: // Extract attributes we will need
068: RegistrationForm info = (RegistrationForm) form;
069:
070: // Was this transaction cancelled?
071: if (isCancelled(request)) {
072: if (log.isInfoEnabled()) {
073: log.info(" " + mapping.getAttribute()
074: + " - Registration transaction was cancelled");
075: }
076:
077: removeFormBean(mapping, request);
078:
079: return mapping.findForward("success");
080: }
081:
082: ActionMessages errors = info.validate(mapping, request);
083:
084: if (errors != null && errors.isEmpty()) {
085: if (info.getPage() == 1)
086: return mapping.findForward("input2");
087:
088: if (info.getPage() == 2)
089: return mapping.findForward("success");
090:
091: } else {
092: this .saveErrors(request, errors);
093:
094: if (info.getPage() == 1) {
095: return mapping.findForward("input" + info.getPage());
096: }
097:
098: if (info.getPage() == 2) {
099: return mapping.findForward("input" + info.getPage());
100: }
101: }
102:
103: return mapping.findForward("input1");
104: }
105:
106: /**
107: * Convenience method for removing the obsolete form bean.
108: *
109: * @param mapping The ActionMapping used to select this instance
110: * @param request The HTTP request we are processing
111: */
112: protected void removeFormBean(ActionMapping mapping,
113: HttpServletRequest request) {
114:
115: // Remove the obsolete form bean
116: if (mapping.getAttribute() != null) {
117: if ("request".equals(mapping.getScope())) {
118: request.removeAttribute(mapping.getAttribute());
119: } else {
120: HttpSession session = request.getSession();
121: session.removeAttribute(mapping.getAttribute());
122: }
123: }
124: }
125: }
|