/* * Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.web.portlet.handler; import java.util.Map; import javax.portlet.PortletRequest; import org.springframework.beans.BeansException; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; /** * Implementation of the {@link org.springframework.web.portlet.HandlerMapping} * to map from a request parameter to request handler beans. * * <p>The default name of the parameter is "action", but can be changed using * {@link #setParameterName setParameterName()}. * * <p>The bean configuration for this mapping will look somthing like this: * * <pre class="code"> * <bean id="parameterHandlerMapping" class="org.springframework.web.portlet.handler.ParameterHandlerMapping"> * <property name="parameterMap"> * <map> * <entry key="add"><ref bean="addItemHandler"/></entry> * <entry key="edit"><ref bean="editItemHandler"/></entry> * <entry key="delete"><ref bean="deleteItemHandler"/></entry> * </map> * </property> * </bean></pre> * * Thanks to Rainer Schmitz for suggesting this mapping strategy! * * @author John A. Lewis * @author Juergen Hoeller * @since 2.0 * @see ParameterMappingInterceptor */ public class ParameterHandlerMapping extends AbstractMapBasedHandlerMapping { /** * Default request parameter name to use for mapping to handlers: "action". */ public final static String DEFAULT_PARAMETER_NAME = "action"; private String parameterName = DEFAULT_PARAMETER_NAME; private Map parameterMap; /** * Set the name of the parameter used for mapping to handlers. * <p>Default is "action". */ public void setParameterName(String parameterName) { Assert.hasText(parameterName, "'parameterName' must not be empty"); this.parameterName = parameterName; } /** * Set a Map with parameters as keys and handler beans or bean names as values. * Convenient for population with bean references. * @param parameterMap map with parameters as keys and beans as values */ public void setParameterMap(Map parameterMap) { this.parameterMap = parameterMap; } /** * Calls the <code>registerHandlers</code> method in addition * to the superclass's initialization. * @see #registerHandlers */ public void initApplicationContext() throws BeansException { super.initApplicationContext(); registerHandlers(this.parameterMap); } /** * Register all handlers for the specified parameters. * @param parameterMap Map with parameter names as keys and handler beans or bean names as values * @throws BeansException if the handler couldn't be registered */ protected void registerHandlers(Map parameterMap) throws BeansException { if (CollectionUtils.isEmpty(parameterMap)) { logger.warn("'parameterMap' is empty on ParameterHandlerMapping"); } else { super.registerHandlers(parameterMap); } } /** * Uses the value of the specified parameter as lookup key. * @see #setParameterName */ protected Object getLookupKey(PortletRequest request) throws Exception { return request.getParameter(this.parameterName); } }