KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > mvc > multiaction > ParameterMethodNameResolver


1 /*
2  * Copyright 2002-2006 the original author or authors.
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 package org.springframework.web.servlet.mvc.multiaction;
18
19 import java.util.Properties JavaDoc;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.springframework.util.Assert;
27 import org.springframework.util.StringUtils;
28 import org.springframework.web.util.WebUtils;
29
30 /**
31  * Implementation of MethodNameResolver which supports several strategies for
32  * mapping parameter values to the names of methods to invoke.
33  *
34  * <p>The simplest strategy looks for a specific named parameter, whose value is
35  * considered the name of the method to invoke. The name of the parameter may be
36  * specified as a JavaBean property, if the default <code>action</code> is not
37  * acceptable.
38  *
39  * <p>The alternative strategy uses the very existence of a request parameter (
40  * i.e. a request parameter with a certain name is found) as an indication that a
41  * method with the same name should be dispatched to. In this case, the actual
42  * request parameter value is ignored. The list of parameter/method names may
43  * be set via the <code>methodParamNames<code> JavaBean property.
44  *
45  * <p>The second resolution strategy is primarily expected to be used with web
46  * pages containing multiple submit buttons. The 'name' attribute of each
47  * button should be set to the mapped method name, while the 'value' attribute
48  * is normally displayed as the button label by the browser, and will be
49  * ignored by the resolver.
50  *
51  * <p>Note that the second strategy also supports the use of submit buttons of
52  * type 'image'. That is, an image submit button named 'reset' will normally be
53  * submitted by the browser as two request paramters called 'reset.x', and
54  * 'reset.y'. When checking for the existence of a paramter from the
55  * <code>methodParamNames</code> list, to indicate that a specific method should
56  * be called, the code will look for request parameter in the "reset" form
57  * (exactly as spcified in the list), and in the "reset.x" form ('.x' appended to
58  * the name in the list). In this way it can handle both normal and image submit
59  * buttons. The actual method name resolved if there is a match will always be
60  * the bare form without the ".x".
61  *
62  * <p><b>Note:</b> If both strategies are configured, i.e. both "paramName"
63  * and "methodParamNames" are specified, then both will be checked for any given
64  * request. A match for an explicit request parameter in the "methodParamNames"
65  * list always wins over a value specified for a "paramName" action parameter.
66  *
67  * <p>For use with either strategy, the name of a default handler method to use
68  * when there is no match, can be specified as a JavaBean property.
69  *
70  * <p>For both resolution strategies, the method name is of course coming from
71  * some sort of view code, (such as a JSP page). While this may be acceptable,
72  * it is sometimes desireable to treat this only as a 'logical' method name,
73  * with a further mapping to a 'real' method name. As such, an optional
74  * 'logical' mapping may be specified for this purpose.
75  *
76  * @author Rod Johnson
77  * @author Juergen Hoeller
78  * @author Colin Sampaleanu
79  * @see #setParamName
80  * @see #setMethodParamNames
81  * @see #setLogicalMappings
82  * @see #setDefaultMethodName
83  */

