KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > actions > SwitchAction


1 /*
2  * $Id: SwitchAction.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 2002-2004 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 org.apache.struts.actions;
20
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
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 import org.apache.struts.util.MessageResources;
33 import org.apache.struts.util.ModuleUtils;
34
35 /**
36  * <p>A standard <strong>Action</strong> that switches to a new module
37  * and then forwards control to a URI (specified in a number of possible ways)
38  * within the new module.</p>
39  *
40  * <p>Valid request parameters for this Action are:</p>
41  * <ul>
42  * <li><strong>page</strong> - Module-relative URI (beginning with "/")
43  * to which control should be forwarded after switching.</li>
44  * <li><strong>prefix</strong> - The module prefix (beginning with "/")
45  * of the module to which control should be switched. Use a
46  * zero-length string for the default module. The
47  * appropriate <code>ModuleConfig</code> object will be stored as a
48  * request attribute, so any subsequent logic will assume the new
49  * module.</li>
50  * </ul>
51  *
52  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
53  * @since Struts 1.1
54  */

55 public class SwitchAction extends Action {
56
57
58     // ----------------------------------------------------- Instance Variables
59

60
61     /**
62      * Commons Logging instance.
63      */

64     protected static Log log = LogFactory.getLog(SwitchAction.class);
65
66
67     /**
68      * The message resources for this package.
69      */

70     protected static MessageResources messages =
71         MessageResources.getMessageResources
72         ("org.apache.struts.actions.LocalStrings");
73
74
75     // --------------------------------------------------------- Public Methods
76

77
78     // See superclass for JavaDoc
79
public ActionForward execute(ActionMapping mapping,
80                                  ActionForm form,
81                                  HttpServletRequest JavaDoc request,
82                                  HttpServletResponse JavaDoc response)
83         throws Exception JavaDoc {
84
85         // Identify the request parameters controlling our actions
86
String JavaDoc page = request.getParameter("page");
87         String JavaDoc prefix = request.getParameter("prefix");
88         if ((page == null) || (prefix == null)) {
89             String JavaDoc message = messages.getMessage("switch.required");
90             log.error(message);
91             throw new ServletException JavaDoc(message);
92         }
93
94         // Switch to the requested module
95
ModuleUtils.getInstance().selectModule(prefix, request, getServlet().getServletContext());
96         
97         if (request.getAttribute(Globals.MODULE_KEY) == null) {
98             String JavaDoc message = messages.getMessage("switch.prefix", prefix);
99             log.error(message);
100             throw new ServletException JavaDoc(message);
101         }
102
103         // Forward control to the specified module-relative URI
104
return (new ActionForward(page));
105
106     }
107
108
109 }
110
Popular Tags