SpringMVC中SimpleUrlHandlerMapping分析

SpringMVC中DispatcherServlet通过访问SimpleUrlHandlerMapping获得页面的url和具体后端控制器之间的映射

下面,分析一下SimpleUrlHandlerMapping源码,了解它的作用机理:


public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
    private final Map<String, Object> urlMap = new LinkedHashMap();

    public SimpleUrlHandlerMapping() {
    }

    public void setMappings(Properties mappings) {
        CollectionUtils.mergePropertiesIntoMap(mappings, this.urlMap);
    }

    public void setUrlMap(Map<String, ?> urlMap) {
        this.urlMap.putAll(urlMap);
    }

    public Map<String, ?> getUrlMap() {
        return this.urlMap;
    }

    public void initApplicationContext() throws BeansException {
        super.initApplicationContext();
        this.registerHandlers(this.urlMap);
    }

    protected void registerHandlers(Map<String, Object> urlMap) throws BeansException {
        String url;
        Object handler;
        if (urlMap.isEmpty()) {
            this.logger.warn("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping");
        } else {
            for(Iterator var2 = urlMap.entrySet().iterator(); var2.hasNext(); this.registerHandler(url, handler)) {
                Entry<String, Object> entry = (Entry)var2.next();
                url = (String)entry.getKey();
                handler = entry.getValue();
                if (!url.startsWith("/")) {
                    url = "/" + url;
                }

                if (handler instanceof String) {
                    handler = ((String)handler).trim();
                }
            }
        }

    }
}

  可以看到,它继承于AbstractUrlHandlerMapping,主要是对urlMap进行设置,即创建url与后端控制器映射,其中主要有两种设置方法,第一种为用set方法直接设置,可以在spring配置文件中配置urlMap



        <property name="mappings">
            <props>
                <prop key="">helloController</prop>
            </props>
        </property>
        <property name="urlMap">
            <map>
                <entry key="" value=""/>
            </map>
        </property>
   
或者说可以用Properties设置参数,此时设置xml文档为<pros></props>在<prop key>class</prop>设置key和映射的class文件

而如果用Properties设置参数,它内部会调用setMappings方法,而在方法中又调用了CollectionUtils.mergePropertiesIntoMap(mappings, this.urlMap)方法,可以猜想,此方法是解析properties文件获得相应的映射关系,然后在赋值给urlMap,查看其源码:

public static <K, V> void mergePropertiesIntoMap(Properties props, Map<K, V> map) {
        if (map == null) {
            throw new IllegalArgumentException("Map must not be null");
        } else {
            String key;
            Object value;
            if (props != null) {
                for(Enumeration en = props.propertyNames(); en.hasMoreElements(); map.put(key, value)) {
                    key = (String)en.nextElement();
                    value = props.get(key);
                    if (value == null) {
                        value = props.getProperty(key);
                    }
                }
            }

        }
    }
可以看出,如果map不是null时,用Enumeration解析Properties文件,将配置文件中key和value赋值给map

最下面的registerHandlers方法则是将urlMap解析并将其数据放入registerHandler等待调用.

 
 
 
 
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值