KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > SetLocaleAction


1 /*
2  * Copyright 2001,2004 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
18 package org.apache.webapp.admin;
19
20
21 import java.io.IOException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Locale JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import javax.servlet.http.HttpSession JavaDoc;
29 import org.apache.struts.Globals;
30 import org.apache.struts.action.Action;
31 import org.apache.struts.action.ActionErrors;
32 import org.apache.struts.action.ActionForm;
33 import org.apache.struts.action.ActionForward;
34 import org.apache.struts.action.ActionMapping;
35
36
37 /**
38  * Implementation of <strong>Action</strong> that sets the current Locale
39  * to the one specified by the <code>locale</code> request parameter.
40  *
41  * @author Craig R. McClanahan
42  * @version $Revision: 1.4 $ $Date: 2004/10/18 06:37:53 $
43  */

44
45 public final class SetLocaleAction extends Action {
46
47
48     // --------------------------------------------------------- Public Methods
49

50
51     /**
52      * Process the specified HTTP request, and create the corresponding HTTP
53      * response (or forward to another web component that will create it).
54      * Return an <code>ActionForward</code> instance describing where and how
55      * control should be forwarded, or <code>null</code> if the response has
56      * already been completed.
57      *
58      * @param mapping The ActionMapping used to select this instance
59      * @param actionForm The optional ActionForm bean for this request (if any)
60      * @param request The HTTP request we are processing
61      * @param response The HTTP response we are creating
62      *
63      * @exception IOException if an input/output error occurs
64      * @exception ServletException if a servlet exception occurs
65      */

66     public ActionForward execute(ActionMapping mapping,
67                                  ActionForm form,
68                                  HttpServletRequest JavaDoc request,
69                                  HttpServletResponse JavaDoc response)
70         throws IOException JavaDoc, ServletException JavaDoc {
71
72         // What locale does the user want to switch to?
73
String JavaDoc requestedLocale = ((SetLocaleForm) form).getLocale();
74
75         // Switch to the specified locale, if it exists
76
if (requestedLocale != null) {
77             ApplicationLocales locales = (ApplicationLocales)
78                 getServlet().getServletContext().getAttribute
79                 (ApplicationServlet.LOCALES_KEY);
80             Iterator JavaDoc iterator = locales.getSupportedLocales().iterator();
81             Locale JavaDoc currentLocale = null;
82             while (iterator.hasNext()) {
83                 currentLocale = (Locale JavaDoc) iterator.next();
84                 if (requestedLocale.equals(currentLocale.toString())) {
85                     HttpSession JavaDoc session = request.getSession();
86                     session.setAttribute(Globals.LOCALE_KEY, currentLocale);
87                     // Remove form bean so it will get recreated next time
88
session.removeAttribute(mapping.getAttribute());
89                     break;
90                 }
91             }
92         }
93
94         // Forward control back to the main menu
95
return (mapping.findForward("Main Menu"));
96
97     }
98
99
100 }
101
Popular Tags