ProcessLocalizationAction xref

View Javadoc

    1   /*
    2    * $Id: ProcessLocalizationAction.java 471754 2006-11-06 14:55:09Z husted $
    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 examples.localization;
    23
    24  import java.util.Locale;
    25
    26  import javax.servlet.http.HttpServletRequest;
    27  import javax.servlet.http.HttpServletResponse;
    28  import javax.servlet.http.HttpSession;
    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
    36  /**
    37   * Retrieve and process data from the submitted form
    38   *
    39   * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
    40   */
    41  public class ProcessLocalizationAction extends Action {
    42
    43      // ------------------------------------------------------------ Constructors
    44
    45      /**
    46       * Constructor for ProcessOptionsAction.
    47       */
    48      public ProcessLocalizationAction() {
    49          super();
    50      }
    51
    52      // ---------------------------------------------------------- Action Methods
    53
    54      /**
    55       * Process the request and return an <code>ActionForward</code> instance
    56       * describing where and how control should be forwarded, or
    57       * <code>null</code>if the response has already been completed.
    58       *
    59       * @param mapping The ActionMapping used to select this instance
    60       * @param form The optional ActionForm bean for this request (if any)
    61       * @param request The HTTP request we are processing
    62       * @param response The HTTP response we are creating
    63       *
    64       * @exception Exception if the application logic throws an exception
    65       *
    66       * @return the ActionForward for the next view
    67       */
    68      public ActionForward execute(
    69          ActionMapping mapping,
    70          ActionForm form,
    71          HttpServletRequest request,
    72          HttpServletResponse response)
    73          throws Exception {
    74
    75          // Extract attributes we will need
    76          HttpSession session = request.getSession();
    77
    78          // Get locale from request, if any
    79          Locale locale = request.getLocale();
    80
    81          // If supplied, set Locale based on request parameters;
    82          // country and language
    83          String language = request.getParameter("language");
    84          String country = request.getParameter("country");
    85
    86          if ((language != null && language.length() > 0)
    87              && (country != null && country.length() > 0)) {
    88              locale = new java.util.Locale(language, country);
    89          } else if (language != null && language.length() > 0) {
    90              locale = new java.util.Locale(language, "");
    91          }
    92
    93          //Save locale
    94          session.setAttribute(Globals.LOCALE_KEY, locale);
    95
    96          // Forward to result page
    97          return mapping.findForward("success");
    98      }
    99
    100 }