KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > handler > SimpleUrlHandlerMapping


1 /*
2  * Copyright 2002-2007 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.handler;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import org.springframework.beans.BeansException;
25
26 /**
27  * Implementation of the {@link org.springframework.web.servlet.HandlerMapping}
28  * interface to map from URLs to request handler beans. Supports both mapping to bean
29  * instances and mapping to bean names; the latter is required for non-singleton handlers.
30  *
31  * <p>The "urlMap" property is suitable for populating the handler map with
32  * bean references, e.g. via the map element in XML bean definitions.
33  *
34  * <p>Mappings to bean names can be set via the "mappings" property, in a form
35  * accepted by the <code>java.util.Properties</code> class, like as follows:<br>
36  * <code>
37  * /welcome.html=ticketController
38  * /show.html=ticketController
39  * </code><br>
40  * The syntax is <code>PATH=HANDLER_BEAN_NAME</code>.
41  * If the path doesn't begin with a slash, one is prepended.
42  *
43  * <p>Supports direct matches (given "/test" -> registered "/test") and "*"
44  * matches (given "/test" -> registered "/t*"). Note that the default is
45  * to map within the current servlet mapping if applicable; see the
46  * {@link #setAlwaysUseFullPath "alwaysUseFullPath"} property for details.
47  * For details on the pattern options, see the
48  * {@link org.springframework.util.AntPathMatcher} javadoc.
49
50  * @author Rod Johnson
51  * @author Juergen Hoeller
52  * @see #setMappings
53  * @see #setUrlMap
54  * @see BeanNameUrlHandlerMapping
55  */

56 public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
57     
58     private final Map JavaDoc urlMap = new HashMap JavaDoc();
59
60
61     /**
62      * Map URL paths to handler bean names.
63      * This is the typical way of configuring this HandlerMapping.
64      * <p>Supports direct URL matches and Ant-style pattern matches. For syntax
65      * details, see the {@link org.springframework.util.AntPathMatcher} javadoc.
66      * @param mappings properties with URLs as keys and bean names as values
67      * @see #setUrlMap
68      */

69     public void setMappings(Properties JavaDoc mappings) {
70         this.urlMap.putAll(mappings);
71     }
72
73     /**
74      * Set a Map with URL paths as keys and handler beans (or handler bean names)
75      * as values. Convenient for population with bean references.
76      * <p>Supports direct URL matches and Ant-style pattern matches. For syntax
77      * details, see the {@link org.springframework.util.AntPathMatcher} javadoc.
78      * @param urlMap map with URLs as keys and beans as values
79      * @see #setMappings
80      */

81     public void setUrlMap(Map JavaDoc urlMap) {
82         this.urlMap.putAll(urlMap);
83     }
84
85     /**
86      * Allow Map access to the URL path mappings, with the option to add or
87      * override specific entries.
88      * <p>Useful for specifying entries directly, for example via "urlMap[myKey]".
89      * This is particularly useful for adding or overriding entries in child
90      * bean definitions.
91      */

92     public Map JavaDoc getUrlMap() {
93         return this.urlMap;
94     }
95
96
97     /**
98      * Calls the {@link #registerHandlers} method in addition to the
99      * superclass's initialization.
100      */

101     public void initApplicationContext() throws BeansException {
102         super.initApplicationContext();
103         registerHandlers(this.urlMap);
104     }
105
106     /**
107      * Register all handlers specified in the URL map for the corresponding paths.
108      * @param urlMap Map with URL paths as keys and handler beans or bean names as values
109      * @throws BeansException if a handler couldn't be registered
110      * @throws IllegalStateException if there is a conflicting handler registered
111      */

112     protected void registerHandlers(Map JavaDoc urlMap) throws BeansException {
113         if (urlMap.isEmpty()) {
114             logger.warn("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping");
115         }
116         else {
117             Iterator JavaDoc it = urlMap.keySet().iterator();
118             while (it.hasNext()) {
119                 String JavaDoc url = (String JavaDoc) it.next();
120                 Object JavaDoc handler = urlMap.get(url);
121                 // Prepend with slash if not already present.
122
if (!url.startsWith("/")) {
123                     url = "/" + url;
124                 }
125                 registerHandler(url, handler);
126             }
127         }
128     }
129
130 }
131
Popular Tags