1 16 17 package org.springframework.web.servlet.mvc.multiaction; 18 19 import java.util.Properties ; 20 21 import javax.servlet.http.HttpServletRequest ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import org.springframework.util.Assert; 27 import org.springframework.util.StringUtils; 28 import org.springframework.web.util.WebUtils; 29 30 84 public class ParameterMethodNameResolver implements MethodNameResolver { 85 86 90 public static final String DEFAULT_PARAM_NAME = "action"; 91 92 93 protected final Log logger = LogFactory.getLog(getClass()); 94 95 private String paramName = DEFAULT_PARAM_NAME; 96 97 private String [] methodParamNames; 98 99 private Properties logicalMappings; 100 101 private String defaultMethodName; 102 103 104 112 public void setParamName(String paramName) { 113 if (paramName != null) { 114 Assert.hasText(paramName, "'paramName' must not be empty"); 115 } 116 this.paramName = paramName; 117 } 118 119 127 public void setMethodParamNames(String [] methodParamNames) { 128 this.methodParamNames = methodParamNames; 129 } 130 131 144 public void setLogicalMappings(Properties logicalMappings) { 145 this.logicalMappings = logicalMappings; 146 } 147 148 152 public void setDefaultMethodName(String defaultMethodName) { 153 if (defaultMethodName != null) { 154 Assert.hasText(defaultMethodName, "'defaultMethodName' must not be empty"); 155 } 156 this.defaultMethodName = defaultMethodName; 157 } 158 159 160 public String getHandlerMethodName(HttpServletRequest request) throws NoSuchRequestHandlingMethodException { 161 String methodName = null; 162 163 if (this.methodParamNames != null) { 166 for (int i = 0; i < this.methodParamNames.length; ++i) { 167 String candidate = this.methodParamNames[i]; 168 if (WebUtils.hasSubmitParameter(request, candidate)) { 169 methodName = candidate; 170 if (logger.isDebugEnabled()) { 171 logger.debug("Determined handler method '" + methodName + 172 "' based on existence of explicit request parameter of same name"); 173 } 174 break; 175 } 176 } 177 } 178 179 if (methodName == null && this.paramName != null) { 181 methodName = request.getParameter(this.paramName); 182 if (methodName != null) { 183 if (logger.isDebugEnabled()) { 184 logger.debug("Determined handler method '" + methodName + 185 "' based on value of request parameter '" + this.paramName + "'"); 186 } 187 } 188 } 189 190 if (methodName != null && this.logicalMappings != null) { 191 String originalName = methodName; 193 methodName = this.logicalMappings.getProperty(methodName, methodName); 194 if (logger.isDebugEnabled()) { 195 logger.debug("Resolved method name '" + originalName + "' to handler method '" + methodName + "'"); 196 } 197 } 198 199 if (methodName != null && !StringUtils.hasText(methodName)) { 200 if (logger.isDebugEnabled()) { 201 logger.debug("Method name '" + methodName + "' is empty: treating it as no method name found"); 202 } 203 methodName = null; 204 } 205 206 if (methodName == null) { 207 if (this.defaultMethodName != null) { 208 methodName = this.defaultMethodName; 210 if (logger.isDebugEnabled()) { 211 logger.debug("Falling back to default handler method '" + this.defaultMethodName + "'"); 212 } 213 } 214 else { 215 throw new NoSuchRequestHandlingMethodException(request); 217 } 218 } 219 220 return methodName; 221 } 222 223 } 224 | Popular Tags |