KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > handler > ParameterHandlerMapping


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.portlet.handler;
18
19 import java.util.Map JavaDoc;
20
21 import javax.portlet.PortletRequest;
22
23 import org.springframework.beans.BeansException;
24 import org.springframework.util.Assert;
25 import org.springframework.util.CollectionUtils;
26
27 /**
28  * <p>Implementation of the HandlerMapping interface to map from
29  * a request parameter to request handler beans.</p>
30  *
31  * <p>The default name of the parameter is "action", but can be changed using
32  * {@link #setParameterName setParameterName()}.</p>
33  *
34  * <p>The bean configuration for this mapping will look somthing like this:</p>
35  * <pre>
36  * &lt;bean id="parameterHandlerMapping" class="org.springframework.web.portlet.handler.ParameterHandlerMapping"&gt;
37  * &lt;property name="parameterMap"&gt;
38  * &lt;map&gt;
39  * &lt;entry key="add"&gt;&lt;ref bean="addItemHandler"/&gt;&lt;/entry&gt;
40  * &lt;entry key="edit"&gt;&lt;ref bean="editItemHandler"/&gt;&lt;/entry&gt;
41  * &lt;entry key="delete"&gt;&lt;ref bean="deleteItemHandler"/&gt;&lt;/entry&gt;
42  * &lt;/map&gt;
43  * &lt;/property&gt;
44  * &lt;/bean&gt;
45  * </pre>
46  *
47  * @author Rainer Schmitz
48  * @author John A. Lewis
49  * @see ParameterMappingInterceptor
50  * @since 2.0
51  */

52 public class ParameterHandlerMapping extends AbstractMapBasedHandlerMapping {
53
54     /**
55      * Default request parameter name to use for mapping to handlers: "action".
56      */

57     public final static String JavaDoc DEFAULT_PARAMETER_NAME = "action";
58
59
60     private String JavaDoc parameterName = DEFAULT_PARAMETER_NAME;
61
62     private Map JavaDoc parameterMap;
63
64
65     /**
66      * Set the name of the parameter used for mapping to handlers.
67      * <p>Default is "action".
68      */

69     public void setParameterName(String JavaDoc parameterName) {
70         Assert.hasText(parameterName, "'parameterName' must not be empty");
71         this.parameterName = parameterName;
72     }
73
74     /**
75      * Set a Map with parameters as keys and handler beans or bean names as values.
76      * Convenient for population with bean references.
77      * @param parameterMap map with parameters as keys and beans as values
78      */

79     public void setParameterMap(Map JavaDoc parameterMap) {
80         this.parameterMap = parameterMap;
81     }
82
83
84     /**
85      * Calls the <code>registerHandlers</code> method in addition
86      * to the superclass's initialization.
87      * @see #registerHandlers
88      */

89     public void initApplicationContext() throws BeansException {
90         super.initApplicationContext();
91         registerHandlers(this.parameterMap);
92     }
93
94     /**
95      * Register all handlers specified in the Portlet mode map for the corresponding modes.
96      * @param parameterMap Map with parameter names as keys and handler beans or bean names as values
97      * @throws BeansException if the handler couldn't be registered
98      */

99     protected void registerHandlers(Map JavaDoc parameterMap) throws BeansException {
100         if (CollectionUtils.isEmpty(parameterMap)) {
101             logger.warn("'parameterMap' is empty on ParameterHandlerMapping");
102         }
103         else {
104             super.registerHandlers(parameterMap);
105         }
106     }
107
108
109     /**
110      * Uses the value of the specified parameter as lookup key.
111      * @see #setParameterName
112      */

113     protected Object JavaDoc getLookupKey(PortletRequest request) throws Exception JavaDoc {
114         return request.getParameter(this.parameterName);
115     }
116
117 }
118
Popular Tags