001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.servlet.mvc.multiaction;
018:
019: import java.util.Properties;
020:
021: import javax.servlet.http.HttpServletRequest;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: import org.springframework.util.Assert;
027: import org.springframework.util.StringUtils;
028: import org.springframework.web.util.WebUtils;
029:
030: /**
031: * Implementation of MethodNameResolver which supports several strategies for
032: * mapping parameter values to the names of methods to invoke.
033: *
034: * <p>The simplest strategy looks for a specific named parameter, whose value is
035: * considered the name of the method to invoke. The name of the parameter may be
036: * specified as a JavaBean property, if the default <code>action</code> is not
037: * acceptable.
038: *
039: * <p>The alternative strategy uses the very existence of a request parameter (
040: * i.e. a request parameter with a certain name is found) as an indication that a
041: * method with the same name should be dispatched to. In this case, the actual
042: * request parameter value is ignored. The list of parameter/method names may
043: * be set via the <code>methodParamNames<code> JavaBean property.
044: *
045: * <p>The second resolution strategy is primarily expected to be used with web
046: * pages containing multiple submit buttons. The 'name' attribute of each
047: * button should be set to the mapped method name, while the 'value' attribute
048: * is normally displayed as the button label by the browser, and will be
049: * ignored by the resolver.
050: *
051: * <p>Note that the second strategy also supports the use of submit buttons of
052: * type 'image'. That is, an image submit button named 'reset' will normally be
053: * submitted by the browser as two request paramters called 'reset.x', and
054: * 'reset.y'. When checking for the existence of a paramter from the
055: * <code>methodParamNames</code> list, to indicate that a specific method should
056: * be called, the code will look for request parameter in the "reset" form
057: * (exactly as spcified in the list), and in the "reset.x" form ('.x' appended to
058: * the name in the list). In this way it can handle both normal and image submit
059: * buttons. The actual method name resolved if there is a match will always be
060: * the bare form without the ".x".
061: *
062: * <p><b>Note:</b> If both strategies are configured, i.e. both "paramName"
063: * and "methodParamNames" are specified, then both will be checked for any given
064: * request. A match for an explicit request parameter in the "methodParamNames"
065: * list always wins over a value specified for a "paramName" action parameter.
066: *
067: * <p>For use with either strategy, the name of a default handler method to use
068: * when there is no match, can be specified as a JavaBean property.
069: *
070: * <p>For both resolution strategies, the method name is of course coming from
071: * some sort of view code, (such as a JSP page). While this may be acceptable,
072: * it is sometimes desireable to treat this only as a 'logical' method name,
073: * with a further mapping to a 'real' method name. As such, an optional
074: * 'logical' mapping may be specified for this purpose.
075: *
076: * @author Rod Johnson
077: * @author Juergen Hoeller
078: * @author Colin Sampaleanu
079: * @see #setParamName
080: * @see #setMethodParamNames
081: * @see #setLogicalMappings
082: * @see #setDefaultMethodName
083: */
084: public class ParameterMethodNameResolver implements MethodNameResolver {
085:
086: /**
087: * Default name for the parameter whose value identifies the method to invoke:
088: * "action".
089: */
090: public static final String DEFAULT_PARAM_NAME = "action";
091:
092: protected final Log logger = LogFactory.getLog(getClass());
093:
094: private String paramName = DEFAULT_PARAM_NAME;
095:
096: private String[] methodParamNames;
097:
098: private Properties logicalMappings;
099:
100: private String defaultMethodName;
101:
102: /**
103: * Set the name of the parameter whose <i>value</i> identifies the name of
104: * the method to invoke. Default is "action".
105: * <p>Alternatively, specify parameter names where the very existence of each
106: * parameter means that a method of the same name should be invoked, via
107: * the "methodParamNames" property.
108: * @see #setMethodParamNames
109: */
110: public void setParamName(String paramName) {
111: if (paramName != null) {
112: Assert.hasText(paramName, "'paramName' must not be empty");
113: }
114: this .paramName = paramName;
115: }
116:
117: /**
118: * Set a String array of parameter names, where the <i>very existence of a
119: * parameter</i> in the list (with value ignored) means that a method of the
120: * same name should be invoked. This target method name may then be optionally
121: * further mapped via the {@link #logicalMappings} property, in which case it
122: * can be considered a logical name only.
123: * @see #setParamName
124: */
125: public void setMethodParamNames(String[] methodParamNames) {
126: this .methodParamNames = methodParamNames;
127: }
128:
129: /**
130: * Specifies a set of optional logical method name mappings. For both resolution
131: * strategies, the method name initially comes in from the view layer. If that needs
132: * to be treated as a 'logical' method name, and mapped to a 'real' method name, then
133: * a name/value pair for that purpose should be added to this Properties instance.
134: * Any method name not found in this mapping will be considered to already be the
135: * real method name.
136: * <p>Note that in the case of no match, where the {@link #defaultMethodName} property
137: * is used if available, that method name is considered to already be the real method
138: * name, and is not run through the logical mapping.
139: * @param logicalMappings a Properties object mapping logical method names to real
140: * method names
141: */
142: public void setLogicalMappings(Properties logicalMappings) {
143: this .logicalMappings = logicalMappings;
144: }
145:
146: /**
147: * Set the name of the default handler method that should be
148: * used when no parameter was found in the request
149: */
150: public void setDefaultMethodName(String defaultMethodName) {
151: if (defaultMethodName != null) {
152: Assert.hasText(defaultMethodName,
153: "'defaultMethodName' must not be empty");
154: }
155: this .defaultMethodName = defaultMethodName;
156: }
157:
158: public String getHandlerMethodName(HttpServletRequest request)
159: throws NoSuchRequestHandlingMethodException {
160: String methodName = null;
161:
162: // Check parameter names where the very existence of each parameter
163: // means that a method of the same name should be invoked, if any.
164: if (this .methodParamNames != null) {
165: for (int i = 0; i < this .methodParamNames.length; ++i) {
166: String candidate = this .methodParamNames[i];
167: if (WebUtils.hasSubmitParameter(request, candidate)) {
168: methodName = candidate;
169: if (logger.isDebugEnabled()) {
170: logger
171: .debug("Determined handler method '"
172: + methodName
173: + "' based on existence of explicit request parameter of same name");
174: }
175: break;
176: }
177: }
178: }
179:
180: // Check parameter whose value identifies the method to invoke, if any.
181: if (methodName == null && this .paramName != null) {
182: methodName = request.getParameter(this .paramName);
183: if (methodName != null) {
184: if (logger.isDebugEnabled()) {
185: logger.debug("Determined handler method '"
186: + methodName
187: + "' based on value of request parameter '"
188: + this .paramName + "'");
189: }
190: }
191: }
192:
193: if (methodName != null && this .logicalMappings != null) {
194: // Resolve logical name into real method name, if appropriate.
195: String originalName = methodName;
196: methodName = this .logicalMappings.getProperty(methodName,
197: methodName);
198: if (logger.isDebugEnabled()) {
199: logger.debug("Resolved method name '" + originalName
200: + "' to handler method '" + methodName + "'");
201: }
202: }
203:
204: if (methodName != null && !StringUtils.hasText(methodName)) {
205: if (logger.isDebugEnabled()) {
206: logger
207: .debug("Method name '"
208: + methodName
209: + "' is empty: treating it as no method name found");
210: }
211: methodName = null;
212: }
213:
214: if (methodName == null) {
215: if (this .defaultMethodName != null) {
216: // No specific method resolved: use default method.
217: methodName = this .defaultMethodName;
218: if (logger.isDebugEnabled()) {
219: logger
220: .debug("Falling back to default handler method '"
221: + this .defaultMethodName + "'");
222: }
223: } else {
224: // If resolution failed completely, throw an exception.
225: throw new NoSuchRequestHandlingMethodException(request);
226: }
227: }
228:
229: return methodName;
230: }
231:
232: }
|