1. /*
  2. * Copyright 2002-2004 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. package org.springframework.web.servlet.mvc.multiaction;
  17. import javax.servlet.http.HttpServletRequest;
  18. /**
  19. * Simple implementation of MethodNameResolver that looks for a
  20. * parameter value containing the name of the method to invoke.
  21. *
  22. * <p>The name of the parameter and optionally also the name of a
  23. * default handler method can be specified as JavaBean properties.
  24. *
  25. * @author Rod Johnson
  26. * @author Juergen Hoeller
  27. * @see #setParamName
  28. * @see #setDefaultMethodName
  29. */
  30. public class ParameterMethodNameResolver implements MethodNameResolver {
  31. public static final String DEFAULT_PARAM_NAME = "action";
  32. private String paramName = DEFAULT_PARAM_NAME;
  33. private String defaultMethodName;
  34. /**
  35. * Set the parameter name we're looking for.
  36. * Default is "action".
  37. */
  38. public void setParamName(String paramName) {
  39. this.paramName = paramName;
  40. }
  41. /**
  42. * Set the name of the default handler method that should be
  43. * used when no parameter was found in the request
  44. */
  45. public void setDefaultMethodName(String defaultMethodName) {
  46. this.defaultMethodName = defaultMethodName;
  47. }
  48. public String getHandlerMethodName(HttpServletRequest request) throws NoSuchRequestHandlingMethodException {
  49. String methodName = request.getParameter(this.paramName);
  50. if (methodName == null) {
  51. methodName = this.defaultMethodName;
  52. }
  53. if (methodName == null) {
  54. throw new NoSuchRequestHandlingMethodException(request);
  55. }
  56. return methodName;
  57. }
  58. }