craigmcc    02/03/20 17:42:42

  Modified:    src/share/org/apache/struts/actions LocalStrings.properties
  Added:       src/share/org/apache/struts/actions SwitchAction.java
  Log:
  Add a standard action that makes it easy to switch between sub-applications
  (surprisingly hard to do with, because the selected ApplicationConfig and
  MessageResources are normally based solely on the original request URI).  Now,
  assuming you have mapped this to path "/switch", you can set up a hyperlink
  that goes to the "/menu.do" action of the sub-application at prefix "/bar":
  
   <html:link page="/switch.do?prefix=/bar&amp;page=/menu.do">...</html:link>
  
  Revision  Changes    Path
  1.3       +2 -1      
jakarta-struts/src/share/org/apache/struts/actions/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/actions/LocalStrings.properties,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- LocalStrings.properties   10 May 2001 03:13:33 -0000      1.2
  +++ LocalStrings.properties   21 Mar 2002 01:42:42 -0000      1.3
  @@ -8,4 +8,5 @@
   forward.rd=Cannot create request dispatcher for path {0}
   include.path=No context-relative URI specified via the 'parameter' attribute
   include.rd=Cannot create request dispatcher for path {0}
  -
  +switch.prefix=Invalid sub-application prefix {0} was specified
  +switch.required=Switch requires both 'prefix' and 'path' request parameters
  
  
  
  1.1                  
jakarta-struts/src/share/org/apache/struts/actions/SwitchAction.java
  
  Index: SwitchAction.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/actions/SwitchAction.java,v 1.1 
2002/03/21 01:42:42 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/03/21 01:42:42 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.struts.actions;
  
  
  import java.io.IOException;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.struts.action.Action;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  import org.apache.struts.config.ApplicationConfig;
  import org.apache.struts.util.MessageResources;
  
  
  /**
   * <p>A standard <strong>Action</strong> that switches to a new sub-application
   * and then forards control to a URI (specified in a number of possible ways)
   * within the new sub-application.</p>
   *
   * <p>Valid request parameters for this Action are:</p>
   * <ul>
   * <li><strong>page</strong> - Application-relative URI (beginning with "/")
   *     to which control should be forwarded after switching.</li>
   * <li><strong>prefix</strong> - The application prefix (beginning with "/")
   *     of the sub-application to which control should be switched.  Use a
   *     zero-length string for the default sub-application.  The
   *     appropriate <code>ApplicationConfig</code> object will be stored as a
   *     request attribute, so any subsequent logic will assume the new
   *     sub-application.</li>
   * </ul>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/03/21 01:42:42 $
   * @since 1.1
   */
  
  public class SwitchAction extends Action {
  
  
      // ----------------------------------------------------- Instance Variables
  
  
      /**
       * Commons Logging instance.
       */
      protected Log log = LogFactory.getLog(this.getClass());
  
  
      /**
       * The message resources for this package.
       */
      protected static MessageResources messages =
          MessageResources.getMessageResources
          ("org.apache.struts.actions.LocalStrings");
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Process the specified HTTP request, and create the corresponding HTTP
       * response (or forward to another web component that will create it),
       * with provision for handling exceptions thrown by the business logic.
       *
       * @param mapping The ActionMapping used to select this instance
       * @param form The optional ActionForm bean for this request (if any)
       * @param request The HTTP request we are processing
       * @param response The HTTP response we are creating
       *
       * @exception Exception if the application business logic throws
       *  an exception
       */
      public ActionForward execute(ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response)
          throws Exception {
  
          // Identify the request parameters controlling our actions
          String page = request.getParameter("page");
          String prefix = request.getParameter("prefix");
          if ((page == null) || (prefix == null)) {
              String message = messages.getMessage("switch.required");
              log.error(message);
              response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                                 message);
              return (null);
          }
  
          // Switch to the requested sub-application
          ApplicationConfig config = (ApplicationConfig)
              getServlet().getServletContext().getAttribute
              (Action.APPLICATION_KEY + prefix);
          if (config == null) {
              String message = messages.getMessage("switch.prefix", prefix);
              log.error(message);
              response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                                 message);
              return (null);
          }
          request.setAttribute(Action.APPLICATION_KEY + prefix, config);
          MessageResources resources = (MessageResources)
              getServlet().getServletContext().getAttribute
              (Action.MESSAGES_KEY + prefix);
          if (resources != null) {
              request.setAttribute(Action.MESSAGES_KEY + prefix, resources);
          } else {
              request.removeAttribute(Action.MESSAGES_KEY + prefix);
          }
  
          // Forward control to the specified application-relative URI
          return (new ActionForward(page));
  
      }
  
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to