1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.actions;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.struts.Globals;
26 import org.apache.struts.action.ActionForm;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.ActionMapping;
29 import org.apache.struts.util.ModuleUtils;
30
31 import javax.servlet.ServletException;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 /**
36 * <p>A standard <strong>Action</strong> that switches to a new module and
37 * 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 *
42 * <ul>
43 *
44 * <li><strong>page</strong> - Module-relative URI (beginning with "/") to
45 * which control should be forwarded after switching.</li>
46 *
47 * <li><strong>prefix</strong> - The module prefix (beginning with "/") of the
48 * module to which control should be switched. Use a zero-length string for
49 * the default module. The appropriate <code>ModuleConfig</code> object will
50 * be stored as a request attribute, so any subsequent logic will assume the
51 * new module.</li>
52 *
53 * </ul>
54 *
55 * @version $Rev: 471754 $ $Date: 2005-05-14 21:27:02 -0400 (Sat, 14 May 2005)
56 * $
57 * @since Struts 1.1
58 */
59 public class SwitchAction extends BaseAction {
60
61
62 /**
63 * Commons Logging instance.
64 */
65 protected static Log log = LogFactory.getLog(SwitchAction.class);
66
67 /**
68 * Process the specified HTTP request, and create the corresponding HTTP
69 * response (or forward to another web component that will create it).
70 * Return an <code>ActionForward</code> instance describing where and how
71 * control should be forwarded, or <code>null</code> if the response has
72 * already been completed.
73 *
74 * @param mapping The ActionMapping used to select this instance
75 * @param form The optional ActionForm bean for this request (if any)
76 * @param request The HTTP request we are processing
77 * @param response The HTTP response we are creating
78 * @return Return an <code>ActionForward</code> instance describing where
79 * and how control should be forwarded, or <code>null</code> if
80 * the response has already been completed.
81 * @throws Exception if an exception occurs
82 */
83 public ActionForward execute(ActionMapping mapping, ActionForm form,
84 HttpServletRequest request, HttpServletResponse response)
85 throws Exception {
86
87 String page = request.getParameter("page");
88 String prefix = request.getParameter("prefix");
89
90 if ((page == null) || (prefix == null)) {
91 String message = messages.getMessage("switch.required");
92
93 log.error(message);
94 throw new ServletException(message);
95 }
96
97
98 ModuleUtils.getInstance().selectModule(prefix, request,
99 getServlet().getServletContext());
100
101 if (request.getAttribute(Globals.MODULE_KEY) == null) {
102 String message = messages.getMessage("switch.prefix", prefix);
103
104 log.error(message);
105 throw new ServletException(message);
106 }
107
108
109 return (new ActionForward(page));
110 }
111 }