/*
* Copyright 2002-2019 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
*
* https://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.servlet.handler;
import java.util.ArrayList;
import java.util.List;
import org.springframework.util.StringUtils;
/**
* {@link org.springframework.web.servlet.HandlerMapping}接口的实现,
* 该接口从URL映射到名称以斜杠(“ /”)开头的bean,
* 类似于Struts如何将URL映射至动作名称。
* {@link org.springframework.web.servlet.DispatcherServlet}和
* {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping}使用的默认实现
* 或者,{@link SimpleUrlHandlerMapping}允许声明性地自定义处理程序映射。
* 映射是从URL到bean名称。因此,传入URL“ / foo”将*映射到名为“ / foo”的处理程序,
* 或者在多个映射到单个处理程序的情况下映射到“ / foo / foo2”。
* 支持直接匹配(给定为“ / test”->已注册的“ / test”)和“ *” *匹配(给定为“ / test”->已注册“ / t *”)。
* 注意,如果适用,默认值为*,以在当前servlet映射中进行映射;
* 有关详细信息,请参见* {@link #setAlwaysUseFullPath“ alwaysUseFullPath”}属性。
* 有关模式选项的详细信息,
* 请参见* {@link org.springframework.util.AntPathMatcher} Javadoc。
*
* @author Rod Johnson
* @author Juergen Hoeller
* @see SimpleUrlHandlerMapping
*/
public class BeanNameUrlHandlerMapping extends AbstractDetectingUrlHandlerMapping {
/**
* 检查给定bean的名称和别名的URL,以“ /”开头。
*/
@Override
protected String[] determineUrlsForHandler(String beanName) {
List<String> urls = new ArrayList<>();
//如果bean的名称以/开头添加到urls中
if (beanName.startsWith("/")) {
urls.add(beanName);
}
//获取bean的别名
String[] aliases = obtainApplicationContext().getAliases(beanName);
for (String alias : aliases) {
//如果别名以/开头添加到urls中
if (alias.startsWith("/")) {
urls.add(alias);
}
}
return StringUtils.toStringArray(urls);
}
}
springmvc组件HandleMapping源码-BeanNameUrlHandlerMapping
最新推荐文章于 2021-07-29 21:32:05 发布