84 public class ParameterMethodNameResolver implements MethodNameResolver {
85
86     /**
87      * Default name for the parameter whose value identifies the method to invoke:
88      * "action".
89      */

90     public static final String JavaDoc DEFAULT_PARAM_NAME = "action";
91
92
93     protected final Log logger = LogFactory.getLog(getClass());
94
95     private String JavaDoc paramName = DEFAULT_PARAM_NAME;
96
97     private String JavaDoc[] methodParamNames;
98
99     private Properties JavaDoc logicalMappings;
100
101     private String JavaDoc defaultMethodName;
102
103
104     /**
105      * Set the name of the parameter whose <i>value</i> identifies the name of
106      * the method to invoke. Default is "action".
107      * <p>Alternatively, specify parameter names where the very existence of each
108      * parameter means that a method of the same name should be invoked, via
109      * the "methodParamNames" property.
110      * @see #setMethodParamNames
111      */

112     public void setParamName(String JavaDoc paramName) {
113         if (paramName != null) {
114             Assert.hasText(paramName, "'paramName' must not be empty");
115         }
116         this.paramName = paramName;
117     }
118
119     /**
120      * Set a String array of parameter names, where the <i>very existence of a
121      * parameter</i> in the list (with value ignored) means that a method of the
122      * same name should be invoked. This target method name may then be optionally
123      * further mapped via the {@link #logicalMappings} property, in which case it
124      * can be considered a logical name only.
125      * @see #setParamName
126      */

127     public void setMethodParamNames(String JavaDoc[] methodParamNames) {
128         this.methodParamNames = methodParamNames;
129     }
130
131     /**
132      * Specifies a set of optional logical method name mappings. For both resolution
133      * strategies, the method name initially comes in from the view layer. If that needs
134      * to be treated as a 'logical' method name, and mapped to a 'real' method name, then
135      * a name/value pair for that purpose should be added to this Properties instance.
136      * Any method name not found in this mapping will be considered to already be the
137      * real method name.
138      * <p>Note that in the case of no match, where the {@link #defaultMethodName} property
139      * is used if available, that method name is considered to already be the real method
140      * name, and is not run through the logical mapping.
141      * @param logicalMappings a Properties object mapping logical method names to real
142      * method names
143      */

144     public void setLogicalMappings(Properties JavaDoc logicalMappings) {
145         this.logicalMappings = logicalMappings;
146     }
147
148     /**
149      * Set the name of the default handler method that should be
150      * used when no parameter was found in the request
151      */

152     public void setDefaultMethodName(String JavaDoc defaultMethodName) {
153         if (defaultMethodName != null) {
154             Assert.hasText(defaultMethodName, "'defaultMethodName' must not be empty");
155         }
156         this.defaultMethodName = defaultMethodName;
157     }
158
159
160     public String JavaDoc getHandlerMethodName(HttpServletRequest JavaDoc request) throws NoSuchRequestHandlingMethodException {
161         String JavaDoc methodName = null;
162
163         // Check parameter names where the very existence of each parameter
164
// means that a method of the same name should be invoked, if any.
165
if (this.methodParamNames != null) {
166             for (int i = 0; i < this.methodParamNames.length; ++i) {
167                 String JavaDoc candidate = this.methodParamNames[i];
168                 if (WebUtils.hasSubmitParameter(request, candidate)) {
169                     methodName = candidate;
170                     if (logger.isDebugEnabled()) {
171                         logger.debug("Determined handler method '" + methodName +
172                                 "' based on existence of explicit request parameter of same name");
173                     }
174                     break;
175                 }
176             }
177         }
178
179         // Check parameter whose value identifies the method to invoke, if any.
180
if (methodName == null && this.paramName != null) {
181             methodName = request.getParameter(this.paramName);
182             if (methodName != null) {
183                 if (logger.isDebugEnabled()) {
184                     logger.debug("Determined handler method '" + methodName +
185                             "' based on value of request parameter '" + this.paramName + "'");
186                 }
187             }
188         }
189
190         if (methodName != null && this.logicalMappings != null) {
191             // Resolve logical name into real method name, if appropriate.
192
String JavaDoc originalName = methodName;
193             methodName = this.logicalMappings.getProperty(methodName, methodName);
194             if (logger.isDebugEnabled()) {
195                 logger.debug("Resolved method name '" + originalName + "' to handler method '" + methodName + "'");
196             }
197         }
198
199         if (methodName != null && !StringUtils.hasText(methodName)) {
200             if (logger.isDebugEnabled()) {
201                 logger.debug("Method name '" + methodName + "' is empty: treating it as no method name found");
202             }
203             methodName = null;
204         }
205
206         if (methodName == null) {
207             if (this.defaultMethodName != null) {
208                 // No specific method resolved: use default method.
209
methodName = this.defaultMethodName;
210                 if (logger.isDebugEnabled()) {
211                     logger.debug("Falling back to default handler method '" + this.defaultMethodName + "'");
212                 }
213             }
214             else {
215                 // If resolution failed completely, throw an exception.
216
throw new NoSuchRequestHandlingMethodException(request);
217             }
218         }
219
220         return methodName;
221     }
222
223 }
224
Popular Tags