Save This Page
Home » struts-2.1.8.1-src » org.apache » struts2 » s1 » [javadoc | source]
    1   /*
    2    * $Id: Struts1Action.java 651946 2008-04-27 13:41:38Z apetrelli $
    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.struts2.s1;
   23   
   24   import java.util.Arrays;
   25   import java.util.Iterator;
   26   
   27   import javax.servlet.http.HttpServletRequest;
   28   import javax.servlet.http.HttpServletResponse;
   29   
   30   import org.apache.struts.Globals;
   31   import org.apache.struts.action.Action;
   32   import org.apache.struts.action.ActionForm;
   33   import org.apache.struts.action.ActionForward;
   34   import org.apache.struts.action.ActionMapping;
   35   import org.apache.struts.action.ActionMessage;
   36   import org.apache.struts.action.ActionMessages;
   37   import org.apache.struts2.ServletActionContext;
   38   import org.apache.struts2.StrutsException;
   39   import org.apache.struts2.dispatcher.DefaultActionSupport;
   40   import org.apache.struts2.dispatcher.Dispatcher;
   41   
   42   import com.opensymphony.xwork2.ActionContext;
   43   import com.opensymphony.xwork2.ObjectFactory;
   44   import com.opensymphony.xwork2.config.Configuration;
   45   import com.opensymphony.xwork2.config.entities.ActionConfig;
   46   import com.opensymphony.xwork2.inject.Inject;
   47   import com.opensymphony.xwork2.interceptor.ScopedModelDriven;
   48   
   49   /**
   50    * Wraps legacy Struts 1.3 Actions.  Supports the following features:
   51    * <ul>
   52    *  <li>ActionForms</li>
   53    *  <li>ActionForwards that have the same name as a result</li>
   54    *  <li>ActionMessages stored in the request, converted to Struts 2 messages</li>
   55    *  <li>Action-level validation flag</li>
   56    * </ul>
   57    * Still to do:
   58    * <ul>
   59    *  <li>Custom ActionForward instances that don't have an associated result config</li>
   60    *  <li>setServlet() calls for the Action</li>
   61    *  <li>Most everything else...</li>
   62    * </ul>
   63    */
   64   public class Struts1Action extends DefaultActionSupport implements ScopedModelDriven<ActionForm> {
   65   
   66       private ActionForm actionForm;
   67       private String className;
   68       private boolean validate;
   69       private String scopeKey;
   70       private ObjectFactory objectFactory;
   71       private Configuration configuration;
   72       
   73       @Inject
   74       public void setObjectFactory(ObjectFactory fac) {
   75           this.objectFactory = fac;
   76       }
   77       
   78       @Inject
   79       public void setConfiguration(Configuration config) {
   80           this.configuration = config;
   81       }
   82       
   83       public String execute() throws Exception {
   84           ActionContext ctx = ActionContext.getContext();
   85           ActionConfig actionConfig = ctx.getActionInvocation().getProxy().getConfig();
   86           Action action = null;
   87           try {
   88               action = (Action) objectFactory.buildBean(className, null);
   89           } catch (Exception e) {
   90               throw new StrutsException("Unable to create the legacy Struts Action", e, actionConfig);
   91           }
   92           
   93           // We should call setServlet() here, but let's stub that out later
   94           
   95           Struts1Factory strutsFactory = new Struts1Factory(Dispatcher.getInstance().getConfigurationManager().getConfiguration());
   96           ActionMapping mapping = strutsFactory.createActionMapping(actionConfig);
   97           HttpServletRequest request = ServletActionContext.getRequest();
   98           HttpServletResponse response = ServletActionContext.getResponse();
   99           ActionForward forward = action.execute(mapping, actionForm, request, response);
  100           
  101           ActionMessages messages = (ActionMessages) request.getAttribute(Globals.MESSAGE_KEY);
  102           if (messages != null) {
  103               for (Iterator i = messages.get(); i.hasNext(); ) {
  104                   ActionMessage msg = (ActionMessage) i.next();
  105                   if (msg.getValues() != null && msg.getValues().length > 0) {
  106                       addActionMessage(getText(msg.getKey(), Arrays.asList(msg.getValues())));
  107                   } else {
  108                       addActionMessage(getText(msg.getKey()));
  109                   }
  110               }
  111           }
  112           
  113           if (forward instanceof WrapperActionForward || actionConfig.getResults().containsKey(forward.getName())) {
  114               return forward.getName();
  115           } else {
  116               throw new StrutsException("Unable to handle action forwards that don't have an associated result", actionConfig);
  117           }
  118       }
  119       
  120       public void setModel(ActionForm model) {
  121           actionForm = model;
  122       }
  123   
  124       public ActionForm getModel() {
  125           return actionForm;
  126       }
  127       
  128       /**
  129        * @return the validate
  130        */
  131       public boolean isValidate() {
  132           return validate;
  133       }
  134   
  135       /**
  136        * @param validate the validate to set
  137        */
  138       public void setValidate(boolean validate) {
  139           this.validate = validate;
  140       }
  141   
  142       /**
  143        * @param className the className to set
  144        */
  145       public void setClassName(String className) {
  146           this.className = className;
  147       }
  148   
  149       public String getScopeKey() {
  150           return scopeKey;
  151       }
  152   
  153       public void setScopeKey(String key) {
  154           this.scopeKey = key;
  155       }
  156   }

Save This Page
Home » struts-2.1.8.1-src » org.apache » struts2 » s1 » [javadoc | source]