View Javadoc

1   /*
2    * $Id: ProcessLocalizationAction.java 421486 2006-07-13 03:37:08Z wsmoak $
3    *
4    * Copyright 2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package examples.localization;
20  
21  import java.util.Locale;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import javax.servlet.http.HttpSession;
26  
27  import org.apache.struts.Globals;
28  import org.apache.struts.action.Action;
29  import org.apache.struts.action.ActionForm;
30  import org.apache.struts.action.ActionForward;
31  import org.apache.struts.action.ActionMapping;
32  
33  /***
34   * Retrieve and process data from the submitted form
35   *
36   * @version $Rev: 421486 $ $Date: 2006-07-12 20:37:08 -0700 (Wed, 12 Jul 2006) $
37   */
38  public class ProcessLocalizationAction extends Action {
39  
40      // ------------------------------------------------------------ Constructors
41  
42      /***
43       * Constructor for ProcessOptionsAction.
44       */
45      public ProcessLocalizationAction() {
46          super();
47      }
48  
49      // ---------------------------------------------------------- Action Methods
50  
51      /***
52       * Process the request and return an <code>ActionForward</code> instance
53       * describing where and how control should be forwarded, or
54       * <code>null</code>if the response has already been completed.
55       *
56       * @param mapping The ActionMapping used to select this instance
57       * @param form The optional ActionForm bean for this request (if any)
58       * @param request The HTTP request we are processing
59       * @param response The HTTP response we are creating
60       *
61       * @exception Exception if the application logic throws an exception
62       *
63       * @return the ActionForward for the next view
64       */
65      public ActionForward execute(
66          ActionMapping mapping,
67          ActionForm form,
68          HttpServletRequest request,
69          HttpServletResponse response)
70          throws Exception {
71  
72          // Extract attributes we will need
73          HttpSession session = request.getSession();
74  
75          // Get locale from request, if any
76          Locale locale = request.getLocale();
77  
78          // If supplied, set Locale based on request parameters;
79          // country and language
80          String language = request.getParameter("language");
81          String country = request.getParameter("country");
82  
83          if ((language != null && language.length() > 0)
84              && (country != null && country.length() > 0)) {
85              locale = new java.util.Locale(language, country);
86          } else if (language != null && language.length() > 0) {
87              locale = new java.util.Locale(language, "");
88          }
89  
90          //Save locale
91          session.setAttribute(Globals.LOCALE_KEY, locale);
92  
93          // Forward to result page
94          return mapping.findForward("success");
95      }
96  
97  }