springmvc组件HandleMapping源码-BeanNameUrlHandlerMapping

/*
 * 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);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LouD_dm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值