1 /*
2 * $Id$
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 package org.apache.struts.dispatcher.servlet;
22
23 import org.apache.struts.chain.contexts.ActionContext;
24 import org.apache.struts.chain.contexts.ServletActionContext;
25 import org.apache.struts.dispatcher.AbstractParameterDispatcher;
26
27 import jakarta.servlet.http.HttpServletResponse;
28
29 /**
30 * This servlet-based dispatcher uses the value of the request parameter to pick
31 * the appropriate method on the action.
32 * <p>
33 * To configure the use of this dispatcher in your configuration, create an
34 * entry like below:
35 * <p>
36 * <code>
37 * <pre>
38 * <action path="/saveSubscription"
39 * type="org.example.SubscriptionAction"
40 * dispatcher="org.apache.struts.dispatcher.servlet.ServletParameterDispatcher"
41 * parameter="method"/>
42 * name="subscriptionForm"
43 * scope="request"
44 * input="/subscription.jsp"
45 * </pre>
46 * </code>
47 * <p>
48 * This example will use the value of the request parameter named "method" to
49 * pick the appropriate method. For example, you might have the following three
50 * methods in the same action:
51 *
52 * <ul>
53 * <li><code>public ActionForward delete(ActionMapping mapping, ActionForm form,
54 * HttpServletRequest request, HttpServletResponse response) throws Exception</code></li>
55 * <li><code>public void insert(ActionContext context)</code></li>
56 * <li><code>public void update(ServletActionContext context)</code></li>
57 * </ul>
58 * and call one of the methods with a URL like this:
59 * <p>
60 * <code>http://localhost:8080/myapp/saveSubscription.do?method=update</code>
61 *
62 * @version $Rev$
63 * @since Struts 1.4
64 */
65 public class ServletParameterDispatcher extends AbstractParameterDispatcher {
66
67 private static final long serialVersionUID = 1L;
68
69 /**
70 * Constructs a new servlet parameter dispatcher.
71 */
72 public ServletParameterDispatcher() {
73 super(new ServletMethodResolver());
74 }
75
76 /**
77 * Extracts the value from the specified servlet parameter.
78 *
79 * @param context {@inheritDoc}
80 * @param parameter the servlet parameter name
81 * @return the servlet parameter value
82 */
83 protected String resolveParameterValue(ActionContext context, String parameter) {
84 ServletActionContext servletContext = (ServletActionContext) context;
85 return servletContext.getParam().get(parameter);
86 }
87
88 /**
89 * Sends the 404 HTTP error response.
90 *
91 * @param context {@inheritDoc}
92 * @return always <code>null</code> since the response is handled directly
93 * @throws Exception if the error code fails to set
94 */
95 protected Object unspecified(ActionContext context) throws Exception {
96 HttpServletResponse response = ((ServletActionContext) context).getResponse();
97 response.sendError(HttpServletResponse.SC_NOT_FOUND);
98 return null;
99 }
100
101 }