Save This Page
Home » struts-1.3.9-src » examples » app1 » [javadoc | source]
    1   /*
    2    * Copyright 2003 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
   10    * Unless required by applicable law or agreed to in writing, software
   11    * distributed under the License is distributed on an "AS IS" BASIS,
   12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13    * See the License for the specific language governing permissions and
   14    * limitations under the License.
   15    */
   16   
   17   package examples.app1;
   18   
   19   
   20   import java.io.IOException;
   21   import java.util.Locale;
   22   import javax.servlet.ServletException;
   23   import javax.servlet.http.HttpServletRequest;
   24   import javax.servlet.http.HttpSession;
   25   import javax.servlet.http.HttpServletResponse;
   26   import org.apache.struts.Globals;
   27   import org.apache.struts.action.Action;
   28   import org.apache.struts.action.ActionErrors;
   29   import org.apache.struts.action.ActionForm;
   30   import org.apache.struts.action.ActionForward;
   31   import org.apache.struts.action.ActionMapping;
   32   
   33   
   34   /**
   35    * <p>A simple action that handles the display and editing of an
   36    * addres record. This action works with both JSP and Velocity templates.
   37    * The type of template to be used is defined in the Struts configuration
   38    * file.</p>
   39    *
   40    * <p>The action support an <i>action</i> URL parameter. This URL parameter
   41    * controls what this action class does. The following values are supported:</p>
   42    * <ul>
   43    *   <li>list - list address record, this is the default if no action parameter is specified
   44    *   <li>edit - edit address record
   45    *   <li>save - save address record
   46    * </ul>
   47    *
   48    *
   49    * @author <a href="mailto:sidler@teamup.com"/>Gabe Sidler</a>
   50    * @version $Id: AddressAction.java 72026 2004-03-12 19:41:11Z marino $
   51    */
   52   public class AddressAction extends Action
   53   {
   54   
   55       // --------------------------------------------------------- Public Methods
   56   
   57       /**
   58        * Handle server requests.
   59        *
   60        * @param mapping The ActionMapping used to select this instance
   61        * @param actionForm The optional ActionForm bean for this request (if any)
   62        * @param request The HTTP request we are processing
   63        * @param response The HTTP response we are creating
   64        *
   65        * @exception IOException if an input/output error occurs
   66        * @exception ServletException if a servlet exception occurs
   67        */
   68       public ActionForward execute(ActionMapping mapping,
   69                                    ActionForm form,
   70                                    HttpServletRequest request,
   71                                    HttpServletResponse response)
   72                                    throws IOException, ServletException
   73       {
   74           String action;
   75           HttpSession session;
   76   
   77           ActionErrors errors = new ActionErrors();
   78   
   79           try
   80           {
   81               session = request.getSession();
   82   
   83               // fetch action from form
   84               action = ((AddressForm)form).getAction();
   85   
   86               servlet.log("[DEBUG] AddressAction at perform(): Action ist " + action);
   87   
   88               // Determine what to do
   89               if ( action.equals("edit") )
   90               {
   91                   // forward to edit formular
   92                   return (mapping.findForward("editAddress"));
   93   
   94               }
   95               else if (action.equals("save"))
   96               {
   97                   // check if an address bean exits already
   98                   AddressBean bean = (AddressBean)session.getAttribute("address");
   99   
  100                   if (bean == null)
  101                   {
  102                       bean = new AddressBean();
  103                       session.setAttribute("address", bean);
  104                   }
  105   
  106                   // update bean with the new values submitted
  107                   bean.setFirstname( ((AddressForm)form).getFirstname() );
  108                   bean.setLastname( ((AddressForm)form).getLastname() );
  109                   bean.setStreet( ((AddressForm)form).getStreet() );
  110                   bean.setZip( ((AddressForm)form).getZip() );
  111                   bean.setCity( ((AddressForm)form).getCity() );
  112                   bean.setCountry( ((AddressForm)form).getCountry() );
  113                   bean.setLanguages( ((AddressForm)form).getLanguages() );
  114   
  115                   // forward to list
  116                   return (mapping.findForward("showAddress"));
  117   
  118               }
  119               else
  120               {
  121                   String locale = ((AddressForm)form).getLocale();
  122                   if (locale.equals("Deutsch"))
  123                       session.setAttribute(Globals.LOCALE_KEY, new Locale("de", ""));
  124                   else
  125                       session.setAttribute(Globals.LOCALE_KEY, new Locale("en", ""));
  126   
  127                   // forward to edit formular
  128                   return (mapping.findForward("showAddress"));
  129               }
  130           }
  131           catch (Exception e)
  132           {
  133               //errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("lo053"));
  134               servlet.log("[ERROR] TskAction at final catch: " + e.getMessage());
  135               e.printStackTrace();
  136   
  137           }
  138   
  139           // Default if everthing else fails
  140           return (mapping.findForward("showAddress"));
  141   
  142       }
  143   }
  144   

Save This Page
Home » struts-1.3.9-src » examples » app1 » [javadoc | source]