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 java.util.Iterator;
  18. import java.util.Properties;
  19. import org.springframework.beans.factory.InitializingBean;
  20. import org.springframework.util.PathMatcher;
  21. /**
  22. * The most sophisticated and useful framework implementation of
  23. * the MethodNameResolver interface. Uses java.util.Properties
  24. * defining the mapping between the URL of incoming requests and
  25. * method name. Such properties can be held in an XML document.
  26. *
  27. * <p>Properties format is
  28. * <code>
  29. * /welcome.html=displayGenresPage
  30. * </code>
  31. * Note that method overloading isn't allowed, so there's no
  32. * need to specify arguments.
  33. *
  34. * <p>Supports direct matches, e.g. a registered "/test" matches "/test",
  35. * and a various Ant-style pattern matches, e.g. a registered "/t*" matches
  36. * both "/test" and "/team". For details, see the PathMatcher class.
  37. *
  38. * @author Rod Johnson
  39. * @author Juergen Hoeller
  40. * @see java.util.Properties
  41. * @see org.springframework.util.PathMatcher
  42. */
  43. public class PropertiesMethodNameResolver extends AbstractUrlMethodNameResolver
  44. implements InitializingBean {
  45. private Properties mappings;
  46. /**
  47. * Set URL to method name mappings from a Properties object.
  48. * @param mappings properties with URL as key and method name as value
  49. */
  50. public void setMappings(Properties mappings) {
  51. this.mappings = mappings;
  52. }
  53. public void afterPropertiesSet() {
  54. if (this.mappings == null || this.mappings.isEmpty()) {
  55. throw new IllegalArgumentException("'mappings' property is required");
  56. }
  57. }
  58. protected String getHandlerMethodNameForUrlPath(String urlPath) {
  59. String name = this.mappings.getProperty(urlPath);
  60. if (name != null) {
  61. return name;
  62. }
  63. for (Iterator it = this.mappings.keySet().iterator(); it.hasNext();) {
  64. String registeredPath = (String) it.next();
  65. if (PathMatcher.match(registeredPath, urlPath)) {
  66. return (String) this.mappings.get(registeredPath);
  67. }
  68. }
  69. return null;
  70. }
  71. }