001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.portlet.handler; 018 019import java.util.Map; 020import javax.portlet.PortletRequest; 021 022import org.springframework.beans.BeansException; 023import org.springframework.util.Assert; 024 025/** 026 * Implementation of the {@link org.springframework.web.portlet.HandlerMapping} 027 * to map from a request parameter to request handler beans. 028 * 029 * <p>The default name of the parameter is "action", but can be changed using 030 * {@link #setParameterName setParameterName()}. 031 * 032 * <p>The bean configuration for this mapping will look somthing like this: 033 * 034 * <pre class="code"> 035 * <bean id="parameterHandlerMapping" class="org.springframework.web.portlet.handler.ParameterHandlerMapping"> 036 * <property name="parameterMap"> 037 * <map> 038 * <entry key="add"><ref bean="addItemHandler"/></entry> 039 * <entry key="edit"><ref bean="editItemHandler"/></entry> 040 * <entry key="delete"><ref bean="deleteItemHandler"/></entry> 041 * </map> 042 * </property> 043 * </bean></pre> 044 * 045 * Thanks to Rainer Schmitz for suggesting this mapping strategy! 046 * 047 * @author John A. Lewis 048 * @author Juergen Hoeller 049 * @since 2.0 050 * @see ParameterMappingInterceptor 051 */ 052public class ParameterHandlerMapping extends AbstractMapBasedHandlerMapping<String> { 053 054 /** 055 * Default request parameter name to use for mapping to handlers: "action". 056 */ 057 public final static String DEFAULT_PARAMETER_NAME = "action"; 058 059 060 private String parameterName = DEFAULT_PARAMETER_NAME; 061 062 private Map<String, ?> parameterMap; 063 064 065 /** 066 * Set the name of the parameter used for mapping to handlers. 067 * <p>Default is "action". 068 */ 069 public void setParameterName(String parameterName) { 070 Assert.hasText(parameterName, "'parameterName' must not be empty"); 071 this.parameterName = parameterName; 072 } 073 074 /** 075 * Set a Map with parameters as keys and handler beans or bean names as values. 076 * Convenient for population with bean references. 077 * @param parameterMap map with parameters as keys and beans as values 078 */ 079 public void setParameterMap(Map<String, ?> parameterMap) { 080 this.parameterMap = parameterMap; 081 } 082 083 084 /** 085 * Calls the {@code registerHandlers} method in addition 086 * to the superclass's initialization. 087 * @see #registerHandlers 088 */ 089 @Override 090 public void initApplicationContext() throws BeansException { 091 super.initApplicationContext(); 092 registerHandlers(this.parameterMap); 093 } 094 095 /** 096 * Uses the value of the specified parameter as lookup key. 097 * @see #setParameterName 098 */ 099 @Override 100 protected String getLookupKey(PortletRequest request) throws Exception { 101 return request.getParameter(this.parameterName); 102 } 103 104}